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())
{
/*
****************************************
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 pressed. If it is being pressed it sets paused
to false
. Add the highlighted code just after our other keyboard-handling code:
/*
****************************************
Handle the players input
****************************************
*/
if (Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}
// Start the game if (Keyboard::isKeyPressed(Keyboard::Return)) { paused = false; }
/*
****************************************
Update the scene
****************************************
*/
Now we have a bool
called paused
, which starts off true
but changes to false
when the player presses the Enter key. At this point, we have to make our game loop respond appropriately, based on whatever the current value of paused
might be.
This is how we will proceed. We will wrap the entire, update part of the code, including the code we wrote in the last chapter to move the bee and clouds, in an if
statement.
Notice that, in the next code, the if
block will only execute when paused
is not equal to true
. Or to put it another way, the game won't move/update when it is paused.
This is exactly what we want. Look carefully at the exact place to add the new if
statement and its corresponding opening and closing curly braces {...}
. If they are put in the wrong place, things will not work as expected.
Add the highlighted code to wrap the update part of the code, paying close attention to the context shown next. I have added ...
on a few lines to represent hidden code. Obviously the ...
is not real code and should not be added to the game. You can identify where to place the new code (highlighted), at the start and the end, by the un-highlighted code surrounding it:
/* **************************************** Update the scene **************************************** */ if (!paused) { // Measure time ... ... ... // Has the cloud reached the right hand edge of the screen? if (spriteCloud3.getPosition().x > 1920) { // Set it up ready to be a whole new cloud next frame cloud3Active = false; } } } // End if(!paused) /* **************************************** Draw the scene **************************************** */
Notice that, when you place the closing curly brace of the new if
block, Visual Studio neatly adjusts all the indenting, to keep the code tidy.
Now you can run the game and everything will be static until you press the Enter key. It is now possible to go about adding features to our game and we just need to remember when the player dies or runs out of time, we need to set paused
to true
.
In the previous chapter we had a first glimpse of C++ strings. We need to learn a bit more about them so we can implement the player's HUD.