modify world generation rule
This commit is contained in:
parent
40fa44f936
commit
1a210382ae
2
main.py
2
main.py
@ -29,7 +29,7 @@ while running:
|
|||||||
if event.button == 1: # 左键放置方块
|
if event.button == 1: # 左键放置方块
|
||||||
if block.id == "air":
|
if block.id == "air":
|
||||||
player.put_block(bx, by)
|
player.put_block(bx, by)
|
||||||
elif event.button == 3: # 右键拆除方块
|
elif event.button == 3 and block.destroyable: # 右键拆除方块
|
||||||
player.put_block(bx, by, 'delete')
|
player.put_block(bx, by, 'delete')
|
||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_1:
|
if event.key == pygame.K_1:
|
||||||
|
@ -8,6 +8,7 @@ COLORS = {
|
|||||||
"stone": (128, 128, 128),
|
"stone": (128, 128, 128),
|
||||||
"sand": (238, 232, 170),
|
"sand": (238, 232, 170),
|
||||||
"water": (0, 0, 255),
|
"water": (0, 0, 255),
|
||||||
|
"bedrock": (0, 0, 0),
|
||||||
"player": (255, 0, 0)
|
"player": (255, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ class Block:
|
|||||||
flowable = False # 可流动
|
flowable = False # 可流动
|
||||||
fallible = False # 可下坠
|
fallible = False # 可下坠
|
||||||
collision = True # 可以碰撞
|
collision = True # 可以碰撞
|
||||||
|
destroyable = True # 可被破坏
|
||||||
|
|
||||||
def __init__(self, x: int, y: int):
|
def __init__(self, x: int, y: int):
|
||||||
self.x = x
|
self.x = x
|
||||||
@ -42,7 +43,7 @@ class Block:
|
|||||||
# 判碰撞:
|
# 判碰撞:
|
||||||
return left < player.x < right and top < player.y < bottom
|
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
|
real_x = self.x * BLOCK_SIZE + BLOCK_SIZE / 2
|
||||||
@ -108,3 +109,8 @@ class WaterBlock(Block):
|
|||||||
|
|
||||||
def __init__(self, x: int, y: int):
|
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
|
@ -1,6 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
from module import WIDTH, BLOCK_SIZE, HEIGHT
|
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
|
ground_level = HEIGHT // BLOCK_SIZE // 2
|
||||||
|
|
||||||
for x in range(WIDTH // BLOCK_SIZE):
|
for x in range(WIDTH // BLOCK_SIZE):
|
||||||
|
#
|
||||||
|
# AIR BLOCK
|
||||||
|
# height ============================= GRASS BLOCK
|
||||||
|
# 4 DIRT BLOCK
|
||||||
|
# +5 =============================
|
||||||
|
# STONE BLOCK
|
||||||
|
# max ============================= BEDROCK BLOCK
|
||||||
|
#
|
||||||
column = []
|
column = []
|
||||||
height = ground_level + random.randint(-3, 3)
|
height = ground_level + random.randint(-3, 3)
|
||||||
for y in range(HEIGHT // BLOCK_SIZE):
|
for y in range(HEIGHT // BLOCK_SIZE):
|
||||||
if y > height + 2:
|
if y < height:
|
||||||
column.append(StoneBlock(x, y))
|
block = AirBlock(x, y)
|
||||||
elif y == height:
|
elif y == height:
|
||||||
column.append(GrassBlock(x, y))
|
block = GrassBlock(x, y)
|
||||||
elif y > height - 3:
|
elif height < y < height + 5:
|
||||||
column.append(DirtBlock(x, y))
|
block = DirtBlock(x, y)
|
||||||
|
elif y == HEIGHT // BLOCK_SIZE - 1:
|
||||||
|
block = BedrockBlock(x, y)
|
||||||
else:
|
else:
|
||||||
column.append(AirBlock(x, y))
|
block = StoneBlock(x, y)
|
||||||
|
column.append(block)
|
||||||
|
|
||||||
world.append(column)
|
world.append(column)
|
||||||
|
|
||||||
return world
|
return world
|
Reference in New Issue
Block a user