Pi Chaser - a Python and Pygame Maze Chase Game

by TechCrafter in Design > Game Design

1137 Views, 4 Favorites, 0 Comments

Pi Chaser - a Python and Pygame Maze Chase Game

The number π (pi) has fascinated mathematicians, scientists, and thinkers for thousands of

years. It appears everywhere — from the circles we draw to the waves we surf, from the planets orbiting stars to the code running on our screens. But for this contest, π isn't just a mathematical constant. It's a challenge. A reminder that the simplest concepts can spiral into infinite complexity — like a maze.


And what better way to celebrate π than by building a maze game from scratch using Python and Pygame?


A fully functional maze chase game where you control a player through a custom designed Pi-shaped labyrinth, collecting points before time runs out. Every wall placed by hand. Every point location chosen strategically.


In this Instructable, I'll walk you through every single step of building this game yourself. From installing Python and Pygame to designing your own maze, from player movement to collision detection, from scoring systems to game over screen.

Supplies

Software Requirements


Python 3.x — python.org/downloads

Pygame — Install via pip after Python is installed

Code Editor '— VS Code, PyCharm, IDLE (comes with Python), or even Notepad

Terminal/Command Prompt — Built into your operating system


Hardware Requirements


Computer — Any machine that runs Python

Internet — For downloading Python and Pygame Not required after installation

Installing Python

If you already have Python installed, you can skip to Step 2. But if you're not sure, let's check

first.

1.1 Check if Python is Already Installed

On Windows:

1. Press Windows Key + R to open the Run dialog

2. Type cmd and press Enter

3. In the black window that appears, type: python --version

4. Press Enter


On Mac/Linux:

1. Open Terminal

2. Type: python3 --version

3. Press Enter


What you'll see:

· If Python is installed, you'll see something like: Python 3.9.7

· If you see an error message, Python is not installed — proceed to installation


1.2 Download Python

1. Open your web browser and go to: python.org/downloads

2. You'll see a big yellow button that says "Download Python" — click it

3. The website will automatically suggest the correct version for your operating system


1.3 Install Python

On Windows:

1. Run the downloaded installer (.exe file)

2. IMPORTANT: Check the box that says "Add Python to PATH" at the bottom of the installer

window

3. Click "Install Now"

4. Wait for the installation to complete

5. Click "Close"


On Mac:

1. Run the downloaded installer (.pkg file)

2. Follow the installation wizard

3. Click "Continue" and agree to the license

4. Click "Install"

5. Enter your password when prompted

6. Click "Close" when finished


On Linux:

Python usually comes pre-installed. If not, use your package manager:

```bash

sudo apt update

sudo apt install python3 python3-pip

```

1.4 Verify Installation

Open a new terminal/command prompt and run:

```bash

python --version

```

Or on Mac/Linux:

```bash

python3 --version

```

You should see the version number you just installed.

Python is now ready!

Installing Pygame

Python alone can't make games — it needs libraries (collections of pre-written code) to handle graphics, sounds, and user input. Pygame is the library we'll use.


2.1 Open Terminal/Command Prompt

On Windows:

· Press Windows Key + R

· Type cmd

· Press Enter

On Mac/Linux:

· Open Terminal from Applications or search for it

2.2 Install Pygame Using pip

In the terminal, type this command:

On Windows:

```bash

pip install pygame

```

On Mac/Linux:

```bash

pip3 install pygame

```

2.3 What Happens During Installation

You'll see text scrolling up the screen as pip:

· Downloads pygame from the Python Package Index ```

Successfully installed pygame-2.5.2

```

2.4 Test That Pygame Installed Correctly

Still in the terminal, type:

On Windows:

```bash

python -c "import pygame; print(pygame.version.ver)"

```

On Mac/Linux:

```bash

python3 -c "import pygame; print(pygame.version.ver)"

```

You should see the pygame version number printed.

✅

Pygame is now ready!

Setting Up Your Workspace

Before we start coding, let's organize our project so everything stays tidy.

3.1 Create a Project Folder

1. Navigate to where you want your project to live (Desktop, Documents, etc.) I'll show you how to set up in VS Code (free and popular), but you can use any editor.


If you don't have VS Code:

1. Go to: code.visualstudio.com/download

2. Download the version for your operating system

3. Install it (default settings are fine)

Open VS Code:

1. Launch VS Code

2. Click File > Open Folder

3. Navigate to and select your Pi_Maze_Game folder

4. Click "Open"


3.3 Create Your Python File

1. In VS Code, hover over the folder name in the left sidebar

2. Click the New File icon (looks like a sheet of paper with a plus)

3. Name it: maze_game.py

4. Press Enter


3.4 Open the Terminal in VS Code (Optional but Helpful)

1. In VS Code, go to Terminal > New Terminal

2. A terminal will open at the bottom of your screen, already inside your project folder

This lets you run your game without switching windows.

✅

Your workspace is ready!

Importing Libraries and Setting Up Game Variables

Now we start coding. Open your maze_game.py file and let's begin.

4.1 The Imports

At the very top of your file, type:

```python

# Import the libraries we need

import pygame

import sys

import random

```

What each import does:

Library Purpose

pygame: Handles graphics, sound, keyboard input, timing

sys: Lets us properly exit the game when needed

random: Helps us pick random point locations

4.2 Initialize Pygame

Right after the imports, add:

```python

# Initialize pygame

pygame.init()

```

This line tells pygame to prepare all its modules for use. Without it, nothing will work.

4.3 Set Up Game Constants

Constants are values that don't change during the game. We put them at the top so they're easy

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

# Score starting value (as string for easy display)

score = '0'

# Create the game window

screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Set up font for displaying text

font = pygame.font.Font(None, 36)

```

Understanding colors:

RGB values range from 0 to 255:

· (255,255,255) = maximum of all colors = white

· (0,0,0) = no colors = black

· (255,0,0) = only red = red

· (0,255,0) = only green = green

· (0,0,255) = only blue = blue


What each variable does:

Variable Purpose

WIDTH, HEIGHT: Size of game window in pixels

WHITE, BLACK, etc.: Colors we'll use for drawing

score: Keeps track of points collected (starts at '0')

screen: The game window itself

font: How we'll display text (score, timer, messages)

✅

Setup complete! Your game foundation is ready

Designing the Maze Wall

This is where your game becomes unique. Instead of using an image or random generation, we're going to build the maze manually — wall by wall, pixel by pixel.

5.1 Understanding Pygame Rectangles

In pygame, a rectangle (Rect) is defined by:

· x position (how far from the left edge)

· y position (how far from the top edge)

· width (how wide)

· height (how tall)

For example:

```python

pygame.Rect(213, 45, 427, 5)

```

Creates a rectangle starting at x=213, y=45, that's 427 pixels wide and 5 pixels tall — a horizontal

wall.

5.2 Creating the Wall List

After your setup code, add:

```python

# Maze walls - each wall is a rectangle

maze_wall = [

pygame.Rect(213, 45, 427, 5),

pygame.Rect(213, 45, 5, 25),

pygame.Rect(158, 65, 55, 5),

pygame.Rect(158, 65, 5, 26),

pygame.Rect(133, 87, 55, 5),

pygame.Rect(133, 87, 5, 70),

pygame.Rect(108, 109, 25, 5),

pygame.Rect(108, 109, 5, 25),

pygame.Rect(80, 129, 30, 5),

pygame.Rect(80, 129, 5, 45),

pygame.Rect(55, 152, 30, 5),

pygame.Rect(55, 152, 5, 67),

pygame.Rect(55, 214, 25, 5),

pygame.Rect(80, 196, 5, 22),

pygame.Rect(80, 196, 30, 5),

pygame.Rect(108, 156, 5, 45),

pygame.Rect(108, 174, 55, 5),

pygame.Rect(158, 112, 5, 67),

pygame.Rect(163, 151, 50, 5),

pygame.Rect(213, 129, 5, 175),

pygame.Rect(213, 238, 55, 5),

pygame.Rect(213, 302, 30, 5),

pygame.Rect(188, 282, 30, 5),

pygame.Rect(185, 282, 5, 45),

pygame.Rect(160, 322, 30, 5),

pygame.Rect(160, 322, 5, 45),

pygame.Rect(160, 367, 80, 5),

pygame.Rect(240, 345, 5, 45),

pygame.Rect(133, 345, 30, 5),

pygame.Rect(133, 345, 5, 65),

pygame.Rect(133, 408, 55, 5),

pygame.Rect(106, 385, 30, 5),

pygame.Rect(106, 385, 5, 85),

pygame.Rect(106, 450, 30, 5),

pygame.Rect(106, 470, 85, 5),

pygame.Rect(185, 450, 5, 25),

pygame.Rect(263, 45, 5, 25),

pygame.Rect(243, 68, 25, 5),

pygame.Rect(238, 68, 5, 130),

pygame.Rect(185, 108, 55, 5),

pygame.Rect(185, 108, 5, 25),

pygame.Rect(210, 83, 5, 25),

pygame.Rect(243, 133, 25, 5),

pygame.Rect(243, 173, 25, 5),

pygame.Rect(478, 45, 5, 45),

pygame.Rect(453, 90, 100, 5),

pygame.Rect(453, 90, 5, 25),

pygame.Rect(503, 65, 5, 25),

pygame.Rect(553, 90, 5, 25),

pygame.Rect(553, 110, 60, 5),

pygame.Rect(608, 90, 5, 25),

pygame.Rect(528, 70, 110, 5),

pygame.Rect(583, 70, 5, 25),

pygame.Rect(633, 45, 5, 90),

pygame.Rect(483, 130, 155, 5),

pygame.Rect(478, 110, 5, 280),

pygame.Rect(478, 110, 60, 5),

pygame.Rect(398, 150, 80, 5),

pygame.Rect(398, 150, 5, 30),

pygame.Rect(423, 70, 5, 80),

pygame.Rect(398, 68, 60, 5),

pygame.Rect(398, 108, 30, 5),

pygame.Rect(428, 133, 30, 5),

pygame.Rect(453, 258, 30, 5),

pygame.Rect(450, 198, 5, 65),

pygame.Rect(425, 215, 30, 5),

pygame.Rect(478, 388, 80, 5),

pygame.Rect(558, 388, 5, 25),

pygame.Rect(528, 366, 5, 25),

pygame.Rect(528, 366, 85, 5),

pygame.Rect(610, 345, 5, 25),

pygame.Rect(555, 325, 5, 45),

pygame.Rect(555, 325, 30, 5),

pygame.Rect(583, 305, 5, 45),

pygame.Rect(583, 303, 30, 5),

pygame.Rect(610, 303, 5, 23),

pygame.Rect(610, 325, 30, 5),

pygame.Rect(635, 325, 5, 65),

pygame.Rect(610, 385, 30, 5),

pygame.Rect(610, 385, 5, 48),

pygame.Rect(583, 385, 5, 70),

pygame.Rect(583, 430, 32, 5),

pygame.Rect(558, 450, 25, 5),

pygame.Rect(558, 450, 5, 25),

pygame.Rect(425, 470, 133, 5),

pygame.Rect(531, 431, 5, 40),

pygame.Rect(477, 430, 82, 5),

pygame.Rect(477, 410, 5, 25),

pygame.Rect(451, 408, 85, 5),

pygame.Rect(423, 452, 5, 24),

pygame.Rect(398, 452, 28, 5),

pygame.Rect(399, 430, 5, 26),

pygame.Rect(371, 430, 28, 5),

pygame.Rect(371, 324, 5, 108),

pygame.Rect(371, 409, 30, 5),

pygame.Rect(291, 109, 5, 215),

pygame.Rect(264, 153, 30, 5),

pygame.Rect(264, 196, 30, 5),

pygame.Rect(264, 196, 5, 23),

pygame.Rect(240, 217, 28, 5),

pygame.Rect(240, 260, 56, 5),

pygame.Rect(240, 260, 5, 23),

pygame.Rect(240, 282, 28, 5),

pygame.Rect(263, 282, 5, 25),

pygame.Rect(213, 324, 82, 5),

pygame.Rect(213, 324, 5, 23),

pygame.Rect(186, 345, 32, 5),

pygame.Rect(265, 324, 5, 87),

pygame.Rect(212, 409, 56, 5),

pygame.Rect(212, 388, 5, 25),

pygame.Rect(158, 388, 56, 5),

pygame.Rect(239, 409, 5, 23),

pygame.Rect(133, 430, 110, 5),

pygame.Rect(159, 430, 5, 25),

pygame.Rect(210, 430, 5, 25),

pygame.Rect(296, 132, 104, 5),

pygame.Rect(345, 89, 5, 45),

pygame.Rect(371, 68, 5, 236),

pygame.Rect(371, 89, 33, 5),

pygame.Rect(292, 68, 84, 5),

pygame.Rect(292, 68, 5, 23),

pygame.Rect(265, 89, 56, 5),

pygame.Rect(265, 89, 5, 23),

pygame.Rect(318, 89, 5, 23),

pygame.Rect(376, 281, 25, 5),

pygame.Rect(371, 302, 57, 5),

pygame.Rect(424, 302, 5, 25),

pygame.Rect(424, 324, 30, 5),

pygame.Rect(451, 281, 5, 110),

pygame.Rect(424, 281, 30, 5),

pygame.Rect(424, 238, 5, 45),

pygame.Rect(398, 238, 30, 5),

pygame.Rect(398, 196, 5, 70),

pygame.Rect(398, 196, 30, 5),

pygame.Rect(424, 174, 30, 5),

pygame.Rect(424, 174, 5, 25),

pygame.Rect(398, 323, 5, 68),

pygame.Rect(403, 345, 25, 5),

pygame.Rect(424, 366, 30, 5),

pygame.Rect(398, 388, 58, 5),

pygame.Rect(424, 393, 5, 40),

pygame.Rect(424, 430, 31, 5),

pygame.Rect(451, 430, 5, 25),

pygame.Rect(451, 452, 56, 5),

]

```

5.3 Understanding the Maze Design

This maze was designed manually by placing walls strategically to create:

· Corridors that force the player to navigate carefully

· Dead ends that trap players who aren't paying attention

· Open areas where multiple paths converge

· Choke points where players must make quick decisions

Each wall was tested by actually playing the game and adjusting positions until the flow felt right.


5.4 How to Design Your Own Maze Walls

If you want to create your own maze:

1. Draw it on paper first — sketch the layout you want

2. Think in rectangles — every wall is just a rectangle

3. Start with boundaries — make sure the player can't leave the play area

4. Add internal walls — build the maze structure

5. Test and adjust — playtest and move walls that don't feel right

A helpful technique: open a blank pygame window and use mouse clicks to print coordinates,

then use those numbers to build walls.

✅

Maze walls are complete!

Placing the Collectible Points

Now we need to decide where the blue points (the items the player collects) will appear.


6.1 Understanding Point Coordinates

Each point is just a tiny rectangle (6×6 pixels) that appears at specific locations. The player

collects them by touching them.


6.2 Creating the Coordinates List

Add this after your maze walls:

```python

# Point spawn coordinates

coordinates = [

[118, 136], [64, 162], [144, 98], [248, 54], [222, 92],

[222, 118], [222, 223], [249, 290], [275, 269], [171, 332],

[223, 350], [250, 365], [208, 374], [144, 356], [196, 398],

[221, 417], [143, 439], [196, 440], [118, 458], [382, 312],

[435, 374], [409, 356], [436, 333], [380, 395], [409, 438],

[512, 442], [512, 416], [549, 375], [594, 314], [542, 455],

[435, 266], [408, 206], [461, 161], [408, 248], [140, 95],

[408, 135], [407, 78], [361, 54], [276, 72], [302, 99],

[328, 77], [356, 118], [463, 73], [433, 118], [414, 118],

[595, 78], [513, 75], [488, 76], [619, 54], [463, 242],

]

```

6.3 Why These Locations Matter

Each coordinate was chosen to:

· Be reachable through the maze (not inside walls)

· Create interesting choices (do I go left for that point or right?)

· Be visible from multiple paths

· Keep the player moving through the maze


6.4 How to Choose Your Own Point Locations

1. Walk through your maze mentally — where would you want to go?

2. Place points at intersections — gives players choices

3. Place points at dead ends — rewards exploration

4. Place points along main paths — keeps the game flowing

5. Count them — 50 is a good number for variety

✅

Point locations are ready!

Creating the Main Game Function

Now we get to the heart of the game — the play() function that contains all the gameplay logic.

7.1 Why Use a Function?

Putting the game in a function lets us:

· Restart easily by calling the function again

· Keep variables isolated between games

· Structure code cleanly

7.2 Start the Function

Add this after your coordinates:

```python

# Main game function

def play():

# We'll put all game code here

```

Everything from now until the end of Step 11 will be inside this function (indented).

Setting Up Game Variables (Inside Play())

Inside the play() function, we need to set up the player, points, and timer.


```python

global score

score = '0'

```

The global keyword tells Python we're using the score variable defined earlier, not creating a new

one.

8.2 Create the Player

```python

player = pygame.Rect(140, 95, 10, 10)

player_speed = 2

```

· Player starts at position (140, 95)

· Size: 10×10 pixels

· Speed: 2 pixels per frame

8.3 Select the First Point

```python

random_index = int(random.random() * 50)

selected_coordinate = coordinates[random_index]

point = pygame.Rect(selected_coordinate[0], selected_coordinate[1], 6, 6)

```

· Pick a random index from 0 to 49

· Get the coordinates at that index

· Create a 6×6 rectangle at that position

8.4 Set the Timer

```python

time = 31

```

Start with 31 seconds on the clock. 8.5 Create the Score Counter Function (Inside play())

This nested function handles increasing the score:

```python

def score_counter():

global score

remainder = int(score)

score = f'{remainder + 1}'

```

When called, it:

· Converts current score from string to integer

· Adds 1

· Converts back to string

✅

Game variables are set!

The Main Game Loop (Inside Play())

This is where the magic happens — the loop that runs 60 times per second, updating the game.

9.1 Start the Loop

```python

while True:

```

This runs forever (until the game ends or player quits).

9.2 Timer Countdown

```python

time -= 1/60

```

At 60 frames per second, subtracting 1/60 each frame equals 1 second of real time. 9.3 Check if Time Ran Out

```python

if int(time) <= 0:

state = False

else:

state = True

```

state controls whether the game is active (True) or game over (False).

9.4 Handle Events (Quit)

```python

if state:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

```

This lets the player close the window properly.

9.5 Get Keyboard Input

```python

keys = pygame.key.get_pressed()

```

This captures all currently pressed keys.

9.6 Player Movement

```python

if keys[pygame.K_UP] and player.y >= 0:

player.y -= player_speed

if keys[pygame.K_DOWN] and player.y <= 490:

player.y += player_speed

if keys[pygame.K_LEFT] and player.x >= 0:

player.x -= player_speed

if keys[pygame.K_RIGHT] and player.x <= 690: player.x += player_speed

```

Each arrow key moves the player in that direction, but prevents moving off-screen.


9.7 Collision Detection

This is the most important part — making walls solid.

```python

for wall in maze_wall:

if player.colliderect(wall):

if keys[pygame.K_UP]:

player.y += player_speed

if keys[pygame.K_DOWN]:

player.y -= player_speed

if keys[pygame.K_LEFT]:

player.x += player_speed

if keys[pygame.K_RIGHT]:

player.x -= player_speed

```

How it works:

1. Check if player overlaps any wall

2. If yes, undo the last movement based on which key was pressed

3. This creates the illusion of a solid wall

9.8 Point Collection

```python

if player.colliderect(point):

score_counter()

random_index = int(random.random() * 50)

selected_coordinate = coordinates[random_index]

point.x, point.y = selected_coordinate

# Add time bonus if time < 5:

time += 10

else:

time += 5

```

When player touches the point:

· Increase score

· Pick a new random location

· Move the point there

· Add time bonus (more if time is low — helps struggling players)

9.9 Drawing Everything

```python

screen.fill(WHITE)

# Draw walls

for wall in maze_wall:

pygame.draw.rect(screen, BLACK, wall)

# Draw player

pygame.draw.rect(screen, RED, player)

# Draw point

pygame.draw.rect(screen, BLUE, point)

# Draw timer

time_text = font.render(f"Time: {int(time)}s", True, GREEN)

screen.blit(time_text, (10, 10))

# Draw score

score_text = font.render(f"Score:{score}", True, GREEN)

screen.blit(score_text, (WIDTH - 210, 10))

pygame.display.flip()

```

Each draw.rect adds shapes to the screen. display.flip() shows them all at once. 9.10 Control Frame Rate

```python

pygame.time.Clock().tick(60)

```

This limits the loop to 60 frames per second — fast enough for smooth movement, slow enough

to be controllable.

✅

Game loop complete!

Game Over Screen (Inside Play())

When time runs out, we need a different screen.

10.1 The Game Over Section

```python

else:

# Handle quit during game over

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

# Check for restart

keys = pygame.key.get_pressed()

if keys[pygame.K_RETURN]:

play() # Restart the game

# Draw game over screen

screen.fill(BLACK)

message = font.render('GAME OVER', True, RED)

screen.blit(message, (250, 200))

message1 = font.render('Press "Enter" to play again', True, WHITE) pygame.display.flip()

pygame.time.Clock().tick(60)

```

10.2 What Happens Here

1. Still check for quit — player might want to exit

2. Watch for Enter key — if pressed, restart the game

3. Draw black screen — creates contrast with gameplay

4. Show messages — tell player what happened and what to do

5. Maintain frame rate — even game over screens need timing

✅

Game Over screen ready!

Start the Game

After the play() function definition, add this line:

```python

play()

```

This actually calls the function and starts the game.

Complete Code

Here's the entire game in one block, ready to copy and run:

```python

# imports

import pygame

import sys

import random

# Initialize pygame

pygame.init()

# variables

WIDTH, HEIGHT = 700, 500

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

score = '0'

screen = pygame.display.set_mode((WIDTH, HEIGHT))

font = pygame.font.Font(None, 36)

# maze wall

maze_wall = [

pygame.Rect(213, 45, 427, 5),

pygame.Rect(213, 45, 5, 25),

pygame.Rect(158, 65, 55, 5),

pygame.Rect(158, 65, 5, 26),

pygame.Rect(133, 87, 55, 5),

pygame.Rect(133, 87, 5, 70),

pygame.Rect(108, 109, 25, 5),

pygame.Rect(108, 109, 5, 25),

pygame.Rect(80, 129, 30, 5),

pygame.Rect(80, 129, 5, 45),

pygame.Rect(55, 152, 30, 5),

pygame.Rect(55, 152, 5, 67),

pygame.Rect(55, 214, 25, 5),

pygame.Rect(80, 196, 5, 22),

pygame.Rect(80, 196, 30, 5),

pygame.Rect(108, 156, 5, 45),

pygame.Rect(108, 174, 55, 5),

pygame.Rect(158, 112, 5, 67),

pygame.Rect(163, 151, 50, 5),

pygame.Rect(213, 129, 5, 175),

pygame.Rect(213, 238, 55, 5),

pygame.Rect(213, 302, 30, 5),

pygame.Rect(188, 282, 30, 5),

pygame.Rect(185, 282, 5, 45),

pygame.Rect(160, 322, 30, 5),

pygame.Rect(160, 322, 5, 45),

pygame.Rect(160, 367, 80, 5),

pygame.Rect(240, 345, 5, 45),

pygame.Rect(133, 345, 30, 5),

pygame.Rect(133, 345, 5, 65),

pygame.Rect(133, 408, 55, 5),

pygame.Rect(106, 385, 30, 5),

pygame.Rect(106, 385, 5, 85),

pygame.Rect(106, 450, 30, 5),

pygame.Rect(106, 470, 85, 5),

pygame.Rect(185, 450, 5, 25),

pygame.Rect(263, 45, 5, 25),

pygame.Rect(243, 68, 25, 5),

pygame.Rect(238, 68, 5, 130),

pygame.Rect(185, 108, 55, 5),

pygame.Rect(185, 108, 5, 25),

pygame.Rect(210, 83, 5, 25),

pygame.Rect(243, 133, 25, 5),

pygame.Rect(243, 173, 25, 5),

pygame.Rect(478, 45, 5, 45),

pygame.Rect(453, 90, 100, 5),

pygame.Rect(453, 90, 5, 25),

pygame.Rect(503, 65, 5, 25),

pygame.Rect(553, 90, 5, 25),

pygame.Rect(553, 110, 60, 5),

pygame.Rect(608, 90, 5, 25),

pygame.Rect(528, 70, 110, 5),

pygame.Rect(583, 70, 5, 25),

pygame.Rect(633, 45, 5, 90),

pygame.Rect(483, 130, 155, 5),

pygame.Rect(478, 110, 5, 280),

pygame.Rect(478, 110, 60, 5),

pygame.Rect(398, 150, 80, 5),

pygame.Rect(398, 150, 5, 30),

pygame.Rect(423, 70, 5, 80),

pygame.Rect(398, 68, 60, 5),

pygame.Rect(398, 108, 30, 5),

pygame.Rect(428, 133, 30, 5),

pygame.Rect(453, 258, 30, 5),

pygame.Rect(450, 198, 5, 65),

pygame.Rect(425, 215, 30, 5),

pygame.Rect(478, 388, 80, 5),

pygame.Rect(558, 388, 5, 25),

pygame.Rect(528, 366, 5, 25),

pygame.Rect(528, 366, 85, 5),

pygame.Rect(610, 345, 5, 25),

pygame.Rect(555, 325, 5, 45),

pygame.Rect(555, 325, 30, 5),

pygame.Rect(583, 305, 5, 45),

pygame.Rect(583, 303, 30, 5),

pygame.Rect(610, 303, 5, 23),

pygame.Rect(610, 325, 30, 5),

pygame.Rect(635, 325, 5, 65),

pygame.Rect(610, 385, 30, 5),

pygame.Rect(610, 385, 5, 48),

pygame.Rect(583, 385, 5, 70),

pygame.Rect(583, 430, 32, 5),

pygame.Rect(558, 450, 25, 5),

pygame.Rect(558, 450, 5, 25),

pygame.Rect(425, 470, 133, 5),

pygame.Rect(531, 431, 5, 40),

pygame.Rect(477, 430, 82, 5),

pygame.Rect(477, 410, 5, 25),

pygame.Rect(451, 408, 85, 5),

pygame.Rect(423, 452, 5, 24),

pygame.Rect(398, 452, 28, 5),

pygame.Rect(399, 430, 5, 26),

pygame.Rect(371, 430, 28, 5),

pygame.Rect(371, 324, 5, 108),

pygame.Rect(371, 409, 30, 5),

pygame.Rect(291, 109, 5, 215),

pygame.Rect(264, 153, 30, 5),

pygame.Rect(264, 196, 30, 5),

pygame.Rect(264, 196, 5, 23),

pygame.Rect(240, 217, 28, 5),

pygame.Rect(240, 260, 56, 5),

pygame.Rect(240, 260, 5, 23),

pygame.Rect(240, 282, 28, 5),

pygame.Rect(263, 282, 5, 25),

pygame.Rect(213, 324, 82, 5),

pygame.Rect(213, 324, 5, 23),

pygame.Rect(186, 345, 32, 5),

pygame.Rect(265, 324, 5, 87),

pygame.Rect(212, 409, 56, 5),

pygame.Rect(212, 388, 5, 25),

pygame.Rect(158, 388, 56, 5),

pygame.Rect(239, 409, 5, 23),

pygame.Rect(133, 430, 110, 5),

pygame.Rect(159, 430, 5, 25),

pygame.Rect(210, 430, 5, 25),

pygame.Rect(296, 132, 104, 5),

pygame.Rect(345, 89, 5, 45),

pygame.Rect(371, 68, 5, 236),

pygame.Rect(371, 89, 33, 5),

pygame.Rect(292, 68, 84, 5),

pygame.Rect(292, 68, 5, 23),

pygame.Rect(265, 89, 56, 5),

pygame.Rect(265, 89, 5, 23),

pygame.Rect(318, 89, 5, 23),

pygame.Rect(376, 281, 25, 5),

pygame.Rect(371, 302, 57, 5),

pygame.Rect(424, 302, 5, 25),

pygame.Rect(424, 324, 30, 5),

pygame.Rect(451, 281, 5, 110),

pygame.Rect(424, 281, 30, 5),

pygame.Rect(424, 238, 5, 45),

pygame.Rect(398, 238, 30, 5),

pygame.Rect(398, 196, 5, 70),

pygame.Rect(398, 196, 30, 5),

pygame.Rect(424, 174, 30, 5),

pygame.Rect(424, 174, 5, 25),

pygame.Rect(398, 323, 5, 68),

pygame.Rect(403, 345, 25, 5),

pygame.Rect(424, 366, 30, 5),

pygame.Rect(398, 388, 58, 5),

pygame.Rect(424, 393, 5, 40),

pygame.Rect(424, 430, 31, 5),

pygame.Rect(451, 430, 5, 25),

pygame.Rect(451, 452, 56, 5),

]

# points coordinates

coordinates = [

[118, 136], [64, 162], [144, 98], [248, 54], [222, 92], [222, 118], [222, 223], [249, 290], [275, 269], [171, 332],

[223, 350], [250, 365], [208, 374], [144, 356], [196, 398],

[221, 417], [143, 439], [196, 440], [118, 458], [382, 312],

[435, 374], [409, 356], [436, 333], [380, 395], [409, 438],

[512, 442], [512, 416], [549, 375], [594, 314], [542, 455],

[435, 266], [408, 206], [461, 161], [408, 248], [140, 95],

[408, 135], [407, 78], [361, 54], [276, 72], [302, 99],

[328, 77], [356, 118], [463, 73], [433, 118], [414, 118],

[595, 78], [513, 75], [488, 76], [619, 54], [463, 242],

]

# play function

def play():

# player details

global score

score = '0'

player = pygame.Rect(140, 95, 10, 10)

player_speed = 2

random_index = int(random.random()*50)

selected_coordinate = coordinates[random_index]

point = pygame.Rect(selected_coordinate[0], selected_coordinate[-1], 6, 6)

time = 31

# score counter and adder

def score_counter():

global score

remainder = int(score)

score = f'{remainder + 1}'

# game play

while True:

# time control

time -= 1/60

if int(time) <= 0:

state = False

else:

state = True # movement

if state:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

keys = pygame.key.get_pressed()

if keys[pygame.K_UP] and player.y >= 0:

player.y -= player_speed

if keys[pygame.K_DOWN] and player.y <= 490:

player.y += player_speed

if keys[pygame.K_LEFT] and player.x >= 0:

player.x -= player_speed

if keys[pygame.K_RIGHT] and player.x <= 690:

player.x += player_speed

# collision control

for wall in maze_wall:

if player.colliderect(wall):

if keys[pygame.K_UP]:

player.y += player_speed

if keys[pygame.K_DOWN]:

player.y -= player_speed

if keys[pygame.K_LEFT]:

player.x += player_speed

if keys[pygame.K_RIGHT]:

player.x -= player_speed

# points

if player.colliderect(point):

score_counter()

random_index = int(random.random()*50)

selected_coordinate = coordinates[random_index]

point.x, point.y = selected_coordinate

# adding time

if time < 5: time += 10

else:

time += 5

screen.fill(WHITE)

# drawing entities

for wall in maze_wall:

pygame.draw.rect(screen, BLACK, wall)

pygame.draw.rect(screen, RED, player)

pygame.draw.rect(screen, BLUE, point)

time_text = font.render(f"Time: {int(time)}s", True, GREEN)

screen.blit(time_text, (10, 10))

score_text = font.render(f"Score:{score}", True, GREEN)

screen.blit(score_text, (WIDTH - 210, 10))

pygame.display.flip()

# start time

pygame.time.Clock().tick(60)

else:

# to quit

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

# when time runs out

keys = pygame.key.get_pressed()

# press enter to play function

if keys[pygame.K_RETURN]:

play()

screen.fill(BLACK) message = font.render(f'GAME OVER', True, RED)

screen.blit(message, (250, 200))

message1 = font.render(f'Press "Enter" to play again', True, WHITE)

screen.blit(message1, (200, 300))

pygame.display.flip()

pygame.time.Clock().tick(60)

# Start the game

play()

```

Running Your Game

13.1 Save Your File

Make sure you've saved maze_game.py (Ctrl+S or Cmd+S).

13.2 Run From Terminal

In VS Code terminal:

```bash

python maze_game.py

```

Or on Mac/Linux:

```bash

python3 maze_game.py

```

In regular terminal/command prompt: ```

2. Run the game:

```bash

python maze_game.py

```

13.3 What You Should See

· A white window appears

· Black maze walls

· Red player square

· Blue point to collect

· Timer counting down

· Score increasing as you collect points


13.4 Troubleshooting

Problem Solution

"pygame not found" Reinstall pygame: pip install pygame

Game runs too fast Check tick(60) is present

Player stuck in wall Check collision logic

Points not appearing Verify coordinates list

Game won't start Check play() at bottom of file

Customizing Your Game

Now that you have a working game, here are ways to make it your own: · Place them in challenging locations

14.3 Modify Game Rules

· Change starting time

· Adjust time bonuses

· Increase/decrease player speed

· Add more points per collection

14.4 Add Features

· Sound effects when collecting points

· Background music

· Multiple levels with different mazes

· High score tracking

· Different colored points worth different amounts

14.5 Visual Enhancements

· Change colors

· Add images instead of rectangles

· Animate the player

· Add background image

Sharing Your Game

15.1 Package for Others

To share with friends who don't have Python:

1. Install PyInstaller:

```bash

pip install pyinstaller

```

2. Create executable:

```bash

pyinstaller --onefile maze_game.py 15.2 Share the Code

· Upload to GitHub

· Share on Instructables

· Send the .py file directly

Conclusion

π reminds us that simple concepts can lead to infinite complexity. Your code is the same —

starting with simple rectangles and loops, you've created an interactive experience that someone can enjoy.