If you had a go at using Game Maker to build the maze game, you may remember monsters were able to kill the player character, but the player character couldn’t retaliate against the monster.
To provide some balance to the game, many games allow the player character to be able to kill the monsters. In the original version of the Pacman maze game, eating a particular piece of ‘fruit’ allowed the Pacman character a period of grace during which it could catch a monster and send it back to the jail in the centre of the room for a short period of time. In a platform game, one common mechanic for attacking a monster is to jump on it…
Making Monsters Look Beautiful
Let’s start by making a monster object. In the simple case, let’s just define a monster type that can patrol a fixed portion of a platform. The monster will wander back and forth along part of the platform, looking in the direction it is traveling in. If it collides with the player character at platform level, the player character loses a life and goes back to the start of the room. However, if the player character can jump so that it lands on top of the monster, the monster will be killed.
How might you define the object and its associated sprite so that the monster can walk from left to right and right to left, looking in the direction it is travelling, and reversing its direction if it collides with a solid wall object?
In the maze game tutorial resources folder, there are three images that show the monster – monsterl, monsterr and monster_flat (for when it has been squished!).
Can you think of a more compact way of packaging the sprite images used to display the monster?
Remember that several ‘subimages’ can be combined in a single image loaded in as a tile set. With the different aspects of the monster contained in several separate image files, you will need to load the separate images into a single sprite. Create a new sprite (spr_monster, for example) and click on Edit Sprite.
Using either the ‘Add a sprite from file’ toolbar button, the “Add from File…” option from the file menu, or the keyboard shortcut ctrl-A, add at least the first two monster images into the sprite.
If you only had the left (or right) facing monster, how might you obtain a right (or left) facing monster?
If you are happy for the left and right facing monsters to be exact mirror images of each other, you could simply load in two right facing monsters (for example), then open one of them into the Image editor, select the whole image (ctrl-A usually does the trick, or Select All from the Edit menu) and then Mirror horizontal from the Transform menu.
In the post Improving the Visual Design of a Game – Better Sprite Design, I described how to use the Change Sprite action to select a different subimage from a sprite depending on which direction the the player character was moving, as determined by which arrow key was being pressed to control the character’s actions.
We shall use a similar action here to select the relevant sprite image for the monster, depending on which direction it is traveling in.
But first we need to start the monster moving…
We then need to choose the appropriate sprite based on the direction the monster is traveling. To do this, we are going to use a variable that is defined for each object instance within Game Maker that has a value corresponding to the direction the instance is traveling. the direction is specified in degrees, with 0 point to the right, 90 straight up, 270 straight down and so on.
When the monster is traveling to the left (direction will be set to 180), we want to choose subimage 0 of out sprite. When it is moving to the right (direction equals 0), we want to choose subimage 1.
Calculate the value of the expression (180-direction)/180 for a monster when it is traveling: a) to the left; b) to the right. Will the correct sprite image be shown for each direction the monster travels in?
The speed of the sprite is set to 0 so that the sprite does not rotate through each of the subimages contained within it by itself.
Create a simple room with an instance or two of the monster in it and move the room so that it is the first room in the room list. (You can reorder rooms by clicking on them and dragging them to the position in the list you want them to appear. The game will start using the first room in the list.)
Does the sprite image point the correct way? Stop the game, change the original direction of the monster and run it again. Does the sprite point the other direction?
Monsters on Patrol
As it stands, the monsters will move horizontally in the direction it started forever. To make matters a little more realistic, we could define them so that they change direction when they collide with a wall object. But how might we get the monsters to just patrol a limited section of a platform?
Knowing what you already know, can you think of a way you might use a new object to limit the patrol area of a monster in a similar way to the way the wall object currently impedes the monster, but without affecting the player character?
The trick is to create an invisible marker block that causes the monster to reverse direction if ever a monster collides with the marker, whilst not affecting the player character. By placing invisible marker blocks at each end of a platform, we can make sure that the monsters never fall off the platform, or walk over the edge of one. By placing invisible markers at certain points on the floor (base platform), we can limit a monster’s patrol area to a particular part of the ground floor platform.
Create a new object with a basic sprite (so we can see where it is in a room) but do not make it Visible or Solid.
In the monster object, add a collision event that checks for a collision with a marker, and performs a
Reverse horizontal direction action in the event of such a collision.
Run the test room. Does the sprite face in the correct direction after the collision?
If subimage 0 is left facing, and the monster starts off moving from left to right (direction 0), then setting the sprite image initially to (180-direction)/180 will set the sprite subimage to (180-0)/180 = 180/180 = 1. When the monster collides, and the direction is reversed (for example to moving left – that is, in direction “180”), the subimage will be set to (180-180)/180 = 0/180 = 0, that is, left facing.
Design one ot two new rooms for your platform game that incorporate monsters that patrol some of the platforms, or different parts of the same, long platform. Test your game – do the monsters patrol just the areas you intended them to? The intention at the moment is just for you to gain familiarity in how to add patrol areas to the game, using the invisible marker blocks. If you feel you need to add a little jeopardy to the game, see if you can find a way of sending the character back to the start of the room if it collides with a monster.
You can use a similar technique to mark out patrol areas for monsters in a maze game. If you have a few minutes spare, why not add a new room to your maze game that incorporates such a feature? Can you also think how you might be able to use a similar technique to mark out an area of the maze game so that the monster patrols a square path? Why would such a path be likely to “make sense” in a maze game, but not in a platform game?