Skip to content

Abdalkarim1979/maze

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

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:

Purpose of the Code

  1. Create a Maze: Defines a maze using a 2D array where 1 represents a valid path and 0 represents a wall.
  2. Set Up Game Environment:
    • Initializes Pygame, sets up the display window, and defines game settings like tile size, colors, and frames per second (FPS).
  3. Draw Maze and Player: Functions to draw the maze and player on the screen.
  4. Move Player: Allows the player to move through the maze using keyboard arrow keys.
  5. Main Game Loop: Continuously updates the display, processes events, and handles player movement until the game is closed.

Detailed Explanation

Libraries Needed:

  • Pygame: Used for creating the game window, handling events, and rendering graphics.
    import pygame
    import sys

Game Settings and Maze Definition:

  • 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

Colors and Pygame Initialization:

  • 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()

Player Initialization:

  • Sets the player's starting position.
    player_pos = [0, 0]

Functions to Draw Maze and Player:

  • 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)

Function to Move the Player:

  • 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

Main Game Loop:

  • 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()

🚀 How to Play

  1. Install Pygame with:
    pip install pygame
  2. Run the script:
    python maze_game.py
  3. Use the arrow keys to move the player through the maze.
  4. Reach the green square to win.

🎯 Goals

  • 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.

Contact

avrmicrotech@gmail.com

About

A simple 2D maze game built with Python and Pygame.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages