程序岛生成混乱

时间:2015-10-06 00:18:02

标签: python numpy procedural-generation

因此,我一直致力于研究一种生存RTS游戏的terragen。到目前为止,我一直在使用径向岛屿生成(基本上它创造了一个有点圆形的岛状)。我发现这是无趣的,所以开发了一个分形岛发电机,它产生(我相信,无论如何)更好,更多样的岛屿形状。

以下是我使用此方法制作的几个岛屿:

enter image description here

它通过制作四边形并递归细分来工作,直到达到所需的细节水平。 要创建岛屿叠加层,我需要用白色填充此轮廓。我最初的计划就像一个油漆桶工具,用像素操作。我发现这对我来说太慢了,所以开发了一种线交叉方法。这种方法的问题在于我无法弄清楚我哪里出错了。

每当该线与形状相交时,通过反转水平线中像素的颜色来操作(或至少意味着)。因此,它填补了形状。

问题的示例图片:

enter image description here

def invertFill(shape, res):
invertMap = numpy.zeros((res,res), dtype=bool)
#loop through outline vectors, find intersect points
for n in range(len(shape)):#loop through outline vectors
    if n == len(shape) - 1:
        nPlus = 0
    else:
        nPlus = n + 1
    sta = shape[n]
    fin = shape[nPlus]
    try:
        loopRange = getRange(sta[1], fin[1])
        for y in range(loopRange[0], loopRange[1]):#loop through y values in each vector
            if inRange(sta[1], fin[1], y) == True:#if y value is between start and finish of vector, find x coord
                xIntersect = (sta[1] - y) / (sta[1] - fin[1]) * (sta[0] - fin[0]) + sta[0]#intersect ratio multiplied against x dist between start and finish, added to x of start = x intersect
                invertMap[int(xIntersect)][y] = not invertMap[int(xIntersect)][y]#if a line intersects it, invert the intersect boolean (so if two lines intersect at that pixel, it stays false, three lines true etc)
    except:
        print("sta, fin = ", sta[1], fin[1])
#loop through pixels in y direction, then x, if pixel has invert property True, invert fill colour
map = numpy.zeros((res,res), dtype=numpy.uint8)
for y in range(res):
    colour = 0
    for x in range(res):
        if invertMap[x][y] == True:
            colour = 255 - colour
        map[x][y] = colour
return(map)

任何人都有机会了解发生了什么事?

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全明白你想要怎么做,因为我不熟悉numpy,但我认为像这样简单的解决方案可以解决这个问题:

def invert_colors(map):
    inside = False
    for x in range(len(_map)):
        for y in range(len(_map[0])):
            if map[x][y].color == 255:
                inside = not inside
                continue
            map[x][y].color = 255 if inside else 0

此输入:

map = [
    [0, 0  , 0  , 0  , 0  ],
    [0, 255, 255, 255, 255],
    [0, 255, 0  , 0  , 255],
    [0, 255, 0  , 0  , 255],
    [0, 255, 255, 255, 255]
      ]

生成此输出:

  [
[0, 0  , 0  , 0  , 0  ],
[0, 255, 255, 255, 255],
[0, 255, 255, 255, 255],
[0, 255, 255, 255, 255],
[0, 255, 255, 255, 255]
  ]