refactor code structure
This commit is contained in:
parent
d74c866caf
commit
823158a8eb
70
main.py
70
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()
|
||||
|
||||
# 游戏循环
|
||||
|
18
module/__init__.py
Normal file
18
module/__init__.py
Normal file
@ -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
|
23
module/player.py
Normal file
23
module/player.py
Normal file
@ -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
|
24
module/utils.py
Normal file
24
module/utils.py
Normal file
@ -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
|
Reference in New Issue
Block a user