© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_8

8. Health, Lives, and Score

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 

Although most games will have different goals, most will have some kind of health, lives, or score system. GameMaker makes it easy to manage such variables by using global variables.

Note Setting health, lives, and score to have global scope allows them to be accessed by in game instance, ideal if you use a separate instance for displaying the in game HUD. If you have multiple instances of other objects in game (such as enemies) that you also want to have health, lives, or score, use instance variables such as: hp=5; my_health=75; health, lives, and score can be accessed, changed, drawn, and saved.

Note

The variables health, lives, and score have global scope, without the need to put global. before them. Note, however, in the future, they maybe redacted, so you may need to create your own global variables, as follows, for example.

I usually set the starting variables at the game start in a splash room for doing just that):
global.player_score=0;
global.player_health=100;

global.player_lives=10;You can treat all of these variables the same as you would any global variable; you can test, change, and draw these variables. The advantage of global variables is that they can be accessed from anywhere else in game.

Health

This code is in a bullet’s Collision Event with a player object:
health--;
instance_destroy();

which reduces the value of health by 1 and destroys the bullet.

Or if you are doing it within the player object with a Collision Event with obj_bullet:
health-=1;
with instance_destroy(other);

which also reduces the value of health by 1 and destroys the bullet.

Another example would be the player in a Collision Event with a health pack:
health+=10;
with (other) instance_destroy();

which would increase health by 10 and then destroy the pack.

You may wish to have the health set between 2 values, for example 0 and 100, say after the player collects a bonus health instance for 50 bonus health points. This can be done :
health+=50;
health=clamp(health,0,100);

keeps the health within a range of 1 and 100.

You can also draw the value of health graphically as a bar, for example:
draw_healthbar(x-100,y-50,x+100,y-30,(health/100)*health,c_green,c_red,c_red,0,true,true);

which would draw bar centered and above the instance this code is in. It will have a width of 200 and a height of 20. This needs to be placed in a Draw Event.

Lives

You can draw lives as text, for example:
draw_text(50,50,lives);

This draws the value of lives as text at position 50,50.

You can also draw lives using sprites:
for (var i = 0; i < lives; i += 1)
{
      draw_sprite(spr_lives,0,50+(50*i),50);
}
This will draw the number of lives as images, evenly spaced out, as shown in Figure 8-1.

An output window of created with game maker studio 2 has five heart emojis.

Figure 8-1

Showing lives drawn using code

Score

You can draw the score using code:
draw_text(100,40,"Your Score "+string(score));

which draws the text Your Score followed the value of score.

Basic Projects

  1. A)

    Make a system that draws the player's health as a bar at the top of the window. Draw player's lives as images. Set up so Q and W change the health, and, A and S change the number of lives. Clamp health at a maximum of 100.

     
  2. B)

    Make lives image change size from small to large and back. Do this through code.

     

Advance Projects

  1. C)

    Set up a system that draws score (mod 1000) as a bar, and level as text. Increase level by 1 for every 1000 points. Allow pressing Q to increase score.

     
  2. D)

    Set up three buttons that become active after 1000, 2000, and 3000 points. Use subimages to show if active or not, change the subimage when mouse hovers (if active). Draw a basic HUD system

     

Useful Functions

variable_global_exists()
variable_global_get()
variable_global_set()

Summary

You should now be able to work with lives, health, and score. You can set, change, and draw these values in an appropriate fashion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.81.109