
GameMaker Cookbook
By :

Any action game involving a player controlled character is likely to have some sort of "health" and "life" system. These are terms long used in video games that are used in conjunction with how your character is abstractly "alive" within the game itself. In reality, health and life in games are simply numbers whereby you lose a chance to play or even the game itself when they reach zero. GameMaker makes keeping track of these numbers quite easy when it comes to the drag and drop actions, so we're going to look at how we can add these elements to our player character.
Since this recipe involves making additions to your existing player character, you don't need to do much. You'll need a sprite to indicate your player's lives (call it spr_life
) but other than that all you need to do now is open the obj_player
Object Properties window.
Health
to 100
.3
.hit
to 0.obj_enemy_patrol
from the menu.hit
equals 0
.-20
.hit
to 1
.Alarm 1
and set it to 60 steps. Repeat these actions using obj_hazard_spike
in place of your enemy.0
.x1: -16 y1: -24 x2: 16 y2: -34 back color: black bar color: green to red
x: room_width-64 y: 64 image: spr_life
health
is equal to 0
.lives
to -1
relative, and Set Variable health
to 100
.Health and lives are very straightforward, with GameMaker doing a lot of the work for you when using the drag and drop actions. Essentially, GameMaker has created the variable health
and you've set it to a value of 100
. You've also created the variable hit
, which you use to control when you can be injured. If hit
is set to 0
(off) then you can be hit by an enemy or hazard. If you are hit by one of them you lose 20 health points and the variable hit
is set to 1
. This is to prevent contact with an enemy continuously draining your health, as it is being checked every step. An alarm is set to 60 steps and at the end of that countdown hit
is set back to 0
, meaning you can be injured once more.
The Step event is also constantly checking for when your health reaches 0
. When it does, the lives
variable is decreased by 1
and your health is reset to 100
.
In this case we set the health bar to a specific coordinate relative to the player's position. This was purely a design choice to show you that you have options. You can set the health bar to a static position on the screen, much like we did for the lives indicator. As for the lives indicator, using the Draw Lives action instead of Draw Life Images allows you to indicate the player's remaining lives with text and numbers.
Change the font size
Change margin width
Change background colour