Pausing and restarting the game
As we progress with this game over the next three chapters, the code will obviously get longer and longer. So, now it seems like a good time to think ahead and add a little bit more structure into our code. We will add this structure to give us the ability to pause and restart the game.
We will add code so that when the game is first run, it will be paused. The player will be able to press the Enter key to get the game started. Then the game will run until either the player gets squished or runs out of time. At this point the game will pause and wait for the player to press Enter key, to restart again.
Let's step through setting this up a bit at a time. First, declare a new bool
variable called paused
, outside the main game loop, and initialize it to true
:
// Variables to control time itself
Clock clock;
// Track whether the game is running
bool paused = true;
while (window.isOpen())
{
/*
**************************************** ...