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]
添加回答
舉報