Book Image

Beginning C++ Game Programming - Second Edition

By : John Horton
Book Image

Beginning C++ Game Programming - Second Edition

By: John Horton

Overview of this book

The second edition of Beginning C++ Game Programming is updated and improved to include the latest features of Visual Studio 2019, SFML, and modern C++ programming techniques. With this book, you’ll get a fun introduction to game programming by building five fully playable games of increasing complexity. You’ll learn to build clones of popular games such as Timberman, Pong, a Zombie survival shooter, a coop puzzle platformer and Space Invaders. The book starts by covering the basics of programming. You’ll study key C++ topics, such as object-oriented programming (OOP) and C++ pointers, and get acquainted with the Standard Template Library (STL). The book helps you learn about collision detection techniques and game physics by building a Pong game. As you build games, you’ll also learn exciting game programming concepts such as particle effects, directional sound (spatialization), OpenGL programmable shaders, spawning objects, and much more. Finally, you’ll explore game design patterns to enhance your C++ game programming skills. By the end of the book, you’ll have gained the knowledge you need to build your own games with exciting features from scratch
Table of Contents (25 chapters)
23
Chapter 23: Before You Go...

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())
{
    /*
 ...