有没有办法将我地图上未连接的白色空间链接?
我正在使用Python中的Perlin-Noise库来尝试创建程序生成的地图。我做到了,以便在某些级别上有黑色块,在其他层面上有白色块。我想要它,以便我的播放器能够在白色块上行走,但与黑色块相撞(但希望以后再出现)。问题是,有时白色区域彼此分离,例如在此示例中(绿色广场是玩家)。我想要的是,通过黑色块来制定某种路径,以便玩家能够到达白色区域。这是我的代码:
from perlin_noise import PerlinNoise
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
SCREENWIDTH = 800
SCREENHEIGHT = 800
###################################################################################
class Block(pygame.sprite.Sprite):
def __init__(self, colour, x, y):
super(Block, self).__init__()
self.image = pygame.Surface([4, 4]) # create a surface to draw onto
self.image.fill(colour) # Set a fill colour, as the R,G,B are in equal proportion this will be a grey
self.rect = self.image.get_rect()
self.rect.x = x * 4
self.rect.y = y * 4
####################################################################################
noise1 = PerlinNoise(octaves=4)
noise2 = PerlinNoise(octaves=8)
noise3 = PerlinNoise(octaves=16)
noise4 = PerlinNoise(octaves=32)
xpix, ypix = 250, 250
pic = []
for i in range(xpix):
row = []
for j in range(ypix):
noise_val = noise1([i/xpix, j/ypix])
noise_val += 0.5 * noise2([i/xpix, j/ypix])
noise_val += 0.25 * noise3([i/xpix, j/ypix])
noise_val += 0.125 * noise4([i/xpix, j/ypix])
row.append(noise_val)
pic.append(row)
#################################################################################
size = (SCREENWIDTH, SCREENHEIGHT)
window = pygame.display.set_mode((size))
screen = pygame.display.get_surface()
block_group = pygame.sprite.Group()
y = 0
for row in pic:
x = 0
for col in row:
if -0.1 > pic[y][x]:
b = Block(BLACK, x, y)
else:
b = Block(WHITE, x, y)
block_group.add(b)
x += 1
y += 1
###################################################################
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
block_group.draw(screen)
p.draw(screen)
pygame.display.flip()
I'm using the Perlin-noise library in python to try and create procedurally generated map. I have made it so that at certain levels, there are black blocks, and at other levels there are white blocks. I want it so that my player will be able to walk on the white blocks but collide with the black blocks (but that will come later, hopefully). The problem is, sometimes the white areas are detached from each other such as in this example (the green square is the player). What I want is that some kind of path is made through the black blocks in order for the player to be able to reach the white areas. Here is my code:
from perlin_noise import PerlinNoise
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
SCREENWIDTH = 800
SCREENHEIGHT = 800
###################################################################################
class Block(pygame.sprite.Sprite):
def __init__(self, colour, x, y):
super(Block, self).__init__()
self.image = pygame.Surface([4, 4]) # create a surface to draw onto
self.image.fill(colour) # Set a fill colour, as the R,G,B are in equal proportion this will be a grey
self.rect = self.image.get_rect()
self.rect.x = x * 4
self.rect.y = y * 4
####################################################################################
noise1 = PerlinNoise(octaves=4)
noise2 = PerlinNoise(octaves=8)
noise3 = PerlinNoise(octaves=16)
noise4 = PerlinNoise(octaves=32)
xpix, ypix = 250, 250
pic = []
for i in range(xpix):
row = []
for j in range(ypix):
noise_val = noise1([i/xpix, j/ypix])
noise_val += 0.5 * noise2([i/xpix, j/ypix])
noise_val += 0.25 * noise3([i/xpix, j/ypix])
noise_val += 0.125 * noise4([i/xpix, j/ypix])
row.append(noise_val)
pic.append(row)
#################################################################################
size = (SCREENWIDTH, SCREENHEIGHT)
window = pygame.display.set_mode((size))
screen = pygame.display.get_surface()
block_group = pygame.sprite.Group()
y = 0
for row in pic:
x = 0
for col in row:
if -0.1 > pic[y][x]:
b = Block(BLACK, x, y)
else:
b = Block(WHITE, x, y)
block_group.add(b)
x += 1
y += 1
###################################################################
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
block_group.draw(screen)
p.draw(screen)
pygame.display.flip()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
This tutorial (from ep5 to ep8) could help you (he uses a marching square algorithm to display his map but the principle is the same as you). The general idea is to detect the isolated zones, then find the points that need to be connected in order to connect two zones (for each zone, the nearest point to the other zone) and finally draw the connection. You can see the final result at the end of the ep8 to see if it looks like what you want to do.