This is tutorial number 12 in the Pygame RPG series
A Health mechanism is essential in every game, whether it be a simple platformer game or an RPG styled one. One reason why it’s so important is because the “Game Over” part is directly linked to the Player’s health. It’s not (usually) possible for the “game over” screen to trigger until the player’s health is down to 0.
The actual code for a Health bar is actually pretty small, which we could have easily fit into any of the last 5 tutorials. However, we decided to go all the way and create a proper visual Health bar instead of printing out the current health to the console.
It also helps this way because the code for the Health bar requires alot of small additions in many places.
This is what our health bar looks like when it’s full.
Simple, but a very classic 2D heart kind of look perfect for our game.
Here we have 6 animations in total for the Health bar. We’ve decided upon giving the Player “5” health. The below 6 images represent the 6 different stages of the Health bar, from 0 health to 5 health.
health_ani = [pygame.image.load("heart0.jpg"), pygame.image.load("heart.png"), pygame.image.load("heart2.png"), pygame.image.load("heart3.png"), pygame.image.load("heart4.png"), pygame.image.load("heart5.png")]
You should watch the video at the end of this tutorial to see how the animations on the Health bar will work.
Modifying the Code
First thing to do is to add a health variable to the player class. Remember to add it in the
__init__() method, as the player must start with full (5) health.
self.health = 5
Next is to actually create a Class for the Health Bar and load in the initial image to be displayed (full health). We don’t have to create a Class for this, but it’s looks neat and fits with the rest of the Classes fairly nicely.
class HealthBar(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load("heart5.png") def render(self): displaysurface.blit(self.image, (10,10))
The second method,
render() the Health Bar at the (10, 10) coordinates which are located in the top left corner of the screen.
Next we initialize the HealthBar; remember to do it in the right location! (Outside the game loop)
health = HealthBar()
This is another modification we’ll be making to the Player class, this time in the
player_hit() function. This is also the main code and logic behind the Health bar.
def player_hit(self): if self.cooldown == False: self.cooldown = True # Enable the cooldown pygame.time.set_timer(hit_cooldown, 1000) # Resets cooldown in 1 second self.health = self.health - 1 health.image = health_ani[self.health] if self.health <= 0: self.kill() pygame.display.update()
Basically, if the Player is hit (if this function is called) then the health will decrease by one. Furthermore, the image that the Health Bar is displaying will be changed to the index of the new health. If the previous image was
health5 and the player was hit, the new image loaded will be
Lastly, there is a little check to see if the player health drops below 0. If it does, then the Player is immediately killed and the screen is updated to reflect this. The
display_update() function is an optional feature, used for when you want to update the screen immediately instead of waiting for the game loop.
Here is our final addition in the game loop. Basically, it’s not enough to call the
kill() method as the image will continue to be rendered every iteration of the game loop.
if player.health > 0: displaysurface.blit(player.image, player.rect) health.render()
The solution was to add a check on the render function of the player, preventing it from executing if health drops below 0. (Also be sure to add the render method for the Health Bar).
Try running the code for yourself now, or move ahead to our second Code Review where there will be a short clip showcasing the latest additions.
Or you can skip the code review at go ahead directly to our next tutorial in this series.
This marks the end of the Pygame RPG Tutorial – Health Bar. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.