This maze game has been adapted for school use from a lecture written by Carl Gustafsson.

In this activity we will create a maze game.

In a maze game the player should move around a grid based maze trying to find a certain point called the target.  On the way around there are obstacles to overcome or things, such as diamonds, to collect.

The game will be built on an invisible grid made up of blocks that are 32 pixels wide x 32 pixels high.  This makes the game easier to design and reduces the chance of the player getting 'stuck' anywhere. 

Add 2 rooms. Set the room size to be 480x480 pixels and set the grid to be 32x32 pixels.

You could make your room any size that you like, but what size will you need to make sure that your grid will end evenly at the edges of the rooms?

Start off by adding 3 sprites: a wall, a person and a target.

Choose the wall sprite from the Pacman folder, because it is 32x32 pixels in size.  The target sprite is called special.gif and is also in the Pacman folder.  For your person, choose any suitable sprite, such as the ghost from the various folder.

Name them sprWall , sprPerson , & sprTarget

 
 You will notice that the person has a green background.  When placed in the game, as long as you tick transparent for your sprite, you will not see the background.  This is because Game Maker sets the transparent colour to be whatever colour the bottom left pixel of the object is.

For the sprWall, make sure transparent is not ticked.  (otherwise it won't look very good - it is square and no part of it should be transparent)

Now create a wall object:

  • Name it objWall
  • Use the wall sprite
  • Make it solid (so things can't pass through it)

Create the person object

  • Name it objPerson
  • Use the person sprite

Create a target object

  • Name it objTarget
  • The target object should not be solid, as the player needs to be able to move on top of it.  When the player collides with the target object, the game will continue on to the next room.

Set up movement for the person

The person will move from point to point on the grid as the cursor keys are pressed.

 
 Before we move the person, we must check to make sure that it is on a grid point.  If the person is in between two grid points, it should just keep moving in the same direction until it gets to a grid point.     

Add an event to the person object for the Keyboard  <Left>

Add the action from the Control tab 'If instance is aligned with grid'

Set the snap horizontal and snap vertical to be 32 pixels.

Add a start moving in direction .

Set the direction to be left and speed to be 4

Add actions for the up, down and right keys.  (You can use the duplicate event option to make this job easier)

Your actions should look like this for each key.

 

 
We need to choose a speed that is a factor of the grid size.  32 divided by 4 is 8, so 4 is a good choice.  Other speeds we could choose would be 1, 2, 8, 16.  Each step of the game, the person will move that many pixels.  If any other number was chosen for a speed the person might never stop on a grid intersection. Try to think why this is....
You can also choose a speed that is not a whole number, such as 32/9.     Why will this work? 

To stop the person moving, add actions for the <No Key> event under Keyboard events.  Click the centre block in the 'Start moving in a direction' options.

 

Set up collision handling

If the person collides with a wall it should stop, so add a collision event.  In the actions just stop the person moving.  You don't need to check for grid alignment first; just add a snap to grid action afterwards so that the person is still aligned on the grid.

This is what you will need for the person object:

Add a collision event to the target object for a collision with objPerson

We want to test whether the next room exists (we could be in the last room) before we move on to it.  So if the next room exists we will move into it, otherwise we must be in the last room so we will end the game.

These are the actions you will need:

  Draw 2 maze-like rooms, making sure that you have walls all around the outside.  Place the person and target at the beginning and end of your maze.
To build the walls quickly, hold down your shift key as you click and drag. 

Right click to remove unwanted blocks.

 

 

You should now have a very simple maze game with two levels! 

To make your game even more exciting than this, move on to Maze Game 2