In the previous lesson, we saved the High Score in a text file.
There is still one thing that doesn’t feel like a real game. As soon as the player touches the enemy, the game ends. Instead of ending the game immediately, let’s give the player 3 lives. Every collision with the enemy will reduce one life. When all lives are lost, the game will end.
This simple feature makes your game more fun and gives players another chance to improve their score.
What You’ll Build Today
After completing this lesson, your game will display:
Score : 8
High Score : 25
Lives : 3
If the player touches the enemy:
Lives : 2
After another collision:
Lives : 1
When the last life is lost:
GAME OVER
Step 1: Create a Lives Variable
Near the score variable, add:
lives = 3
The player starts every game with three lives.
Step 2: Display the Lives
After displaying the High Score, create another text.
lives_text = font.render(
"Lives : " + str(lives),
True,
(0,255,0)
)
Display it on the screen.
screen.blit(lives_text,(10,80))
Run the program.
You should now see:
Lives : 3
Step 3: Reduce One Life
Find the enemy collision code.
if player_rect.colliderect(enemy_rect):
pygame.mixer.music.stop()
gameover_sound.play()
game_over = True
Replace it with:
if player_rect.colliderect(enemy_rect):
lives -= 1
player_x = 350
player_y = 250
enemy_x = 0
Now the player loses one life instead of ending the game.
We also move the player back to the center so the collision doesn’t happen repeatedly.
Step 4: Check for Game Over
After reducing a life, add this code.
if lives <= 0:
pygame.mixer.music.stop()
gameover_sound.play()
game_over = True
Now the Game Over screen appears only when all lives are finished.
Complete Working Program
You do not need to rewrite your entire game.
Make these four changes to the program from Lesson 18.
1. Create the lives variable
lives = 3
2. Display the lives
lives_text = font.render(
"Lives : " + str(lives),
True,
(0,255,0)
)
screen.blit(lives_text,(10,80))
3. Update the enemy collision
if player_rect.colliderect(enemy_rect):
lives -= 1
player_x = 350
player_y = 250
enemy_x = 0
if lives <= 0:
pygame.mixer.music.stop()
gameover_sound.play()
game_over = True
4. Restart the game correctly
Inside your restart function, reset the lives.
lives = 3
Now run the game.
Touch the enemy.
Watch the Lives decrease one by one.
The Game Over screen appears only after the last life is lost.
Why Do We Move the Player?
After a collision, the player and enemy are still touching.
If we don’t move the player away, the game will immediately reduce another life.
That’s why we send the player back to the center.
player_x = 350
player_y = 250
This gives the player a fresh chance.
Common Errors
Lives become negative
Check that you stop the game when:
lives <= 0
The player loses all lives instantly
Move the player away from the enemy after every collision.
Lives are not visible
Make sure you added:
screen.blit(lives_text,(10,80))
before:
pygame.display.update()
Practice Challenge
Challenge 1
Start the game with 5 lives instead of 3.
Challenge 2
Display the lives in red when only one life is left.
Challenge 3
Instead of writing Lives : 3, display three heart images.
(Hint: We’ll learn image-based UI in a later lesson.)
Teacher’s Tip
Professional games rarely end after the first mistake.
Giving players a few lives makes the game more enjoyable and encourages them to keep trying.
As a beginner game developer, always think about the player’s experience, not just the code.
What You Learned Today
Today you learned:
- How to add lives in Pygame.
- How to reduce lives after a collision.
- How to display lives on the screen.
- How to end the game only after all lives are lost.
Frequently Asked Questions
How do I add lives in Pygame?
Create a variable such as lives = 3 and reduce it whenever the player touches an enemy.
How do I display lives in Pygame?
Create text using font.render() and display it with screen.blit().
Why does my player lose all lives at once?
Move the player to a safe position immediately after a collision so the same collision isn’t detected again.
Can I use heart icons instead of numbers?
Yes. Many games use heart images to represent the player’s lives.
How many lives should a beginner game have?
Three lives are a good starting point because they make the game challenging without being frustrating.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.
