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

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 situations whereby Pac-Man is outside the screen (and make it reappear on the opposite end)
  • Add basic AI (paths)

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

Make Pac-Man reappear at the other hand of the room

At this stage, we Pac-Man can move around the maze and collect objects. However, it would be great if we could change the programme so that, if it reaches one hand of the game (e.g., right side) that it disappears and reappears on the opposite side. For this purpose, we will change the properties of the object that we have already created for Pac-Man: pac_man_obj

  1. Double-click on the object pac_man_obj in the left-hand part of the screen to display its properties
  2. Once the window is open, click on Add Event, then Other, then Outside Room.This event will be called whenever Pac-Man is outside the room, either vertically or horizontally.
  3. Select the Tab Action from the right-hand side of the window and drag and drop the icon labelled Wrap Screen as illustrated on the next figure.
Wrapping the coordinates to fit the screen

Wrapping the coordinates to fit the screen

  • In the new window labelled “Wrap Screen”, click on the section to the right of the label direction and select the option in both direction from the list. This will ensure that the object will reappear on scree if its horizontal position (x coordinate) or vertical position (y coordinate) are outside the screen.
  • Click OK.

At this stage, we just need to modify the room slightly so that we leave corresponding horizontal and vertical gaps on all four sides of the maze left:

  • Double-click on the room Level1 from the left-hand part of the screen to display its properties.
  • Click on the tab Objects, to be able to add/remove objects
  • Modify the room to include four gaps in all four sides of the room, as illustrated in the next figure.

gaps_wrap_screen

Once you are happy with the look of your room, close its properties and test it to ensure that if Pac-Man reaches any of these gapes, that it reappears on the other hand of the screen.

Adding Simple AI

We will now add a little bit of challenge to the player by adding a simple Non-Player Character (NPC) that will follow a path.

First, let’s create a corresponding sprite and object for this NPC:

  1. Create a new sprite, rename it monster_path_spr and use the sprite monster1.gif located in the initial folder that you have downloaded in Part 1
  2. Create the associated object, that you will call monster_path_obj and that will be using the sprite monster_path_spr

Once this is done, we will create a path. To do so, you will need to switch to the advanced mode, am mode that provides more option for your game. To switch to this mode, select File > Advanced Mode from the top menu. At the prompt, make sure that you chose to “Save your Changes” and click OK. Game Maker will then restart, and you can then reload your project using File > Open or File > Open Recent.

Once the project has been reloaded, you will notice that the tool bar at the the top of the window includes more items, including a shortcut for the creation of paths, as illustrated on the next figure.

New toolbar (advanced mode)

New toolbar (advanced mode)

Let’s create a new path:

Select Resources > Create Path (or use the shortcut highlighted in the previous figure)

  • In the new window, set the Name of the path to path1, SnapX, and SnapY to 32
  • Once this is done, it would be great to display the window where the path will be applied. This can be done by pressing the button located at the top-right of the screen, as illustrated on the next figure.
displaying the corresponding room for the path

Displaying the corresponding room for the path

  • After clicking this button, a list of rooms (if you have more than one room) will appear, and you can choose Level1, as the path we will be creating will apply to this room.
  • At this stage, the room Level1 will appear, with all the objects within, and we just need to set our path
  • A path is defined by several points or dots; by clicking in different locations in the window, we can add (or remove) the points that make up our path, and effectively define a path that can be later used by one of the NPCs. All points for the path are defined by a x and y coordinate and are listed on the left-hand side of the window once they have been added. You will also notice that the path can be either closed or open (i.e., the first and last dots are linked) and that the path can be drawn using straight lines or a smooth curve.

For now we will just create three dots:

  • Click in three different locations to define your path.
  • Leave all the other options as default.
  • Once you are happy with your path, you can save the setting by clicking on OK
path1

Setting the path

Once the path has been created, we just need to allocate our NPC to this path. Let’s proceed:

  • Open the properties of the object monster_path_obj.
  • Click on Add Event, and then Create. This event is triggered whenever the object is created.
  • Click on the tab Move and then drag and drop the icon labelled Set Path to the Action window, as illustrated in the next figure.
set_path

Allocating a path

In the new window, set the settings as follows:

  • path: path1 (the path we have just created)
  • speed: 2 (or any other value that you wish to specify)
  • at the end: stop (you could change this setting if you wish)
  • relative: absolute (this is quite important so that the path is the same as how it looked when created; relative would set the path in relation to the position of the object in the room )
set_path_c

Setting the path’s properties

Once the path has been allocated, we just need to add the corresponding object to the room:

  • Open the properties of the room
  • Click on the tab Objects
  • Add the object monster_path_obj anywhere in the room

The End … (for now)

That’s it. You have managed to add some form of Artificial intelligence to the game. While this is rather simple, the next tutorial will show you how you can include more advanced behaviours. The important principles to bear in mind here are as follows:

  • Path consist of several points.
  • One created, paths can be allocated to an object at its creation (or during game play)
  • The Action Wrap Screen, will ensure that your character remains onscreen even if it navigates outside its boundaries.

Related Articles: