modify world generation rule

This commit is contained in:
Jeffrey Hsu 2025-02-01 12:30:21 +08:00
parent 40fa44f936
commit 1a210382ae
4 changed files with 29 additions and 10 deletions

View File

@ -29,7 +29,7 @@ while running:
if event.button == 1: # 左键放置方块
if block.id == "air":
player.put_block(bx, by)
elif event.button == 3: # 右键拆除方块
elif event.button == 3 and block.destroyable: # 右键拆除方块
player.put_block(bx, by, 'delete')
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:

View File

@ -8,6 +8,7 @@ COLORS = {
"stone": (128, 128, 128),
"sand": (238, 232, 170),
"water": (0, 0, 255),
"bedrock": (0, 0, 0),
"player": (255, 0, 0)
}

View File

@ -12,6 +12,7 @@ class Block:
flowable = False # 可流动
fallible = False # 可下坠
collision = True # 可以碰撞
destroyable = True # 可被破坏
def __init__(self, x: int, y: int):
self.x = x
@ -42,7 +43,7 @@ class Block:
# 判碰撞:
return left < player.x < right and top < player.y < bottom
def get_collision_surface(self) -> tuple[int]:
def get_collision_surface(self) -> tuple[int, int, int, int]:
# 获得方块表面界限(碰撞面)
# 上、右、下、左
real_x = self.x * BLOCK_SIZE + BLOCK_SIZE / 2
@ -107,4 +108,9 @@ class WaterBlock(Block):
collision = False
def __init__(self, x: int, y: int):
super().__init__(x, y)
super().__init__(x, y)
class BedrockBlock(Block):
id = 'bedrock'
color = COLORS.get(id)
destroyable = False

View File

@ -1,6 +1,6 @@
import random
from module import WIDTH, BLOCK_SIZE, HEIGHT
from module.block import StoneBlock, GrassBlock, DirtBlock, AirBlock, Block
from module.block import StoneBlock, GrassBlock, DirtBlock, AirBlock, Block, BedrockBlock
# 生成地形
@ -9,17 +9,29 @@ def generate_terrain() -> list[list[Block]]:
ground_level = HEIGHT // BLOCK_SIZE // 2
for x in range(WIDTH // BLOCK_SIZE):
#
# AIR BLOCK
# height ============================= GRASS BLOCK
# 4 DIRT BLOCK
# +5 =============================
# STONE BLOCK
# max ============================= BEDROCK BLOCK
#
column = []
height = ground_level + random.randint(-3, 3)
for y in range(HEIGHT // BLOCK_SIZE):
if y > height + 2:
column.append(StoneBlock(x, y))
if y < height:
block = AirBlock(x, y)
elif y == height:
column.append(GrassBlock(x, y))
elif y > height - 3:
column.append(DirtBlock(x, y))
block = GrassBlock(x, y)
elif height < y < height + 5:
block = DirtBlock(x, y)
elif y == HEIGHT // BLOCK_SIZE - 1:
block = BedrockBlock(x, y)
else:
column.append(AirBlock(x, y))
block = StoneBlock(x, y)
column.append(block)
world.append(column)
return world