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.
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.
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
which reduces the value of health by 1 and destroys the bullet.
which also reduces the value of health by 1 and destroys the bullet.
which would increase health by 10 and then destroy the pack.
keeps the health within a range of 1 and 100.
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
This draws the value of lives as text at position 50,50.
Score
which draws the text Your Score followed the value of score.
Basic Projects
- 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.
- B)
Make lives image change size from small to large and back. Do this through code.
Advance Projects
- 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.
- 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
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.