Creating a Player Health Bar in GameMaker
Tutorial 13: Creating a Player Healthbar
In this tutorial we will learn how to create a player health bar by setting up a variable for the player health. The number assigned to this variable is the number of frames in the GIF. This tutorial could be replicated for lives.
Create a Health Bar Sprite
- Create a new sprite called sprHealthBar and import sprHealthBar.gif below.
(right click and save this image to your computer)

Set up Health for your Player object
- In the Create event of your player object you will need a variable to track the player’s health.
Set the variable to the amount of frames your health bar or lives GIF has:
(Note you may already have coded this in a previous tutorial. If so skip to the Create a HealthBar object section )
maxHealth = 10;
currentHealth = maxHealth;
Create a collision event on the player object with the enemy & the enemy’s laser objects and deduct 1 from the currentHealth variable:
currentHealth -=1;
Create a Health Bar Object
-
Create an object for your health bar and associate the sprHealthBar sprite with it
-
Add a Create Event and initialise the object as follows:
image_speed =0; //stop the health bar animating image_index=0; // start on the frame index of 0 -
Add a Step event to it and insert this code:
if(instance_exists(objPlayer)){ image_index = objPlayer.currentHealth; }else{ image_index = 11; } -
Drag the healthbar object into the game room.
Place it either at the top or bottom of the screen so that it is not in the way of the player.
Additional Step:
There is a tutorial on how to create a camera room, if you want your healthbar to follow the camera use the following code
x = camera_get_view_x(view_camera[0])+900;
y = camera_get_view_y(view_camera[0])+600;
Shaun Spalding explains this well in the following video:
https://www.youtube.com/watch?v=2iRKTFxT75Y&ab\_channel=ShaunSpalding

Learning GameMaker?
Code Create Play takes you through building a complete game step by step over seven days.