Pause and start

How can we pause this game

Pausing the game depends on the rules you set in your code. Some systems allow to “pause” the game by freezing every component that is present in the game (for example, in Unity you could “pause” the game by using Time.timescale = 0;)

In the cpp fundamentals course I would recommend you to put every object that would be affected by a “playing” and “paused” state inside a boolean.

If you did the “isAlive” boolean you have the tool to work with a “isPaused” boolean. If the game’s “isPaused” is false then put all the Tick and gameplay functions/code that make the game be played.

When “isPaused” is true then the compiler will not access the game’s code and you won’t be able to control it. You can put other features when “isPaused” is true, for example, a menu screen.

“isPaused” could be controlled with a keyboard key. If that key is pressed then switch “isPaused” to its opposite value (try using isPaused = !isPaused to invert it).

Share any results if you want, have fun

Can you please provide code for this

The code setup is in three parts, so I’ll only show the parts that are relevant and in the context of Raylib.

first, we want to declare isPaused:
bool isPaused = false; //Pause State

Next, we want to create code that toggles our paused state base on a key press, like so:

     isPaused = !isPaused;

The above code is placed within our while loop. Next, we wrap any code we want to no longer “Tick” inside of an if statement like this:

    //Put tick code here

And those are the three components for implementing a pause function for a game! You’ll also want to do something when the game is paused to show the player the game is paused.


This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms