Exemplo n.º 1
0
def run_game(filename, start=None, end=None):
    maze = Maze.load_from_file(filename)

    if start is None:
        while True:
            a, b = randint(0, maze.nrows - 1), randint(0, maze.ncolumns - 1)
            c, d = randint(0, maze.nrows - 1), randint(0, maze.ncolumns - 1)

            if (a, b) != (c, d) and not maze.is_wall(a, b) and not maze.is_wall(c, d) and \
                    len(astar_solver.solve_maze(maze, (a, b), (c, d))):
                start = (a, b)
                end = (c, d)
                break

    current = start

    # Define some colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    RED = (255, 0, 0)
    PURPLE = (186, 85, 211)
    GREEN = (127, 255, 0)
    BLUE = (30, 144, 255)

    # This sets the WIDTH and HEIGHT of each grid location
    WIDTH = 1
    HEIGHT = 1

    screen_width, screen_height = __screen_resolution()
    while (WIDTH + 2) * maze.ncolumns < screen_width and (HEIGHT + 2) * maze.nrows < screen_height:
        WIDTH += 1
        HEIGHT += 1

    # Initialize pygame
    pygame.init()

    # Set the HEIGHT and WIDTH of the screen
    WINDOW_SIZE = [WIDTH * maze.ncolumns, HEIGHT * maze.nrows]
    screen = pygame.display.set_mode(WINDOW_SIZE)

    # Set title of screen
    pygame.display.set_caption("Mazify")

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # grid of colors
    color_grid = [[WHITE for column in range(maze.ncolumns)] for row in range(maze.nrows)]

    start_time = datetime.now()

    MIN_FRAME_RATE = 10
    MAX_FRAME_RATE = 1000
    frame_rate = 60

    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                root = tk.Tk()
                root.withdraw()

                if messagebox.askyesno('Verify', 'Really quit?'):
                    done = True  # Flag that we are done so we exit this loop
                else:
                    messagebox.showinfo('No', 'Quit has been cancelled')

        keys = pygame.key.get_pressed()

        if keys[pygame.K_DOWN] or keys[pygame.K_s]:
            # print("DOWN")
            if maze.in_maze(current[0] + 1, current[1]) and not maze.is_wall(current[0] + 1, current[1]):
                current = (current[0] + 1, current[1])

        if keys[pygame.K_UP] or keys[pygame.K_w]:
            # print("UP")
            if maze.in_maze(current[0] - 1, current[1]) and not maze.is_wall(current[0] - 1, current[1]):
                current = (current[0] - 1, current[1])

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            # print("LEFT")
            if maze.in_maze(current[0], current[1] - 1) and not maze.is_wall(current[0], current[1] - 1):
                current = (current[0], current[1] - 1)

        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            # print("RIGHT")
            if maze.in_maze(current[0], current[1] + 1) and not maze.is_wall(current[0], current[1] + 1):
                current = (current[0], current[1] + 1)

        if current == end:
            root = tk.Tk()
            root.withdraw()

            end_time = datetime.now()
            messagebox.showwarning('', 'Maze solved in {}. Congratulations!'.format(
                __get_time((end_time - start_time).total_seconds())))

            if not messagebox.askyesno('', 'Do you want to play another game?'):
                break

            while True:
                x, y = randint(0, maze.nrows - 1), randint(0, maze.ncolumns - 1)

                if not maze.is_wall(x, y) and len(astar_solver.solve_maze(maze, current, (x, y))):
                    end = (x, y)
                    break

            start_time = datetime.now()
            continue

        # Set the screen background
        screen.fill(BLACK)

        # set the color_grid
        for row in range(maze.nrows):
            for column in range(maze.ncolumns):
                color = WHITE

                if maze.is_wall(row, column):
                    color = BLACK

                if (row, column) == end:
                    color = BLUE

                if (row, column) == current:
                    color = PURPLE

                color_grid[row][column] = color

        if keys[pygame.K_t]:
            # print("solution")
            path = astar_solver.solve_maze(maze, current, end)

            for (x, y) in path:
                if (x, y) != current and (x, y) != end:
                    color_grid[x][y] = RED

        # Draw the grid
        for row in range(maze.nrows):
            for column in range(maze.ncolumns):
                pygame.draw.rect(screen, color_grid[row][column], [WIDTH * column, HEIGHT * row, WIDTH, HEIGHT])

        if keys[pygame.K_z]:
            pygame.draw.circle(screen, PURPLE, (WIDTH * current[1], HEIGHT * current[0]), 25)

        if keys[pygame.K_x]:
            pygame.draw.circle(screen, BLUE, (WIDTH * end[1], HEIGHT * end[0]), 25)

        if keys[pygame.K_c]:
            frame_rate -= 1
            frame_rate = max(frame_rate, MIN_FRAME_RATE)

        if keys[pygame.K_v]:
            frame_rate += 1
            frame_rate = min(frame_rate, MAX_FRAME_RATE)

        if keys[pygame.K_f]:
            print("Frame rate:", frame_rate)

        # Limit to frame_rate frames per second
        clock.tick(frame_rate)

        # 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()
Exemplo n.º 2
0
if sys.argv[1].startswith('create'):
    nrows = int(get_param('rows'))
    ncols = int(get_param('columns'))
    filepath = get_param('file-path')

    maze = Maze.create_maze(nrows, ncols)
    draw_maze(maze, file_path=filepath)


elif sys.argv[1].startswith('solve'):
    start = make_tuple(get_param('start'))
    end = make_tuple(get_param('end'))

    maze = Maze.load_from_file(get_param('file-path'))
    path = astar_solver.solve_maze(maze, start, end)
    print(path)
    #draw_maze(maze, path)
elif sys.argv[1].startswith('play'):
    game.run_game(get_param('file-path'))
elif sys.argv[1] == 'help':
    print('Key controls')
    print('z    Highlight current location')
    print('x    Highlight destination')
    print('t    Show solution from current location')
    print('c    Decrease frame rate')
    print('v    Increase frame rate')
    print('f    Show frame rate')
    print('Arrows or WASD for movement')

Exemplo n.º 3
0
from solvers import astar_solver
from maze import Maze
from draw_maze import draw_maze

maze = Maze.load_from_file('mazes/small.txt')
path = astar_solver.solve_maze(maze, (1, 1), (7, 6))
prev = (1, 1)
for x in path:
    #print(x[0] , x[1]) # y then x
    if x[0] > prev[0]:
        print("down")
    elif x[0] < prev[0]:
        print("up")
    if x[1] > prev[1]:
        print("right")
    elif x[1] < prev[1]:
        print("left")
    if x[0] == prev[0] and x[1] == prev[1]:
        print("stay")
    prev = x
#draw_maze(maze, path)
Exemplo n.º 4
0
from solvers import astar_solver
from maze import Maze
#from draw_maze import draw_maze
from maze import Maze
import urllib.request

ip = "http://127.0.0.1:5000"
maze = Maze.load_from_file('mazes/small.txt')
start = (1, 1)
target = (7, 6)
path = astar_solver.solve_maze(maze, start, target)
prev = (1, 1)
for x in path:
    #print(x[0] , x[1]) # y then x
    if x[0] > prev[0]:
        print("down")
        #urllib.request.urlopen(ip+"/down")
    elif x[0] < prev[0]:
        print("up")
        #urllib.request.urlopen(ip+"/up")
    if x[1] > prev[1]:
        print("right")
        #urllib.request.urlopen(ip+"/right")
    elif x[1] < prev[1]:
        print("left")
        #urllib.request.urlopen(ip+"/left")
    if x[0] == prev[0] and x[1] == prev[1]:
        print("stay")
    prev = x
#draw_maze(maze, path)