As you progress with this game over the next three sections, the code gets longer and longer. So, now it seems like a good time to think ahead and add a little bit more structure into your code. Add this structure to give yourself the ability to pause and
- [Instructor] Hi, and welcome to section three, C++ Strings, SFML time, player input, and HUD. In this section, we will start with pausing and restarting the game. We will then move on to C++ strings, SFML text, and SFML font classes. Later, we will take a look at adding a HUD to Timber, and finally, we will learn adding a time bar to Timber. Now we move on to the first video of this section that deals with pausing and restarting the game.
In this video, we are going to add a little bit more structure in our code. We will add this structure to give us the ability to pause and restart the game. As we progress with this game over the next three sections the code will obviously get longer and longer. 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 and get the game started. 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. 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 says paused to false.
Add this highlighted code just after other keyboard handling code. 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. Now, we will wrap the entire update part of the code, including the code we wrote in the last section to move the B in clouds in an if statement.
You can see I have highlighted entire update the scene part. Notice that in the next code the if block will only execute when paused is equal to true, or to put it in another way, the game won't move or update when it is paused. This exactly what we want. Look carefully at the exact place to add the new if statement and its corresponding opening and closing curly braces. Here, I have place opening curly brace, and here is the closing curly brace. If they are put in the wrong place, things will not work as expected.
You have to add this highlighted code to wrap the update part of the code. You can identify where to place the new code at the start and the end by the unhighlighted code surrounding it. I'll show you once again. This is the end, and this is the start. 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 by clicking the local windows debugger button.
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 pause to true. Awesome! In this video, we have learned pausing and restarting the game. Cool! We need to learn a bit more about C++ strings so we can implement the player's HUD. We'll take a look at them in the next video.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Creating the main game loop
- Drawing the game background
- Handling errors
- Manipulating C++ variables
- Randomizing numbers
- Making decisions with if, else, and switch
- Moving game objects
- Pausing and restarting games
- Adding scoring and messages
- Using class enumerations and arrays
- Adding sound effects
- Object-oriented programming in C++
- C++ references
- Texture management
- Collision detection
- Implementing the HUD
- Finishing the game