import pygame from module import WIDTH, HEIGHT, BLOCK_SIZE, PLAYER_SPEED, GRAVITY, COLORS from module.player import Player from module.utils import generate_terrain # 初始化 pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() world = generate_terrain() player = Player() # 游戏循环 running = True while running: # 事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: mx, my = pygame.mouse.get_pos() bx = mx // BLOCK_SIZE by = my // BLOCK_SIZE if event.button == 1: # 左键放置方块 if world[bx][by] == "air": world[bx][by] = player.selected_block elif event.button == 3: # 右键拆除方块 world[bx][by] = "air" elif event.type == pygame.KEYDOWN: if event.key == pygame.K_1: player.selected_block = "grass" elif event.key == pygame.K_2: player.selected_block = "stone" elif event.key == pygame.K_3: player.selected_block = "sand" elif event.key == pygame.K_4: player.selected_block = "water" # 玩家移动 keys = pygame.key.get_pressed() if keys[pygame.K_a]: player.move(-PLAYER_SPEED, 0) if keys[pygame.K_d]: player.move(PLAYER_SPEED, 0) if keys[pygame.K_w]: player.jump() # 物理模拟 player.velocity += GRAVITY player.move(0, player.velocity) # 碰撞检测 px = int(player.x // BLOCK_SIZE) py = int(player.y // BLOCK_SIZE) if py < len(world[px]) - 1 and world[px][py + 1] != "air": player.on_ground = True player.velocity = 0 else: player.on_ground = False # 绘制世界 screen.fill(COLORS["air"]) 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) # 显示提示文字 font = pygame.font.SysFont('KaiTi', 24) text = font.render(f"Selected: {player.selected_block} (1-4切换方块)", True, (255, 255, 255)) screen.blit(text, (10, 10)) pygame.display.flip() clock.tick(60) pygame.quit()