implement block class
This commit is contained in:
parent
64c710aae7
commit
d7f5e990fd
10
main.py
10
main.py
@ -62,11 +62,11 @@ while running:
|
||||
|
||||
# 绘制世界
|
||||
screen.fill(COLORS["air"])
|
||||
for x in range(len(world)):
|
||||
for y in range(len(world[x])):
|
||||
if world[x][y] != "air":
|
||||
rect = pygame.Rect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
|
||||
pygame.draw.rect(screen, COLORS[world[x][y]], rect)
|
||||
for row in world:
|
||||
for block in row:
|
||||
if block.id != "air":
|
||||
rect = pygame.Rect(block.x * BLOCK_SIZE, block.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
|
||||
pygame.draw.rect(screen, block.color, rect)
|
||||
|
||||
# 绘制玩家
|
||||
pygame.draw.circle(screen, COLORS["player"], (player.x, player.y), 10)
|
||||
|
54
module/block.py
Normal file
54
module/block.py
Normal file
@ -0,0 +1,54 @@
|
||||
from module import COLORS
|
||||
|
||||
class Block:
|
||||
id = ''
|
||||
color = ''
|
||||
flowable = False # 可流动
|
||||
fallible = False # 可下坠
|
||||
collision = True # 可以碰撞
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
class GrassBlock(Block):
|
||||
id = 'grass'
|
||||
color = COLORS.get(id)
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class DirtBlock(Block):
|
||||
id = 'dirt'
|
||||
color = COLORS.get(id)
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class SandBlock(Block):
|
||||
id = 'sand'
|
||||
color = COLORS.get(id)
|
||||
flowable = True
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class StoneBlock(Block):
|
||||
id = 'stone'
|
||||
color = COLORS.get(id)
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class AirBlock(Block):
|
||||
id = 'air'
|
||||
color = COLORS.get(id)
|
||||
collision = False
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
@ -1,9 +1,10 @@
|
||||
import random
|
||||
from module import WIDTH, BLOCK_SIZE, HEIGHT
|
||||
from module.block import StoneBlock, GrassBlock, DirtBlock, AirBlock, Block
|
||||
|
||||
|
||||
# 生成地形
|
||||
def generate_terrain():
|
||||
def generate_terrain() -> list[list[Block]]:
|
||||
world = []
|
||||
ground_level = HEIGHT // BLOCK_SIZE // 2
|
||||
|
||||
@ -12,13 +13,13 @@ def generate_terrain():
|
||||
height = ground_level + random.randint(-3, 3)
|
||||
for y in range(HEIGHT // BLOCK_SIZE):
|
||||
if y > height + 2:
|
||||
column.append("stone")
|
||||
column.append(StoneBlock(x, y))
|
||||
elif y == height:
|
||||
column.append("grass")
|
||||
column.append(GrassBlock(x, y))
|
||||
elif y > height - 3:
|
||||
column.append("dirt")
|
||||
column.append(DirtBlock(x, y))
|
||||
else:
|
||||
column.append("air")
|
||||
column.append(AirBlock(x, y))
|
||||
world.append(column)
|
||||
|
||||
return world
|
Reference in New Issue
Block a user