This repository has been archived on 2025-02-14. You can view files and clone it, but cannot push or open issues or pull requests.
LXCsGame/main.py

88 lines
2.8 KiB
Python

import pygame
from module import WIDTH, HEIGHT, BLOCK_SIZE, PLAYER_SPEED, GRAVITY, COLORS, OPTIONS
from module.block import GrassBlock, StoneBlock, SandBlock, WaterBlock
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(world)
# 游戏循环
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
block = world[bx][by]
if event.button == 1: # 左键放置方块
if block.id == "air":
player.put_block(bx, by)
elif event.button == 3 and block.destroyable: # 右键拆除方块
player.put_block(bx, by, 'delete')
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
player.select_block(GrassBlock)
elif event.key == pygame.K_2:
player.select_block(StoneBlock)
elif event.key == pygame.K_3:
player.select_block(SandBlock)
elif event.key == pygame.K_4:
player.select_block(WaterBlock)
# 玩家移动
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()
if keys[pygame.K_F3]:
OPTIONS["debug"] = True
if keys[pygame.K_F4]:
OPTIONS["debug"] = False
# 物理模拟
if not player.on_ground:
player.velocity = GRAVITY
player.move(0, player.velocity)
# 绘制世界
for row in world:
for block in row:
rect = pygame.Rect(block.x * BLOCK_SIZE, block.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, block.color, rect)
# 绘制玩家
player.draw(screen)
# 显示提示文字
font = pygame.font.SysFont('KaiTi', 24)
text = font.render(f"Selected: {player.selected_block.id} (1-4切换方块)", True, (255, 255, 255))
screen.blit(text, (10, 10))
# Debug Mode
if OPTIONS["debug"]:
debug_text = font.render("调试模式", True, (255, 255, 255))
screen.blit(debug_text, (10, 34))
for row in world:
for block in row:
if block.check_collision(player) and block.id != "air":
pygame.draw.circle(screen, COLORS["player"], block.get_center_vector(), 2)
pygame.display.flip()
clock.tick(60)
pygame.quit()