Pausing and restarting the game
We will add code so that when the game is first run, it will be in a paused state. The player will then be able to press the Enter key to start the game. Then, the game will run until either the player gets squashed or runs out of time.
At this point, the game will pause once more and wait for the player to press Enter 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())
{
/*
****************************************
Handle the players input
****************************************
*/
Now, whenever the game is run, we have a variable, paused
, that will be true
.
Next, we will add another if
statement where the expression will check to see whether the Enter key is currently being...