first commit

This commit is contained in:
Jeffrey Hsu 2025-01-31 19:23:31 +08:00
commit 9a73fa0ff9
3 changed files with 161 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.venv/
venv/
.idea/
.vscode/
*.pyc

11
README.md Normal file
View File

@ -0,0 +1,11 @@
## LXC's Game
### How to run
```bash
git pull https://cantyonion.site/git/cantyonion/LXCsGame.git
cd LXCsGame
python -m venv .venv
pip install pygame
python main.py
```

144
main.py Normal file
View File

@ -0,0 +1,144 @@
import pygame
import random
# 初始化
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()
# 游戏循环
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 x in range(len(world)):
for y in range(len(world[x])):
if world[x][y] != "air":
rect = pygame.Rect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, COLORS[world[x][y]], rect)
# 绘制玩家
pygame.draw.circle(screen, COLORS["player"], (player.x, player.y), 10)
# 显示提示文字
font = pygame.font.SysFont(None, 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()