diff --git a/main.py b/main.py index 9f4413c..11a7b3e 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ while running: if event.button == 1: # 左键放置方块 if block.id == "air": player.put_block(bx, by) - elif event.button == 3: # 右键拆除方块 + elif event.button == 3 and block.destroyable: # 右键拆除方块 player.put_block(bx, by, 'delete') elif event.type == pygame.KEYDOWN: if event.key == pygame.K_1: diff --git a/module/__init__.py b/module/__init__.py index 6d0d58a..f43d5d1 100644 --- a/module/__init__.py +++ b/module/__init__.py @@ -8,6 +8,7 @@ COLORS = { "stone": (128, 128, 128), "sand": (238, 232, 170), "water": (0, 0, 255), + "bedrock": (0, 0, 0), "player": (255, 0, 0) } diff --git a/module/block.py b/module/block.py index 97db6c6..5455ccd 100644 --- a/module/block.py +++ b/module/block.py @@ -12,6 +12,7 @@ class Block: flowable = False # 可流动 fallible = False # 可下坠 collision = True # 可以碰撞 + destroyable = True # 可被破坏 def __init__(self, x: int, y: int): self.x = x @@ -42,7 +43,7 @@ class Block: # 判碰撞: return left < player.x < right and top < player.y < bottom - def get_collision_surface(self) -> tuple[int]: + def get_collision_surface(self) -> tuple[int, int, int, int]: # 获得方块表面界限(碰撞面) # 上、右、下、左 real_x = self.x * BLOCK_SIZE + BLOCK_SIZE / 2 @@ -107,4 +108,9 @@ class WaterBlock(Block): collision = False def __init__(self, x: int, y: int): - super().__init__(x, y) \ No newline at end of file + super().__init__(x, y) + +class BedrockBlock(Block): + id = 'bedrock' + color = COLORS.get(id) + destroyable = False \ No newline at end of file diff --git a/module/utils.py b/module/utils.py index 3c2da17..61a4b17 100644 --- a/module/utils.py +++ b/module/utils.py @@ -1,6 +1,6 @@ import random from module import WIDTH, BLOCK_SIZE, HEIGHT -from module.block import StoneBlock, GrassBlock, DirtBlock, AirBlock, Block +from module.block import StoneBlock, GrassBlock, DirtBlock, AirBlock, Block, BedrockBlock # 生成地形 @@ -9,17 +9,29 @@ def generate_terrain() -> list[list[Block]]: ground_level = HEIGHT // BLOCK_SIZE // 2 for x in range(WIDTH // BLOCK_SIZE): + # + # AIR BLOCK + # height ============================= GRASS BLOCK + # 4 DIRT BLOCK + # +5 ============================= + # STONE BLOCK + # max ============================= BEDROCK BLOCK + # column = [] height = ground_level + random.randint(-3, 3) for y in range(HEIGHT // BLOCK_SIZE): - if y > height + 2: - column.append(StoneBlock(x, y)) + if y < height: + block = AirBlock(x, y) elif y == height: - column.append(GrassBlock(x, y)) - elif y > height - 3: - column.append(DirtBlock(x, y)) + block = GrassBlock(x, y) + elif height < y < height + 5: + block = DirtBlock(x, y) + elif y == HEIGHT // BLOCK_SIZE - 1: + block = BedrockBlock(x, y) else: - column.append(AirBlock(x, y)) + block = StoneBlock(x, y) + column.append(block) + world.append(column) return world \ No newline at end of file