diff --git a/main.py b/main.py index 70ec0af..704b486 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import pygame from module import WIDTH, HEIGHT, BLOCK_SIZE, PLAYER_SPEED, GRAVITY, COLORS +from module.block import AirBlock, GrassBlock, StoneBlock, SandBlock, WaterBlock from module.player import Player from module.utils import generate_terrain @@ -23,20 +24,22 @@ while running: mx, my = pygame.mouse.get_pos() bx = mx // BLOCK_SIZE by = my // BLOCK_SIZE + + block = world[bx][by] if event.button == 1: # 左键放置方块 - if world[bx][by] == "air": - world[bx][by] = player.selected_block + if block.id == "air": + world[bx][by] = player.selected_block(bx, by) elif event.button == 3: # 右键拆除方块 - world[bx][by] = "air" + world[bx][by] = AirBlock(bx, by) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_1: - player.selected_block = "grass" + player.selected_block = GrassBlock elif event.key == pygame.K_2: - player.selected_block = "stone" + player.selected_block = StoneBlock elif event.key == pygame.K_3: - player.selected_block = "sand" + player.selected_block = SandBlock elif event.key == pygame.K_4: - player.selected_block = "water" + player.selected_block = WaterBlock # 玩家移动 keys = pygame.key.get_pressed() @@ -73,7 +76,7 @@ while running: # 显示提示文字 font = pygame.font.SysFont('KaiTi', 24) - text = font.render(f"Selected: {player.selected_block} (1-4切换方块)", True, (255, 255, 255)) + text = font.render(f"Selected: {player.selected_block.id} (1-4切换方块)", True, (255, 255, 255)) screen.blit(text, (10, 10)) pygame.display.flip() diff --git a/module/block.py b/module/block.py index 97bc36c..24209b6 100644 --- a/module/block.py +++ b/module/block.py @@ -52,3 +52,13 @@ class AirBlock(Block): def __init__(self, x: int, y: int): super().__init__(x, y) + + +class WaterBlock(Block): + id = 'water' + color = COLORS.get(id) + flowable = True + collision = False + + def __init__(self, x: int, y: int): + super().__init__(x, y) \ No newline at end of file diff --git a/module/player.py b/module/player.py index be28381..b6e0c7e 100644 --- a/module/player.py +++ b/module/player.py @@ -1,4 +1,5 @@ from module import WIDTH, HEIGHT, JUMP_FORCE +from module.block import AirBlock # 玩家类 @@ -8,7 +9,7 @@ class Player: self.y = HEIGHT // 2 self.velocity = 0 self.on_ground = False - self.selected_block = "grass" + self.selected_block = AirBlock def move(self, dx, dy): new_x = self.x + dx