我的目标是使用pygame制作复古的月球着陆器游戏。现在我正试图创造登陆的山脉和平台的背景。我希望每次开始新游戏时平台和山脉都会有所不同。我的想法是绘制随机数量的水平线,随机长度作为平台。我有一些问题:
我目前工作的代码目前生成随机行,但我不确定如何进一步开发代码。
for platforms in range(1, random.randint(1, 10)):
rand_x_start = random.randint(0, 400)
rand_x_end = random.randint(0, 400)
rand_y = random.randint(HEIGHT / 2, HEIGHT)
pygame.draw.line(self.background, white, (rand_x_start, rand_y), (rand_x_end, rand_y), 2)
我已尝试查看有关随机生成的许多问题和教程,但没有一个与我正在寻找的内容非常接近。
我必须解决重叠问题的一个想法是,如果有办法去阅读"或"见"以前创建过的行并根据某些方式限制绘图...但我不知道如何做到这一点。
答案 0 :(得分:1)
我把以下代码放在一起,可以帮到你:
import random
from collections import namedtuple
from PIL import Image, ImageDraw
GaussianDistribution = namedtuple('GaussianDistribution', ['mu', 'sigma'])
Platform = namedtuple('Platform', ['start', 'end', 'height'])
Point = namedtuple('Point', ['x', 'y'])
PLATFORM_HEIGHT_DISTRIBUTION = GaussianDistribution(50, 10)
PLATFORM_WIDTH_DISTRIBUTION = GaussianDistribution(100, 10)
INTER_PLATFORM_DISTRIBUTION = GaussianDistribution(500, 20)
RANDOM = random.Random()
def sample_distribution(distribution):
return RANDOM.normalvariate(distribution.mu, distribution.sigma)
def create_platforms(num_platforms):
last_platform_end = 0
for i in range(num_platforms):
start = int(sample_distribution(INTER_PLATFORM_DISTRIBUTION) + last_platform_end)
end = int(sample_distribution(PLATFORM_WIDTH_DISTRIBUTION) + start)
height = int(sample_distribution(PLATFORM_HEIGHT_DISTRIBUTION))
last_platform_end = end
yield Platform(start, end, height)
def create_mountains_between_points(p1, p2):
mountain_start = p1.x
mountain_end = p2.x
num_points = int((mountain_end - mountain_start) / 10)
for i in range(num_points):
# TODO: use 1D Perlin function to generate point.y
yield Point(mountain_start + i * 10, RANDOM.random() * 100)
def create_terrain(platforms):
origin = Point(0, 0)
first_platform_start = Point(platforms[0].start, platforms[0].height)
terrain = []
terrain += list(create_mountains_between_points(origin, first_platform_start))
for i, platform in enumerate(platforms):
platform_starts_at = Point(platform.start, platform.height)
platform_ends_at = Point(platform.end, platform.height)
terrain.append(platform_starts_at)
terrain.append(platform_ends_at)
if i < len(platforms) - 1:
next_platform = platforms[i + 1]
next_platform_starts_at = Point(next_platform.start, next_platform.height)
mountains = create_mountains_between_points(platform_ends_at, next_platform_starts_at)
terrain += mountains
return terrain
def draw_terrain(terrain):
im = Image.new('RGBA', (4000, 200), (255, 255, 255, 0))
draw = ImageDraw.Draw(im)
draw.line(terrain, fill=(0, 0, 0, 255))
im.show()
if __name__ == '__main__':
platforms = list(create_platforms(num_platforms=10))
terrain = create_terrain(platforms)
draw_terrain(terrain)
这会生成以下地形。你可以看到有山脉分隔的平坦区域(平台)。保证平台不会重叠。它们也保证“可访问”,因为它们永远不会产生任何地形。
我通过从均匀分布中抽样来生成山脉的y坐标。你可以使用我前面提到的Perlin噪声算法使山看起来更好。我发现this attempt在pygame应用程序中使用Perlin噪音。