Create a PacMan Game in 20 minutes with Game Maker: Part 2

Pac-Man Image from arcade-museum.com

Prerequisites for the tutorial

Length of the tutorial

This tutorial should take 5 minutes to complete

Objectives of the tutorial

After completing this tutorial you should be able to:

  • Manage collisions
  • Collect objects
  • Destroy Objects

Acquiring the assets for your game

  • Download the assets to create your game here.
  • Save the zip file on your desktop
  • Unzip the folder, you should obtain a folder called ‘pacman’ that includes images

Adding walls to the room

At this stage, we only have Pac-Man moving around the room, and we may need to ad more objects, including walls and dots to collect. Let’s add two sprites, one for the wall and one for the dots to be collected:

  1. From the top menu, select: Resources > Create Sprite
  2. A new window should appear
  3. In the section labelled “Name”, type “wall_spr
  4. Click on the button labelled “Load Sprite
  5. In the new window labelled “Open Existing File”, click on the folder where you have saved the sprites downloaded previously, and display its content(e.g., Desktop).
  6. Open the folder labelled “pacman” that you have created previously (from the zip file that was downloaded).
  7. Select the image labelled “wall”, and click on the button labelled “Open”. This should display a preview of the animated image on the right hand side of the sprite window in Game Maker.
  8. Click the button labelled “OK”
  9. Repeat these steps to create a sprite called dot_spr based on the sprite dot.gif

At this stage, you should have two additional sprites ready to be used for new objects as illustrated on the next Figure.

dot_and_wall_sprites

New sprites for walls and dots

We will now create a corresponding object.

Creating new objects:

  1. From the top menu, select: Resources > Create Object
  2. A new window should appear.
  3. In the section labelled “Name”, type “wall_obj”
  4. Leave the box labelled “Solid” empty (we will use this feature at a later stage for AI)
  5. In the section labelled “Sprite”, click on the text “<no sprite> and select “wall_spr” from the list.
  6. Click on the button labelled “OK”.
  7. Repeat these steps to create an object labelled dot_obj that is using the sprite dot_spr created previously

Adding the new objects to the room

At this stage, we have two new object with associated sprites and we want to add them to the room, so that we can create corresponding interactions with Pac-Man:

  1. Open the properties of the room that you have created previously by double-clicking on its name in the list located on left-hand side of the screen
  2. Once the room properties window is open, click on the tab labelled “Objects”: this tab is used to add/remove objects from the scene
  3. In the new tab, click on the empty field below the label “Object to add with the left mouse” and select the object “wall_obj”, that you have created previously..
  4. Once this object has been selected, you can perform a left-click  anywhere in the room to add the wall_obj object to the room. Note that to make things a little bit faster, you can keep the SHIFT key pressed after left-clicking, and just drag your mouse around the screen where you want to add new objects.
Room with walls

Room with walls

Managing collision between Pac-Man and the walls

Once you have created the walls, it is now time to manage collisions and this will be done using events:

  1. Double-click on the Pac-Man object on the left-hand side of the screen. This should display its properties
  2. Click on the button labelled “Add event”
  3. Click on the event “Collision” and select the object “wall_obj” to specify that you want to specify actions when the pac_man_obj object collides with a wall. This should add a “wall_obj” collision event in the event window
  4. Click on the tab labelled “Move
  5. From the “Move” section of the window, drag the top left icon (red star) and drop it in the “Action” section; this should open a window called “Move fixed

Adding Movement to Pac-Man

  1. In the new window, click on the middle square  and set the speed to “0”. By doing this, we specify that the object should not move (or stop moving) as illustrated on the next Figure:
    Stopping the character after collision

    Stopping the character after collision

    Once this has been done, click OK and test your game to ensure that the collision is accounted for.

Adding dots to the room

So far we have managed to account for collisions with walls as Pac-Man moves around the maze. However, in the original game, Pac-Man also need to collect dots (in fact all the dots in the level) to be able to proceed to the next level. Let’s add these dots and implement a pick-up mechanism.

  1. Open the properties of the room that you have created previously by double-clicking on its name in the list located on left-hand side of the screen
  2. Once the room properties window is open, click on the tab labelled “Objects”: this tab is used to add/remove objects from the scene
  3. In the new tab, click on the empty field below the label “Object to add with the left mouse” and select the object dot_obj, that you have created previously..
  4. Once this object has been selected, you can perform a left-click  anywhere in the room to add the dot_obj object to the room. Note that to make things a little bit faster, you can keep the SHIFT key pressed after left-clicking, and just drag your mouse around the screen where you want to add new objects.
room_add_dots

Adding dots to the room

Last item: collecting dots

We can specify what should happen when Pac-Man collides with them. In this particular instance, the dot should disappear (we could also increase the score by 1).

  1. Double-click on the Pac-Man object on the left-hand side of the screen. This should display its properties.
  2. Click on the button labelled “Add event
  3. Click on the event “Collision” and select the object “dot_obj” to specify that you want to set actions when the pac_man_obj object collides with a dot. This should add a “dot_obj” collision event in the event window
  4. Click on the tab called “Main1” and drag and drop the icon “Change Sprite” to the action window, as illustrated on the next figure.
Destroying the dot after collision

Destroying the dot after collision

  1. In the new window, select the option “other” to specify that the other object involved in the collision (i.e., the dot) should be destroyed, as described on the next figure.

set_destroy_dot_other

  1. Click OK and test your game

The End … (for now)

That’s it. You have managed to move Pac-Man in all four directions and to stop its movement when the keys are released. You can, of course,  apply these principles, to any other game object or type of game. The important principles to bear in mind here are as follows:

  • Actions are performed when an event occurs
  • An event is defined for a particular object
  • Each object needs to be created and added to the room
  • Each object can be based on sprites that need to be created accordingly
  • The appearance of an object can change overtime by allocating different sprites

Congratulations, you can now move to Part3.

Related Articles: