1 回答

TA貢獻(xiàn)1811條經(jīng)驗 獲得超6個贊
您的代碼中可能缺少最重要的步驟:
如果新點(diǎn)有效,則將新點(diǎn)列為 spawnpoint 并放入 grid 中的單元格中。
我建議將這一點(diǎn)添加到cellGrid是否有效:
if isValid:
cellGrid[newPointIndex[0]][newPointIndex[1]] = newPoint
points.append(newPoint)
spawnpoints.append(newPoint)
spawned = True
break
newPointIndex此外,在可以添加點(diǎn)之前,您必須驗證具有索引的單元格是否尚未被占用:
newPointIndex = [int(newPoint.x/cellSize), int(newPoint.y/cellSize)]
if cellGrid[newPointIndex[0]][newPointIndex[1]] != None:
continue
neighbours = FindNeighbours(cellNumberX,cellNumberY,newPointIndex,cellGrid)
最后,函數(shù)存在問題FindNeighbours。range(start, stop)為 x in 創(chuàng)建一個范圍start <= x < stop。
所以停止必須是index[0]+3而不是index[0]+2。
此外,控制 2 個嵌套for循環(huán)的范圍從x-2toy+2而不是 from x-2tox+2分別從y-2to運(yùn)行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
查看結(jié)果,尺寸為 300 x 300,半徑為 15:
spawnpoints如果始終使用第一個點(diǎn)而不是隨機(jī)點(diǎn),則可以獲得更好的結(jié)果:
# spawnIndex = random.randint(0,len(spawnpoints)-1)
spawnIndex = 0 # 0 rather than random
spawnpoint = spawnpoints[spawnIndex]
添加回答
舉報