Don’t you just hate dying in your game in one hit? I know I do. I like playing Mario just like the next guy, but having an actual health bar can go a long way depending on what kind of game you’re trying to create. Could you imagine playing Legend of Zelda games where you die in a single hit? That would probably be the most frustrating game ever unless you’re a “pro”. So, let’s learn how to remedy that situation by creating a health system in our game.
The Health System
The health system is a classic used in games like Zelda, Grand Theft Auto, Kingdom Hearts, and many others. It’s an alternative to the life system in some cases, which allows us as game developers to give the player more opportunities to make mistakes and different types of experiences. With that said, every system can be distilled into basic building blocks; for the health system that is numbers.
Health is just hit points; hit points are essentially a number used to represent how much damage a player or enemy can take until they’re considered dead. If you look at the graphic above, the health bar and hearts fluctuate based on the hit points the player has as they all use the same underlying health in different ways. Let’s look at the code piece by piece.
Let’s start with line 8; this init function creates the important properties that we’re evaluating in the tutorial (health and healthMaximum). These properties are the basic data structures used to represent all the forms of health representations.
Next, if you look at line 26, we start the process of drawing each representation within the draw function using the player as a parameter. In drawPlayerHealthMinMax on line 69, we simply print the player’s current health / maximum health. This is just a text representation that’s all. With that said, let’s get a little more complicated and look at line 81 where we start drawing player health in the form of a health bar.
Within the drawPlayerHealthBar function, we define how long the health bar should be(maxHealthBarWidth) and where it should be placed on the screen. The most important aspect here is line 84; here we use the same idea from drawPlayerHealthMinMax to get a percentage that can be used to adjust the width of the health bar to create the effect of player health being at different percentages. Not so hard is it? We’re just thinking about different ways to represent the same thing. Now, we have one more to go through my friend, the sweet Zelda Style hearts.
Zelda Style hearts are a more robust representation than the other two and require a bit more thought. If you haven’t played Zelda before, first, you definitely should and two, the hearts are broken into quarters. That means effectively a heart is worth 4 hit points. You might start to think, “How am I going to represent this one? This sounds complicated.” Well, I have a solution for you before you get extremely frustrated. You only need to represent one of the hearts differently at any point in time. Look at line 35 – 36; we create that differentiation by having full hearts and one remainder heart. Because we decided that heart is worth 4 hit points, we divide by 4. Because a heart is a complex shape, we have to create individual sprites for each one state (1/4, 2/4, 3/4, and full heart). After designing the sprites, we loop through each full heart and then add one more heart with a different sprite based on if it’s a 1/4, 2/4, or 3/4 in terms of the remainder. With that, you can now create Zelda Style hearts in your game.
When you get down to the barebones, all health representations in every game function the same way. In fact, a lot of the complex things we see in games are just numbers and formulas put together in such a way to create those beautiful pieces of work we call video games.
If you would like to try it out online, you can try it at this link and look at the code, in the code tab: Online Example
Awesome, now you have the tools to create your own styles of health or Zelda-like health bar. Stay tuned for more content coming soon! If you enjoy the content, subscribe and get updates on new blog posts that come out on Godot, Script-8, or Business concepts for Indie Game Developers.
Peace and have a good one!