In this Restart Game in Pygame tutorial, you will learn how to restart your game after the Game Over screen appears.
At the end of the previous lesson, the game stopped when the player touched the enemy. The only way to play again was to close the game window and run the program again. That is not how real games work. Most games have a Play Again option that lets players start a new game instantly. Today, we will build the same feature.
By the end of this lesson, you will be able to press the R key to restart the game without closing the window.
What Will You Build Today?
After completing this lesson, your game will have:
- ✅ Background image
- ✅ Player movement
- ✅ Moving enemy
- ✅ Coin collection
- ✅ Score counter
- ✅ Game Over screen
- ✅ Restart Game using the R key
Why Do We Need a Restart Feature?
Imagine you are playing a game and you lose after collecting 12 coins. Would you like to:
❌ Close the game
❌ Open the program again
or simply press one key?
Most players prefer the second option. That is why almost every game includes a restart feature.
Step 1: Create a Restart Function
Instead of writing the same code many times, we will create a function.
Add this before the game loop.
def restart_game():
global player_x, player_y
global enemy_x, enemy_y
global coin_x, coin_y
global score
global game_over
player_x = 350
player_y = 250
enemy_x = 0
enemy_y = 100
coin_x = random.randint(0,736)
coin_y = random.randint(0,536)
score = 0
game_over = False
pygame.mixer.music.play(-1)
This function resets everything to its starting position.
Understanding restart_game()
This function performs six important tasks.
It:
- Moves the player back to the center.
- Moves the enemy to the starting position.
- Places the coin at a new random location.
- Resets the score to 0.
- Removes the Game Over screen.
- Starts the background music again.
Instead of writing all this code again, we simply call one function.
Step 2: Detect the R Key
Inside the event loop, add:
if event.type == pygame.KEYDOWN:
if game_over:
if event.key == pygame.K_r:
restart_game()
Now the game waits for the player to press the R key.
Step 3: Show a Restart Message
Inside the Game Over screen, create another text.
restart_text = font.render(
"Press R to Play Again",
True,
(255,255,255)
)
Display it.
screen.blit(restart_text,(250,340))
Now the player knows what to do.
Complete Program
Instead of pasting another 200+ lines of code with only a few changes, this lesson should build directly on Lesson 15. Keep the complete code from Lesson 15 and make these additions:
1. Add the restart_game() function before the game loop.
2. Add the keyboard event:
if event.type == pygame.KEYDOWN:
if game_over:
if event.key == pygame.K_r:
restart_game()
3. Add the restart message:
restart_text = font.render(
"Press R to Play Again",
True,
(255,255,255)
)
screen.blit(restart_text,(250,340))
These are the only new changes. Everything else remains exactly the same as Lesson 15.
Teacher’s Tip: Professional programmers avoid copying hundreds of unchanged lines into every lesson. They focus on the new concept and clearly highlight only what changed. This also makes it easier for students to understand and debug their code.
How the Restart Feature Works
Let’s understand the process.
- The player touches the enemy.
game_overbecomesTrue.- The Game Over screen appears.
- The player presses the R key.
restart_game()is called.- The player, enemy, coin, and score return to their starting values.
- The game begins again.
This is the same idea used in many arcade games.
Mini Project
Play your game.
Try to:
- Collect 5 coins.
- Touch the enemy.
- Press R.
- Start playing again.
If everything works, congratulations! You have built your first replayable game.
Practice Challenge
Challenge 1
Change the restart key from R to Spacebar.
Hint:
pygame.K_SPACE
Challenge 2
Display:
Press SPACE to Play Again
instead of the R key.
Challenge 3
Keep track of the Highest Score.
Even after restarting, the highest score should remain.
(Hint: Create a new variable called high_score.)
Common Mistakes
The game does not restart
Check that the restart_game() function is being called when the R key is pressed.
The score is not resetting
Make sure the function contains:
score = 0
The coin stays in the same place
Generate a new random position using random.randint() inside the restart function.
Background music does not start again
Call:
pygame.mixer.music.play(-1)
inside the restart function.
What You Learned Today
Today you learned:
- How to restart a game in Pygame
- How to reset game variables
- How to use a restart function
- How to detect the R key
- How real games allow players to play again
Quick Revision
- Why do we create a
restart_game()function? - Which key restarts the game?
- Which variables should be reset when the game starts again?
- Why do we keep all restart code in one function?
Frequently Asked Questions
How do I restart a game in Pygame?
Create a restart function that resets the player’s position, enemy position, score, and other game variables. Call the function when the player presses a restart key.
How do I restart a game after Game Over in Pygame?
Detect a key press, such as R, and reset all gameplay variables before starting the game again.
What is the best way to reset a Pygame game?
The best practice is to create a dedicated restart function instead of rewriting the reset code in multiple places.
Can I create a Play Again button in Pygame?
Yes. You can restart the game using a keyboard key or create a clickable button with mouse events.
Should I reset the high score when restarting?
No. Most games keep the highest score even after restarting. Only the current score is reset.
Why do professional games use restart functions?
A restart function keeps the code clean, avoids repetition, and makes future updates much easier.
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.
