This code implements a simple 2D maze game using the Pygame library. Here's a breakdown of what the code does and the libraries needed for it to work:
- Create a Maze: Defines a maze using a 2D array where
1represents a valid path and0represents a wall. - Set Up Game Environment:
- Initializes Pygame, sets up the display window, and defines game settings like tile size, colors, and frames per second (FPS).
- Draw Maze and Player: Functions to draw the maze and player on the screen.
- Move Player: Allows the player to move through the maze using keyboard arrow keys.
- Main Game Loop: Continuously updates the display, processes events, and handles player movement until the game is closed.
- Pygame: Used for creating the game window, handling events, and rendering graphics.
import pygame import sys
- Defines the size of each tile and the maze layout.
TILE_SIZE = 50 maze = [ [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1], ... [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] ROWS, COLS = len(maze), len(maze[0]) WIDTH, HEIGHT = COLS * TILE_SIZE, ROWS * TILE_SIZE FPS = 60
- Defines colors and initializes Pygame.
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("2D Maze Game") clock = pygame.time.Clock()
- Sets the player's starting position.
player_pos = [0, 0]
- Draws the maze and player on the screen.
def draw_maze(screen, maze): for row in range(ROWS): for col in range(COLS): color = WHITE if maze[row][col] == 1 else BLACK pygame.draw.rect(screen, color, (col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE)) pygame.draw.rect(screen, GREEN, ((COLS-1) * TILE_SIZE, (ROWS-1) * TILE_SIZE, TILE_SIZE, TILE_SIZE)) def draw_player(screen, position): pygame.draw.circle(screen, (0, 0, 255), (position[1] * TILE_SIZE + TILE_SIZE / 2, position[0] * TILE_SIZE + TILE_SIZE / 2), TILE_SIZE / 2)
- Allows the player to move in the maze if the move is valid.
def move_player(maze, player_pos, direction): x, y = player_pos if direction == "up" and x > 0 and maze[x-1][y] == 1: player_pos[0] -= 1 elif direction == "down" and x < ROWS - 1 and maze[x+1][y] == 1: player_pos[0] += 1 elif direction == "left" and y > 0 and maze[x][y-1] == 1: player_pos[1] -= 1 elif direction == "right" and y < COLS - 1 and maze[x][y+1] == 1: player_pos[1] += 1
- Handles game events, updates the display, and processes player movements until the game is exited.
running = True while running: screen.fill(BLACK) draw_maze(screen, maze) draw_player(screen, player_pos) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: move_player(maze, player_pos, "up") elif event.key == pygame.K_DOWN: move_player(maze, player_pos, "down") elif event.key == pygame.K_LEFT: move_player(maze, player_pos, "left") elif event.key == pygame.K_RIGHT: move_player(maze, player_pos, "right") pygame.display.flip() clock.tick(FPS) pygame.quit() sys.exit()
- Install Pygame with:
pip install pygame
- Run the script:
python maze_game.py
- Use the arrow keys to move the player through the maze.
- Reach the green square to win.
- Practice Pygame basics (drawing, events, game loop).
- Demonstrate grid‑based movement in a maze.
- Provide a foundation for expanding into more advanced maze or puzzle games.