Beginning C++ Game Programming
上QQ阅读APP看书,第一时间看更新

Pausing and restarting the game

As we work on this game over the next three chapters, the code will obviously get longer and longer. So, now seems like a good time to think ahead and add a little bit more structure to our code. We will add this structure so that we can pause and restart the game.

We will add code so that, when the game is run for the first time, 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 squished or runs out of time. At this point, the game will pause and wait for the player to press Enter so that they can restart the game.

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 paused variable that will be true.

Next, we will add another if statement where the expression will check whether the Enter key is currently being pressed. If it is being pressed, it sets paused to false. Add the following 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 that starts off true but changes to false when the player presses the Enter key. At this point, we must 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 for moving the bee and clouds, in an if statement.

In the following code, note that the if block will only execute when paused is not equal to true. Put another way, the game won't move/update when it is paused.

This is exactly what we want. Look carefully at the place where we added 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 following highlighted code to wrap the updated part of the code, paying close attention to the context that follows. I have added ... on a few lines to represent hidden code. Obviously, ... 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 unhighlighted 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

****************************************

*/

Note 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. We just need to remember that, when the player dies or runs out of time, we need to set paused to true.

In the previous chapter, we took our first look at C++ Strings. We need to learn a bit more about them so that we can implement the player's HUD.