# 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:

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

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

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.

We have this complete code to draw the maze:

maze

Related course:

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

Andy Sabantara Sat, 24 Dec 2016

I'm really not understand in part this:

`bx = bx + 1If 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 !