Exemplo n.º 1
0
def main(maze_method, speed=0.010, mode=0):
        """
        the main program of the program
        mode 0: default mode, build the wall
        mode 1: first all cell are wall, then pull down the wall
        """
        screen = pygame.display.set_mode(resolution, 0, 32)
        clock = pygame.time.Clock()
        maze, cell_list = maze_method()

        path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1), 
                              height//grid_size, width//grid_size)
        path = path_finder.bfs_find_path()

        index = 0
        path_index = 0
        maze_finished = False

        pass_time = 0
        grid_view = GridView(screen, width, height, grid_size, grid_line_color) 
        while True:
                for event in pygame.event.get():
                        if event.type == QUIT:
                                exit()

                press_key = pygame.key.get_pressed()
                
                # press F5 to regenerate the maze
                if press_key[K_F5]:
                        index = 0
                        path_index = 0
                        maze, cell_list = maze_method()
                        path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1), 
                                height//grid_size, width//grid_size)
                        path = path_finder.bfs_find_path()
                        maze_finished = False

                #print index
                
                if mode == 0:
                    screen.fill(background_color)
                else:
                        screen.fill(cell_color)

                # draw the cell
                for i in range(index + 1):
                        if mode == 0:
                            grid_view.fill_a_cell(cell_list[i][1], cell_list[i][0], cell_color)
                        else:
                            grid_view.fill_a_cell(cell_list[i][1], cell_list[i][0], background_color)

                # draw the grid
                grid_view.draw()

                # draw the path
                if maze_finished and path:
                        for i in range(path_index + 1):
                                grid_view.fill_a_cell_with_circle(path[len(path) - i - 1][1], 
                                                                  path[len(path) - i - 1][0], path_color)

                time_passed_seconds = clock.tick() / 1000.0
                pass_time += time_passed_seconds

                if pass_time >= speed:
                        pass_time = 0
                        if maze_finished:
                                if path_index + 1 < len(path):
                                        path_index += 1

                        if index >= len(cell_list) - 1:
                                #print 'maze_finished'
                                maze_finished = True
                                
                        if index + 1 < len(cell_list):
                                index += 1

                pygame.display.update()
Exemplo n.º 2
0
def main(maze_method, speed=0.010, mode=0):
    """
        the main program of the program
        mode 0: default mode, build the wall
        mode 1: first all cell are wall, then pull down the wall
        """
    screen = pygame.display.set_mode(resolution, 0, 32)
    clock = pygame.time.Clock()
    maze, cell_list = maze_method()

    path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1),
                                 height // grid_size, width // grid_size)
    path = path_finder.bfs_find_path()

    index = 0
    path_index = 0
    maze_finished = False

    pass_time = 0
    grid_view = GridView(screen, width, height, grid_size, grid_line_color)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

        press_key = pygame.key.get_pressed()

        # press F5 to regenerate the maze
        if press_key[K_F5]:
            index = 0
            path_index = 0
            maze, cell_list = maze_method()
            path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1),
                                         height // grid_size, width // grid_size)
            path = path_finder.bfs_find_path()
            maze_finished = False

        # print index

        if mode == 0:
            screen.fill(background_color)
        else:
            screen.fill(cell_color)

        # draw the cell
        for i in range(index + 1):
            if mode == 0:
                grid_view.fill_a_cell(cell_list[i][1], cell_list[i][0], cell_color)
            else:
                grid_view.fill_a_cell(cell_list[i][1], cell_list[i][0], background_color)

        # draw the grid
        grid_view.draw()

        # draw the path
        if maze_finished and path:
            for i in range(path_index + 1):
                grid_view.fill_a_cell_with_circle(path[len(path) - i - 1][1],
                                                  path[len(path) - i - 1][0], path_color)

        time_passed_seconds = clock.tick() / 1000.0
        pass_time += time_passed_seconds

        if pass_time >= speed:
            pass_time = 0
            if maze_finished:
                if path_index + 1 < len(path):
                    path_index += 1

            if index >= len(cell_list) - 1:
                # print 'maze_finished'
                maze_finished = True

            if index + 1 < len(cell_list):
                index += 1

        pygame.display.update()
Exemplo n.º 3
0
def main(maze_method, speed=0.010, mode=0):
    pygame.init()
    check = 0
    screen = pygame.display.set_mode(resolution)
    pygame.display.set_caption("A_star_Maze")
    clock = pygame.time.Clock()
    maze, cell_list = maze_method()  # 랜덤으로 생성된 2차원 리스트 미로와 xxx 좌표가 담긴 리스트를 받는다

    path = A_star_logic.main(maze, start_point, end_point)
    index = 0
    maze_finished = False

    pass_time = 0
    grid_view = GridView(screen, width, height, grid_size, grid_line_color)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        press_key = pygame.key.get_pressed()

        # press F5 to regenerate the maze
        if press_key[pygame.K_F5]:
            index = 0
            # path_index = 0
            maze, cell_list = maze_method()
            path = A_star_logic.main(maze, start_point, end_point)
            maze_finished = False
            check = 0
        elif press_key[pygame.K_d]:
            main(maze_method=get_maze_method(DFS), mode=1)
            pass
        elif press_key[pygame.K_k]:
            main(maze_method=get_maze_method(Kruskal), mode=1)

        # print index

        if mode == 0:
            screen.fill(background_color)
        else:
            screen.fill(cell_color)

        # draw the cell
        for i in range(index + 1):
            if mode == 0:
                grid_view.fill_a_cell(
                    cell_list[i][1], cell_list[i][0], cell_color)
            else:
                grid_view.fill_a_cell(
                    cell_list[i][1], cell_list[i][0], background_color)

        # draw the grid
        grid_view.draw()

        # draw the path
        if maze_finished and path:
                for i in range(len(path)):
                    grid_view.fill_a_cell_with_circle(
                        path[i][1], path[i][0], path_color)
                    pygame.display.update()
                    sleep(0.1)
                maze_finished = False
                check = 1

        time_passed_seconds = clock.tick() / 1000.0
        pass_time += time_passed_seconds

        if pass_time >= speed:
            pass_time = 0

            if index >= len(cell_list) - 1 and check == 0:
                    maze_finished = True
                    print("랜덤으로 생성된 미로 탐색 비용은 : "+str(len(path)))

            if index + 1 < len(cell_list):
                index += 1
                pygame.display.update()