1 回答
TA贡献1811条经验 获得超6个赞
您的代码中可能缺少最重要的步骤:
如果新点有效,则将新点列为 spawnpoint 并放入 grid 中的单元格中。
我建议将这一点添加到cellGrid是否有效:
if isValid:
cellGrid[newPointIndex[0]][newPointIndex[1]] = newPoint
points.append(newPoint)
spawnpoints.append(newPoint)
spawned = True
break
newPointIndex此外,在可以添加点之前,您必须验证具有索引的单元格是否尚未被占用:
newPointIndex = [int(newPoint.x/cellSize), int(newPoint.y/cellSize)]
if cellGrid[newPointIndex[0]][newPointIndex[1]] != None:
continue
neighbours = FindNeighbours(cellNumberX,cellNumberY,newPointIndex,cellGrid)
最后,函数存在问题FindNeighbours。range(start, stop)为 x in 创建一个范围start <= x < stop。
所以停止必须是index[0]+3而不是index[0]+2。
此外,控制 2 个嵌套for循环的范围从x-2toy+2而不是 from x-2tox+2分别从y-2to运行y+2:
for cellX in range(max(0,(index[0]-2)), min(cellNumberX,(index[1]+2))):
for cellY in range(max(0,(index[0]-2)), min(cellNumberY,(index[1]+2)))
固定功能必须是:
def FindNeighbours(cellNumberX, cellNumberY, index, cellGrid):
neighbours = []
for cellX in range(max(0, index[0]-2), min(cellNumberX, index[0]+3)):
for cellY in range(max(0, index[1]-2), min(cellNumberY, index[1]+3)):
if cellGrid[cellX][cellY] != None:
neighbours.append(cellGrid[cellX][cellY])
return neighbours
查看结果,尺寸为 300 x 300,半径为 15:
spawnpoints如果始终使用第一个点而不是随机点,则可以获得更好的结果:
# spawnIndex = random.randint(0,len(spawnpoints)-1)
spawnIndex = 0 # 0 rather than random
spawnpoint = spawnpoints[spawnIndex]
添加回答
举报