我工作的一个塔防游戏在Python中,用户的塔楼可以修改的敌人必须采取的路径。为了计算路径,我认为Lee算法的实现可能是最好的和最简单的,特别是如果网格变大。我试图松散地基于我的算法。Python的迷宫路线调查
我的代码,但是,不能正常工作。我不明白为什么。
def getRoute(self):
self.routes = [[(0,0)]]
def _getRoutes():
new_routes = []
for route in self.routes:
for i in [(1,0),(-1,0),(0,1),(0,-1)]:
new_loc = [x + y for x,y in zip(route[-1],i)]
for index in new_loc:
if index < 0:
print('< 0')
continue ## if the index causes a skip across the side of the array, ignore
try:
if self.level.map[new_loc[0]][new_loc[1]].travellable: ## if the tile is able to be travelled on by enemies
route.append(new_loc) ## add the tile to the 'r' array
new_routes.append(route) ## add the new route to the new_routes array
except IndexError: ## if the tile is off the map, ignore
print('index error')
self.routes = new_routes
def _checkRoutesValidity():
for route in self.routes:
if self.level.map[route[-1][0]][route[-1][1]].access == 5:
return route
break
else:
return None
while not _checkRoutesValidity():
_getRoutes()
self.route = _checkRoutesValidity()
for i,j in self.route:
self.level.map[i][j].overlay_col = [0,1,0,1]
路线是,应该包含所有可能路径的敌人可以利用,并最终与正确路线的变量。目前,该算法应该以最快的绿色阴影路线。 Level是一个level对象,level.map是一个Grid对象的二维数组,每个对象都是一个单独的单元格。如果cell.access为5,则意味着它是敌人的出口点。
所有这一切实际发生的,是它创建的元组读取一个令人难以置信的长列表(0,1)和(1,0)。没有负数是不断产生的,没有什么以外的1或0
可有人请点我在正确的方向要么建立一个适当的李算法或修复我当前的代码?
这是一个李算法的方法? – TemporalWolf
@TemporalWolf代码尝试从一个点向外绘制路线,类似于填充洪水。它受lee算法的影响,但它并不代表它。现在不正确的问题 – JellyWX