python logo

python maze game


Python hosting: Host, run, and code Python in the cloud!

In this tutorial you will learn how to build a maze game. The idea is simply to move around the maze with the arrow keys.

Related course:
Create Space Invaders with Python

Getting started: Basic structure and event handling.
We define a class Player which holds the players position on the screen and the speed by which it moves. In addition we define the actions a Player instance can do (movements):

class Player:
x = 10
y = 10
speed = 1

def moveRight(self):
self.x = self.x + self.speed

def moveLeft(self):
self.x = self.x - self.speed

def moveUp(self):
self.y = self.y - self.speed

def moveDown(self):
self.y = self.y + self.speed

A player object can be created and variables can be modified using the movement methods. We link those methods to the events. In Pygame we can get non-blocking keyboard input using this code:

pygame pygame

Creating the maze

We define a matrix of NxM to represent the positions of the maze blocks. In this matrix the element 1 represents the presence of a block and element 0 represents the absence.

class Maze:
def __init__(self):
self.M = 10
self.N = 8
self.maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,]

We have this complete code to draw the maze:

from pygame.locals import *
import pygame

class Player:
x = 44
y = 44
speed = 1

def moveRight(self):
self.x = self.x + self.speed

def moveLeft(self):
self.x = self.x - self.speed

def moveUp(self):
self.y = self.y - self.speed

def moveDown(self):
self.y = self.y + self.speed

class Maze:
def __init__(self):
self.M = 10
self.N = 8
self.maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,]

def draw(self,display_surf,image_surf):
bx = 0
by = 0
for i in range(0,self.M*self.N):
if self.maze[ bx + (by*self.M) ] == 1:
display_surf.blit(image_surf,( bx * 44 , by * 44))

bx = bx + 1
if bx > self.M-1:
bx = 0
by = by + 1


class App:

windowWidth = 800
windowHeight = 600
player = 0

def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self._block_surf = None
self.player = Player()
self.maze = Maze()

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

pygame.display.set_caption('Pygame pythonspot.com example')
self._running = True
self._image_surf = pygame.image.load("player.png").convert()
self._block_surf = pygame.image.load("block.png").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False

def on_loop(self):
pass

def on_render(self):
self._display_surf.fill((0,0,0))
self._display_surf.blit(self._image_surf,(self.player.x,self.player.y))
self.maze.draw(self._display_surf, self._block_surf)
pygame.display.flip()

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
pygame.event.pump()
keys = pygame.key.get_pressed()

if (keys[K_RIGHT]):
self.player.moveRight()

if (keys[K_LEFT]):
self.player.moveLeft()

if (keys[K_UP]):
self.player.moveUp()

if (keys[K_DOWN]):
self.player.moveDown()

if (keys[K_ESCAPE]):
self._running = False

self.on_loop()
self.on_render()
self.on_cleanup()

if __name__ == "__main__" :
theApp = App()
theApp.on_execute()

maze maze

Related course:
Create Space Invaders with Python

Concluding
You learned how to create a 2d maze in Python. Now you may want to add collision detection which we showed in our previous tutorial. Because we already explained that concept we will not go over it again :-)

Next tutorial: AI with Pygame






Leave a Reply:




Andy Sabantara Sat, 24 Dec 2016

I'm really not understand in part this:

bx = bx + 1
If bx > self.M-1:
bx = 0
by = by + 1

I'm using Python 3++ sir.

Andy Sabantara Sun, 25 Dec 2016

and how make this maze and player is solid? please explain it.

Frank Wed, 28 Dec 2016

Its a one dimensional loop, filling a two dimensional space. To do so, the y position must be updated (+1) once the width is reached.

Frank Wed, 28 Dec 2016

the matrix self.maze defines which are solid and space. If a cell is '1', its solid

vishal chaudhary Thu, 13 Jul 2017

I did not understand these lines of code.
Some syntactical error in these lines. So, please correct it.


if bx > self.M-1:
bx = 0
by = by + 1

Frank Fri, 14 Jul 2017

This is how the program walks through the screen. It starts at top left and ends on bottom right.
If the maximum horizontal steps are taken (self.M-1), it takes a vertical step.
It goes through the matrix that way. The variables bx and by are the current position in the matrix.
This position changes because its inside a for loop.

William Mon, 15 Feb 2021

Where are the image files 'pygame.png' 'player.png' and 'block.png' to be found?

Frank Mon, 15 Feb 2021

Just pick any images from the web. I got the cube images from an open source game, but I don't remember which.

Yitzhak Spielberg 2021-12-22T12:08:30.853Z

Thank you so much for this very useful tutorial !