|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from random import randint, choice
import pygame
# CONSTANTS & VARIABLES #
SS = SW, SH = (1500, 1000) # SCREEN [SIZE / WIDTH / HEIGHT]
BW = BH = 20 # BLOCK [WIDTH / HEIGHT]
WW = 75 # WORLD WIDTH
WH = 72 # WORLD HEIGHT
BLOCKS = [
# name, color
("air", (128, 192, 255)),
("grass_land", (128, 255, 192)),
("stone", (96, 96, 96)),
]
rx = 0 # render x
ry = 25 # render y
screen = None
world = []
# FUNCTIONS #
def generate_world(): # 生成世界
world = [[0 for _ in range(WW)] for _ in range(WH)] # 世界
h = [0 for _ in range(WW)] # 各个平面坐标的高度
for x in range(WW): # 生成 16 列的世界
if x == 0: # 第一列是随机高度
h[x] = randint(40, 56)
else: # 上一列的高度 +1 / 0 / -1
h[x] = h[x - 1] + choice([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
for y in range(0, 72): # 生成高度
if y < h[x]: # 高度低于需要生成的高度,设置为石头
world[y][x] = 2
elif y == h[x]: # 高度等于需要生成的高度,设置为草地
world[y][x] = 1
else: # 高度高于需要生成的高度,设置为空气
world[y][x] = 0
return world
def initalize(): # 初始化函数
global screen, world
pygame.init()
screen = pygame.display.set_mode(SS)
pygame.display.set_caption("GezheLand")
world = generate_world()
# MAIN PROGRAM #
initalize()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
for x in range(0, SW // BW):
for y in range(0, SH // BH):
try:
pygame.draw.rect(screen, BLOCKS[world[y + ry][x + rx]][1], (x * BW, SH - (y + 1) * BH, BW, BH))
except:
pass
pygame.display.flip()
pygame.quit()
rt,ai 说 58 行应该是 SH - (y + 1) * BH(已经在代码里加上了),但这样造成的结果是少一行黑边,并没有让黑边消失
from random import randint, choice
import pygame
# CONSTANTS & VARIABLES #
SS = SW, SH = (1500, 1000) # SCREEN [SIZE / WIDTH / HEIGHT]
BW = BH = 20 # BLOCK [WIDTH / HEIGHT]
WW = 75 # WORLD WIDTH
WH = 72 # WORLD HEIGHT
BLOCKS = [
# name, color
("air", (128, 192, 255)),
("grass_land", (128, 255, 192)),
("stone", (96, 96, 96)),
]
rx = 0 # render x
ry = WH - SH // BH # Adjusted to start rendering from the bottom of the world matrix
screen = None
world = []
# FUNCTIONS #
def generate_world(): # 生成世界
world = [[0 for _ in range(WW)] for _ in range(WH)] # 世界
h = [0 for _ in range(WW)] # 各个平面坐标的高度
for x in range(WW): # 生成 16 列的世界
if x == 0: # 第一列是随机高度
h[x] = randint(40, 56)
else: # 上一列的高度 +1 / 0 / -1
h[x] = h[x - 1] + choice([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
for y in range(WH): # 生成高度
if y < h[x]: # 高度低于需要生成的高度,设置为石头
world[y][x] = 2
elif y == h[x]: # 高度等于需要生成的高度,设置为草地
world[y][x] = 1
else: # 高度高于需要生成的高度,设置为空气
world[y][x] = 0
return world
def initialize(): # 初始化函数
global screen, world
pygame.init()
screen = pygame.display.set_mode(SS)
pygame.display.set_caption("GezheLand")
world = generate_world()
# MAIN PROGRAM #
initialize()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
for x in range(SW // BW):
for y in range(SH // BH):
try:
block_color = BLOCKS[world[y + ry][x + rx]][1]
pygame.draw.rect(screen, block_color, (x * BW, SH - (y + 1) * BH, BW, BH))
except IndexError:
pass # This pass handles cases where the block is out of the world's range
pygame.display.flip()
pygame.quit()
|
|