相应地更新显示以每帧产生函数

Update display accordingly to yield function each frame

我正在尝试在我的算法上使用 yield 函数来实现寻路算法可视化工具,该函数在我的函数结束时每次访问一个节点时都会生成已访问节点的列表:

# Breadth First Search Algorithm
def bfs(graph, start, goal):
    explored = []

    # Queue for traversing the
    # graph in the BFS
    queue = [[start]]

    # If the desired node is
    # reached
    if start == goal:
        return

    # Loop to traverse the graph
    # with the help of the queue

    while queue:
        path = queue.pop(0)
        node = path[-1]
        y, x = node
        # Codition to check if the
        # current node is not visited

        if node not in explored and nodes_rows[x][y].color is not BLACK:

            neighbours = graph[node]

            # Loop to iterate over the
            # neighbours of the node
            for neighbour in neighbours:
                new_path = list(path)
                new_path.append(neighbour)
                queue.append(new_path)

                # Condition to check if the
                # neighbour node is the goal
                if neighbour == goal:
                    new_path.remove(start)
                    new_path.remove(goal)
                    return new_path

            explored.append(node)
            yield explored
    return None

nodes_rows[x][y].color is not BLACK - 避开黑色的墙壁

我有一个主循环,当按下回车按钮时,算法可视化:

if event.key == pygame.K_RETURN:
    algorithm = bfs(neihgbours, (start[0]), (end[0]))
    ticks = None        

    try:
        while True:
            if not ticks or pygame.time.get_ticks() - ticks >= 500:
                ticks = pygame.time.get_ticks()
                nodes = next(algorithm)
                nodes_rows[nodes[-1][1]][nodes[-1][0]].color = LIGHT_BLUE
                pygame.display.update()
    except StopIteration:
        pass

nodes_rows[nodes[-1][1]][nodes[-1][0]].color = LIGHT_BLUE 这个令人困惑的部分基本上是取列表中最后一个元素的行 nodes[-1][1] 然后是列表中最后一个元素的列 nodes[-1][0] 然后在nodes_rows 矩阵它相应地改变节点颜色,列表由(行,列)元组组成,以便更好地说明。为了更好地表示,如果我打印 nodes = next(algorithm) 输出将如下所示:

[(7, 6)]
[(7, 6), (8, 6)]
[(7, 6), (8, 6), (6, 6)]
[(7, 6), (8, 6), (6, 6), (7, 5)]
[(7, 6), (8, 6), (6, 6), (7, 5), (7, 7)]
[(7, 6), (8, 6), (6, 6), (7, 5), (7, 7), (9, 6)]
...

我试图让算法每半秒为网格上的节点着色一个节点,如果我打印它,它确实对产量函数起作用,它实际上确实每 0.5 个节点添加一个节点到访问的节点列表,但出于某种原因,当我尝试更新屏幕并为它们着色时,它只是一次等待所有刻度和颜色的总量,而不是更多的动画外观。我如何更新显示,以便在算法函数结束时使用 yield 逐个节点而不是每个访问的节点着色?

为了更好地理解这里是一次完成所有节点着色后网格的样子:

视频表示算法过程:

https://cdn.discordapp.com/attachments/772816508015083552/832303260911272046/PowerPoint_-_1_2021-04-15_20-13-35_Trim.mp4

您的事件循环或主游戏循环中不应该有 while 循环。使用标志来表示是否更新算法:


algorithm = None
update_timer = 0
clock = pygame.time.Clock()

while running:
    dt = clock.tick()
    for event in pygame.event.get():
        # ... Other event handling
        if event.key == pygame.K_RETURN:
            algorithm = bfs(neihgbours, (start[0]), (end[0]))
            update_timer = 500 # To make sure we update first time

    update_timer += dt
    if algorithm and update_timer > 500:
        update_timer -= 500
        try:
            nodes = next(algorithm)
        except StopIteration:
            algorithm = None
        else:
            nodes_rows[nodes[-1][1]][nodes[-1][0]].color = LIGHT_BLUE
     
     # The actual rendering should happen here.