这是我的寻路功能:

def get_distance(x1,y1,x2,y2):
    neighbors = [(-1,0),(1,0),(0,-1),(0,1)]
    old_nodes = [(square_pos[x1,y1],0)]
    new_nodes = []
    for i in range(50):
        for node in old_nodes:
            if node[0].x == x2 and node[0].y == y2:
                return node[1]
            for neighbor in neighbors:
                try:
                    square = square_pos[node[0].x+neighbor[0],node[0].y+neighbor[1]]
                    if square.lightcycle == None:
                        new_nodes.append((square,node[1]))
                except KeyError:
                    pass
        old_nodes = []
        old_nodes = list(new_nodes)
        new_nodes = []
        nodes = []
    return 50

问题是AI需要很长时间才能响应(响应时间这只是一种执行 https://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm 的 python 方式

最佳答案

你应该用 A*-search 替换你的算法,曼哈顿距离作为启发式。

关于Python - 加速寻路,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39383914/

10-12 16:24