In this game, you use WASD or arrow keys to move around and try to complete the maze, once you are over the red block at the end, click S or down arrow to beat the maze.
You can click here for a demonstration.
In order to play the game, copy the code below and paste it into your python file. Run the game and enjoy!
Copy the code for FREE!:
import pygame import sys import time # Initialize Pygame pygame.init() # Set up display width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Maze Navigation Game") # Colors white = (255, 255, 255) black = (0, 0, 0) green = (0, 255, 0) red = (255, 0, 0) font = pygame.font.Font(None, 72) # Larger font size for "You Win!" # Game variables player_size = 50 player_x = 50 player_y = 50 player_speed = 5 goal_size = 50 goal_x = width - 100 goal_y = height - 100 maze = [ "####################", "#S # #", "# # ### ### ## ### #", "# # # # # # #", "# ### # ### ## ## ##", "# # # # #", "### # ### # ###### #", "# # # #", "##### ########## ###", "#G #", "####################", ] clock = pygame.time.Clock() def draw_player(x, y): pygame.draw.rect(screen, green, [x, y, player_size, player_size]) def draw_goal(x, y): pygame.draw.rect(screen, red, [x, y, goal_size, goal_size]) def draw_maze(): for i in range(len(maze)): for j in range(len(maze[i])): if maze[i][j] == "#": pygame.draw.rect(screen, white, [j * player_size, i * player_size, player_size, player_size]) def display_message(message): text = font.render(message, True, red) # Render in red color screen.blit(text, (width // 2 - text.get_width() // 2, height // 2 - text.get_height() // 2)) pygame.display.flip() # Game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() # Store the potential new position new_x = player_x new_y = player_y if keys[pygame.K_LEFT] or keys[pygame.K_a]: new_x -= player_speed if keys[pygame.K_RIGHT] or keys[pygame.K_d]: new_x += player_speed if keys[pygame.K_UP] or keys[pygame.K_w]: new_y -= player_speed if keys[pygame.K_DOWN] or keys[pygame.K_s]: new_y += player_speed # Check for collisions with white blocks collision = any( maze[i // player_size][j // player_size] == "#" for i in range(new_y, new_y + player_size) for j in range(new_x, new_x + player_size) if 0 <= i // player_size < len(maze) and 0 <= j // player_size < len(maze[0]) ) # Check for collisions with red color player_rect = pygame.Rect(new_x, new_y, player_size, player_size) red_rect = pygame.Rect(goal_x, goal_y, goal_size, goal_size) red_collision = player_rect.colliderect(red_rect) # Update the player's position only if there's no collision with white blocks if not collision: player_x = new_x player_y = new_y # Draw everything screen.fill(black) draw_maze() draw_goal(goal_x, goal_y) draw_player(player_x, player_y) # Check if the player is on red color if red_collision: display_message("You Win!") pygame.display.flip() # Ensure the message is displayed time.sleep(2) # Wait for 2 seconds pygame.quit() sys.exit() pygame.display.flip() clock.tick(60)