It’s all very well having monsters in a game, providing one of the arbitrary challenges we use to impede the progress of a player character through a game, but sometimes we just want to to retaliate and, in short, just “take them out”.
Killing monsters in games can be achieved by a variety of means – hand-to-hand combat, fireballs, big guns, and so on, but a natural monster killing mechanic for a platform game is to jump on it.
In One, Two, Three, JUMP! we looked at how to make a character jump – now we need to modify the action so that if we jump in such a way that we land on a monster, we kill it… After that, we’ll look at how to shoot a monster from a distance…
We will add the monster killing ability as a series of actions associated with the player character. The even we need to detect is the “collision with monster” event. If the the collision happens whilst the player character is traveling in a sideways direction, the player character loses a life. If the player character collides with the monster by landing on it from above, the monster dies.
Add a new collision event to the player character object that detects a collision with a monster. We now need to detect from which direction the player character collided with the monster.
We do this by testing to see whether the expression
vspeed > 0 && y < other.y+8 is true. So what does this expression mean? If
vspeed > 0 then we know that the vertical speed of the player character is greater than zero – that is, the player character is moving in the positive y direction – downwards, in other words (remember, the origin x=0, y=0 is in the top left of the room, with positive x to the right, and positive y pointing down.).
&& means ‘logical AND’. That is, both the first part of the expression (
vspeed > 0) and the second part of the expression (
y < other.y+8) must BOTH be true, at the same time for the whole expression to be true.
So what does
y < other.y+8 mean?
other corresponds to the other object instance in the collision – that is, a monster. The expression tests that current position of the player character is above the bottom of the monster character. Since we know the player character has collided with the monster, and the other half of the expression (
vspeed > 0) is checking that the player character is moving downwards, this means essentially that the player character must have fallen onto the monster character. So it must die…
If the expression has not evaluated true, then the player character collided with the monster from the side, so the player should suffer some penalty, such as going back to the start of the room.
That, in a nutshell, is how to kill the monster by squishing it!
You might of course want to have a rather more elaborate series of actions to manage the death of a monster or the player character. In the official Game Maker platform game tutorial, example game 3 uses the following sequence of actions.
Start of a block and
End of a block statements group sets of action that are to be executed if the condition evaluates true or false. The comment action (the exclamation mark) is just there for our benefit – comments are ignored by Game Maker and are simply used to act as inline documentation for a game programme.
You might also notice that rather than killing the monster immediately, the monster is turned into a ‘dead monster’ object.
On its creation, the dead monster object sets an alarm that raises an alarm event after a short period of time. When the alarm goes off, the Self object is destroyed.
Another popular way of killing monsters is to shoot them. At its simplest, all we need to do is respond to a ‘shoot event’ (typically raised by pressing the spacebar on the keyboard) and create a fast moving bullet or missile object. If the bullet or missile hits the monster, the monster dies and the bullet is destroyed. Similarly, if the bullet hits a wall it is destroyed.
Create a new object (
obj_bullet) depicted by an appropriate sprite (
When a bullet is fired, the first thing we need to do is work out which direction to fire it. One way would be to detect the direction the player character is moving. Alternatively, if we are using different sprite images that show the player character looking to the left or to the right, we can detect which sprite is being used and determine the direction from that.
To see which sprite image is currently displayed for the player character, we must write a logical expression to check one of two different variables. If multiple sprites are being used to display the character (e.g. spr_playerLeft and spr_playerRight, as in the official Game Maker platfrom tutorial) we need to check the variable
sprite_index; if we are using a single sprite with several subimages, we need to check the variable
The VAR block (with shaped corners) is another condition testing block. If the condition is true, then the next instruction (or block of instructions) is executed.
So, having detected which way the player character is looking, we create an object that moves in that direction.
We also need an similar action to handle a character facing in the other direction.
If you try to play the game now, you should find that the player character fires bullets in the direction it is facing if you press the space bar.
As it stands, though, the bullet objects don’t actually do anything. At the very least, we want them to be able to kill the monsters…
The first Destroy object action should destroy the “Other” object (that is, the monster), the second action destroys the bullet.
Modify your game so that your player character can shoot the monster, and test it in a simple room. Does it work? Experiment with the speed of the bullet object. What happens if you set it too fast or too slow compared to the speed of the player character and the monster? What happens if there are no monsters in the direction you are firing? Add another collision event to the bullet object that destroys the bullet if it collides with a wall object.
You now have several interesting ingredients that you can add to your platform game (and also reuse in your maze game). Add a few rooms to one or other of the platform or maze games that make use of marked out monster patrol areas or the ability of the player character to kill monsters by jumping on them or firing bullets at them.
By adding the ability to kill monsters by shooting at them “from a distance”, how does this affect the way you can place monsters in a room compared to the case where the monsters are untouchable?