From 823158a8ebf6622605cdaad9f821332dcca9ac09 Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Fri, 31 Jan 2025 19:35:09 +0800 Subject: [PATCH] refactor code structure --- main.py | 70 +++------------------------------------------- module/__init__.py | 18 ++++++++++++ module/player.py | 23 +++++++++++++++ module/utils.py | 24 ++++++++++++++++ 4 files changed, 69 insertions(+), 66 deletions(-) create mode 100644 module/__init__.py create mode 100644 module/player.py create mode 100644 module/utils.py diff --git a/main.py b/main.py index 70fdbb7..12507d8 100644 --- a/main.py +++ b/main.py @@ -1,77 +1,15 @@ import pygame -import random + +from module import WIDTH, HEIGHT, BLOCK_SIZE, PLAYER_SPEED, GRAVITY, COLORS +from module.player import Player +from module.utils import generate_terrain # 初始化 pygame.init() -WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() -# 颜色定义 -COLORS = { - "air": (0, 191, 255), - "grass": (34, 139, 34), - "dirt": (139, 69, 19), - "stone": (128, 128, 128), - "sand": (238, 232, 170), - "water": (0, 0, 255), - "player": (255, 0, 0) -} - -# 游戏参数 -BLOCK_SIZE = 20 -GRAVITY = 0.5 -PLAYER_SPEED = 5 -JUMP_FORCE = -12 - - -# 生成地形 -def generate_terrain(): - world = [] - ground_level = HEIGHT // BLOCK_SIZE // 2 - - for x in range(WIDTH // BLOCK_SIZE): - column = [] - height = ground_level + random.randint(-3, 3) - for y in range(HEIGHT // BLOCK_SIZE): - if y > height + 2: - column.append("stone") - elif y == height: - column.append("grass") - elif y > height - 3: - column.append("dirt") - else: - column.append("air") - world.append(column) - - return world - - world = generate_terrain() - - -# 玩家类 -class Player: - def __init__(self): - self.x = WIDTH // 2 - self.y = HEIGHT // 2 - self.velocity = 0 - self.on_ground = False - self.selected_block = "grass" - - def move(self, dx, dy): - new_x = self.x + dx - new_y = self.y + dy - if 0 <= new_x < WIDTH and 0 <= new_y < HEIGHT: - self.x = new_x - self.y = new_y - - def jump(self): - if self.on_ground: - self.velocity = JUMP_FORCE - self.on_ground = False - - player = Player() # 游戏循环 diff --git a/module/__init__.py b/module/__init__.py new file mode 100644 index 0000000..0d10b2f --- /dev/null +++ b/module/__init__.py @@ -0,0 +1,18 @@ +WIDTH, HEIGHT = 800, 600 + +# 颜色定义 +COLORS = { + "air": (0, 191, 255), + "grass": (34, 139, 34), + "dirt": (139, 69, 19), + "stone": (128, 128, 128), + "sand": (238, 232, 170), + "water": (0, 0, 255), + "player": (255, 0, 0) +} + +# 游戏参数 +BLOCK_SIZE = 20 +GRAVITY = 0.5 +PLAYER_SPEED = 5 +JUMP_FORCE = -12 \ No newline at end of file diff --git a/module/player.py b/module/player.py new file mode 100644 index 0000000..ea026e0 --- /dev/null +++ b/module/player.py @@ -0,0 +1,23 @@ +# 玩家类 +from module import WIDTH, HEIGHT, JUMP_FORCE + + +class Player: + def __init__(self): + self.x = WIDTH // 2 + self.y = HEIGHT // 2 + self.velocity = 0 + self.on_ground = False + self.selected_block = "grass" + + def move(self, dx, dy): + new_x = self.x + dx + new_y = self.y + dy + if 0 <= new_x < WIDTH and 0 <= new_y < HEIGHT: + self.x = new_x + self.y = new_y + + def jump(self): + if self.on_ground: + self.velocity = JUMP_FORCE + self.on_ground = False \ No newline at end of file diff --git a/module/utils.py b/module/utils.py new file mode 100644 index 0000000..1f1df50 --- /dev/null +++ b/module/utils.py @@ -0,0 +1,24 @@ +import random +from module import WIDTH, BLOCK_SIZE, HEIGHT + + +# 生成地形 +def generate_terrain(): + world = [] + ground_level = HEIGHT // BLOCK_SIZE // 2 + + for x in range(WIDTH // BLOCK_SIZE): + column = [] + height = ground_level + random.randint(-3, 3) + for y in range(HEIGHT // BLOCK_SIZE): + if y > height + 2: + column.append("stone") + elif y == height: + column.append("grass") + elif y > height - 3: + column.append("dirt") + else: + column.append("air") + world.append(column) + + return world \ No newline at end of file