Setting up player health and lives
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.
Getting ready
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.
How to do it...
- In the Create event, from the Score tab, drag and drop the Set Health action into the Actions box and set
Health
to100
. - From the same tab, drag and drop Set Lives and set it to
3
. - Add a Set Variable to the Actions box and set
hit
to 0. - Click Add Event, then Collision, and select
obj_enemy_patrol
from the menu. - Add Test Variable to the Actions box and have it check for whether
hit
equals0
. - Below, drag and drop Start and End Block, and within that block add Set Health with a relative value of
-20
. - Add Set Variable, setting
hit
to1
. - Add Set
Alarm 1
and set it to 60 steps. Repeat these actions usingobj_hazard_spike
in place of your enemy. - Add an event for Alarm 1 and drag Set Variable to the Actions box, setting hit to
0
. - Add the event Draw GUI and, from the score tab, drag Draw Health to the Actions box.
- Make sure Relative is checked and, under the appropriate headings, enter the following values:
x1: -16 y1: -24 x2: 16 y2: -34 back color: black bar color: green to red
- Drag and drop Draw Life Images to the Actions box and enter the following values:
x: room_width-64 y: 64 image: spr_life
- Create a Step event and add a Test Variable that checks if
health
is equal to0
. - Below that, drag a Start and End Block and within that block add Set Variable
lives
to-1
relative, and Set Variablehealth
to100
.
How it works...
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
.
There's more...
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.