Compare commits
6 Commits
9a73fa0ff9
...
3cb2bf4b74
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cb2bf4b74 | |||
| e2cdd2e4e5 | |||
| d7f5e990fd | |||
| 64c710aae7 | |||
| 823158a8eb | |||
| d74c866caf |
@@ -6,6 +6,12 @@
|
||||
git pull https://cantyonion.site/git/cantyonion/LXCsGame.git
|
||||
cd LXCsGame
|
||||
python -m venv .venv
|
||||
|
||||
# for Windows user
|
||||
.venv\Script\active.bat
|
||||
# for Linux user
|
||||
source .venv/bin/active
|
||||
|
||||
pip install pygame
|
||||
python main.py
|
||||
```
|
||||
101
main.py
101
main.py
@@ -1,77 +1,16 @@
|
||||
import pygame
|
||||
import random
|
||||
|
||||
from module import WIDTH, HEIGHT, BLOCK_SIZE, PLAYER_SPEED, GRAVITY, COLORS
|
||||
from module.block import AirBlock, GrassBlock, StoneBlock, SandBlock, WaterBlock
|
||||
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()
|
||||
|
||||
# 游戏循环
|
||||
@@ -85,20 +24,22 @@ while running:
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
bx = mx // BLOCK_SIZE
|
||||
by = my // BLOCK_SIZE
|
||||
|
||||
block = world[bx][by]
|
||||
if event.button == 1: # 左键放置方块
|
||||
if world[bx][by] == "air":
|
||||
world[bx][by] = player.selected_block
|
||||
if block.id == "air":
|
||||
world[bx][by] = player.selected_block(bx, by)
|
||||
elif event.button == 3: # 右键拆除方块
|
||||
world[bx][by] = "air"
|
||||
world[bx][by] = AirBlock(bx, by)
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_1:
|
||||
player.selected_block = "grass"
|
||||
player.selected_block = GrassBlock
|
||||
elif event.key == pygame.K_2:
|
||||
player.selected_block = "stone"
|
||||
player.selected_block = StoneBlock
|
||||
elif event.key == pygame.K_3:
|
||||
player.selected_block = "sand"
|
||||
player.selected_block = SandBlock
|
||||
elif event.key == pygame.K_4:
|
||||
player.selected_block = "water"
|
||||
player.selected_block = WaterBlock
|
||||
|
||||
# 玩家移动
|
||||
keys = pygame.key.get_pressed()
|
||||
@@ -124,18 +65,18 @@ while running:
|
||||
|
||||
# 绘制世界
|
||||
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)
|
||||
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(None, 24)
|
||||
text = font.render(f"Selected: {player.selected_block} (1-4切换方块)", True, (255, 255, 255))
|
||||
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))
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
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
|
||||
64
module/block.py
Normal file
64
module/block.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from module import COLORS
|
||||
|
||||
class Block:
|
||||
id = ''
|
||||
color = ''
|
||||
flowable = False # 可流动
|
||||
fallible = False # 可下坠
|
||||
collision = True # 可以碰撞
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
class GrassBlock(Block):
|
||||
id = 'grass'
|
||||
color = COLORS.get(id)
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class DirtBlock(Block):
|
||||
id = 'dirt'
|
||||
color = COLORS.get(id)
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class SandBlock(Block):
|
||||
id = 'sand'
|
||||
color = COLORS.get(id)
|
||||
flowable = True
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class StoneBlock(Block):
|
||||
id = 'stone'
|
||||
color = COLORS.get(id)
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class AirBlock(Block):
|
||||
id = 'air'
|
||||
color = COLORS.get(id)
|
||||
collision = False
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
|
||||
|
||||
class WaterBlock(Block):
|
||||
id = 'water'
|
||||
color = COLORS.get(id)
|
||||
flowable = True
|
||||
collision = False
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y)
|
||||
24
module/player.py
Normal file
24
module/player.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from module import WIDTH, HEIGHT, JUMP_FORCE
|
||||
from module.block import AirBlock
|
||||
|
||||
|
||||
# 玩家类
|
||||
class Player:
|
||||
def __init__(self):
|
||||
self.x = WIDTH // 2
|
||||
self.y = HEIGHT // 2
|
||||
self.velocity = 0
|
||||
self.on_ground = False
|
||||
self.selected_block = AirBlock
|
||||
|
||||
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
|
||||
25
module/utils.py
Normal file
25
module/utils.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import random
|
||||
from module import WIDTH, BLOCK_SIZE, HEIGHT
|
||||
from module.block import StoneBlock, GrassBlock, DirtBlock, AirBlock, Block
|
||||
|
||||
|
||||
# 生成地形
|
||||
def generate_terrain() -> list[list[Block]]:
|
||||
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(StoneBlock(x, y))
|
||||
elif y == height:
|
||||
column.append(GrassBlock(x, y))
|
||||
elif y > height - 3:
|
||||
column.append(DirtBlock(x, y))
|
||||
else:
|
||||
column.append(AirBlock(x, y))
|
||||
world.append(column)
|
||||
|
||||
return world
|
||||
Reference in New Issue
Block a user