Как я могу распечатать мой BFS, чтобы найти путь в виде анимации или визуализации в Pygame? - PullRequest
0 голосов
/ 27 марта 2020

Приведен мой код поиска пути, и я хочу, чтобы он отображался последовательно. Но это показывает весь обход сразу. Этот find_path () вызывается из моего основного l oop.

def find_path():
    #Start Coordinates: StartCo()
    #End Coordinates: EndCo()
    #grid[][]:
    #       -1: start
    #       -2: end
    #       0: wall
    #       1: unvisited adjacent
    #       2: visited adjacent
    #       3: path
    queue = []
    path = False
    queue.append(START_Co)
    while len(queue) != 0:

        node = queue.pop(0)
        if node == END_Co:
            path = True
            break
        #__________________________EXPLORE_NODES
        x, y = node
        #Up, Down
        ver = [-1, +1, 0, 0]
        #Left, Right
        hor = [0, 0, +1, -1]
        for i in range(4):
            check_h = hor[i] + x
            check_v = ver[i] + y

            #__________________________manage out of bounds
            if check_v < 0 or check_h < 0:
                continue
            if check_v >= len(grid) or check_h >= len(grid[0]):
                continue
            # if WALL: skip
            if grid[check_h][check_v] == 0:
                continue
            #if visited: skip
            if grid[check_h][check_v] == 2:
                continue
            queue.append((check_h, check_v))
            grid[check_h][check_v] = 2
            pygame.draw.rect(
                screen, BLUE,
                [(GRID_MARGIN + GRID_WIDTH) * column + GRID_MARGIN,
                 (GRID_MARGIN + GRID_HEIGHT) * row + GRID_MARGIN, GRID_WIDTH,
                 GRID_HEIGHT])

            pygame.display.update()
            pygame.time.delay(50)

Мой основной код l oop выглядит следующим образом:

## _______________________________________________________________ -------- Main Program Loop ----
while not done:
    for event in pygame.event.get():  # User did something

        #CLOSE
        if event.type == pygame.QUIT:
            done = True

        #KEY PRESSED
        elif event.type == pygame.KEYDOWN:
            #Escape key
            if event.key == pygame.K_ESCAPE:
                done = True

            #Ctrl+A pressed
            elif event.key == pygame.K_a:
                mods = pygame.key.get_mods()
                #If pressed ctrl+A keep making a wall untill ctrl+Alt is pressed
                if mods & pygame.KMOD_CTRL:
                    makeWall()

            #Spacebar pressed FIND PATH FROM START TO END
            elif event.key == pygame.K_SPACE:
                if not FLAG:
                    continue
                else:
                    find_path()

        #SET_START_MARKER_ON_CLICK
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            # Change the x/y screen coordinates to grid coordinates
            column = pos[0] // (GRID_WIDTH + GRID_MARGIN)
            row = pos[1] // (GRID_HEIGHT + GRID_MARGIN)
            x = (GRID_MARGIN + GRID_WIDTH) * column + GRID_MARGIN
            y = (GRID_MARGIN + GRID_HEIGHT) * row + GRID_MARGIN

            if len(START_xy) == 0:
                START_xy = (x, y)
                START_Co = (row, column)
            else:
                grid[START_Co[0]][START_Co[1]] = 1
                START_xy = (x, y)
                START_Co = (row, column)

            grid[row][column] = -1

        #SET_END_MARKER_ON_RELEASE
        elif event.type == pygame.MOUSEBUTTONUP:
            pos = pygame.mouse.get_pos()
            # Change the x/y screen coordinates to grid coordinates
            column = pos[0] // (GRID_WIDTH + GRID_MARGIN)
            row = pos[1] // (GRID_HEIGHT + GRID_MARGIN)
            x = (GRID_MARGIN + GRID_WIDTH) * column + GRID_MARGIN
            y = (GRID_MARGIN + GRID_HEIGHT) * row + GRID_MARGIN

            if len(END_xy) == 0:
                END_xy = (x, y)
                END_Co = (row, column)
            else:
                grid[END_Co[0]][END_Co[1]] = 1
                END_xy = (x, y)
                END_Co = (row, column)

            grid[row][column] = -2
            FLAG = True

# Set the screen background
    screen.fill(BACKGROUND)

    # Draw the grid
    for row in range(GRID_ROWS):
        for column in range(GRID_COLUMNS):
            if grid[row][column] == -1:
                screen.blit(start_image, [START_xy[0], START_xy[1]])
            elif grid[row][column] == -2:
                screen.blit(end_image, [END_xy[0], END_xy[1]])
            else:
                color = WHITE
                if grid[row][column] == 0:
                    color = CLICKED
                if grid[row][column] == 2:
                    color = BLUE
                pygame.draw.rect(
                    screen, color,
                    [(GRID_MARGIN + GRID_WIDTH) * column + GRID_MARGIN,
                     (GRID_MARGIN + GRID_HEIGHT) * row + GRID_MARGIN,
                     GRID_WIDTH, GRID_HEIGHT])
# Limit to 60 frames per second
    clock.tick(20)
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()

Я хочу, чтобы мой обход обновлял шаг пошагово, а не все сразу. The start is in the center and the end is at the right corner of the blue figure

Начало находится в центре, а конец - в правом углу синей цифры

...