Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to make a pause game function?

Started by CopperGoat Jan 12, 2003 at 12:10 PM 6 replies 1.1k views
Original Post
CopperGoat
CopperGoat
I want to create a pause function for my snake game. For example, if the user presses the escape key, it would say quite? and if the person pressed y it would quit. and if the key is n, then it would go back to the game. how can i pause the game without losing information and then going back to it to make a pause function?
"There is such a need for a revolution, that revolutionaries themselves, cannot get to sleep thinking about other people who can't get to sleep" - Parham Mortazavi
CoiN
CoiN
Lo,

A simple (but a bit messy) way of doing it would be to use some type of pause flag, like:

if(!paused)
{
// Do normal game stuff here
// Check for esc and set pause flag if pressed
}
else if(paused)
{
// Display Pause Dialog
// Loop here until user enters yes or no and unset pauseflag
}

I would normally use a switch statement, with all the different game states, e.g. LOADING, PLAYING, QUITTING, etc, etc.

Theres probably better ways though.

Hope it helped...

CoiN
cliffski
cliffski
I would do it the same way, an alternative is to have a frame rate throttle (a number saying how much time has elapsed between frames) used to control all activity in the game, when the games paused, you just force the elapsed time to be zero until its unpaused, thus ensuring nothing gets updated.
Krisc
Krisc
The problem with your code is that when you pause, it wont render everything. I plan to have a global boolean like them but no matter what, everything will be drawn, but the user will not and cannot interact with the game.
aggregate
aggregate
I usually store the current state of the game, to decide what needs updated or what specific thing we are in (menu, level, etc.). And then a boolean variable ''pause'' which can be used to fine tune a few parts, but mainly to decide whether the fancy "PAUSE" text needs to be rendered.
Prefect
Prefect
A nice and flexible way to implement pausing is via virtual time keeping. Basically, your game tracks the real time (as returned by OS functions) and a virtual time which tells you how much time has passed in the simulation.

During normal operation, you advance the virtual time by the frame time every frame - this is really straightforward.
When in pause mode, you simply stop updating the virtual time.

The nice thing is that you can easily add more "advanced" stuff using this approach. For example, you can easily implement a slow motion mode where virtual time is only advanced by frametime * 0.5 or something.

cu,
Prefect
Widelands - laid back, free software strategy
Mescalito
Mescalito
Personnaly to manage games (& applications) I use states system:

//first you have to define states
#define STATE_GAME_RUNNING 0
#define STATE_GAME_LOADING 1
#define STATE_GAME_PAUSED 2
#define STATE_GAME_SHUTDOWN 3
...

//Then you need to set a state variable like:

UINT Game_Current_Process = 0;


Once you''ve got it you build your functions that depends of it:

//something like:
//(this is a sample and need optimizing especially with 3d !)
HRESULT Render ()
{

switch(Game_Current_Process)
{
case STATE_GAME_RUNNING:
//...draw the scene
break;

case STATE_GAME_PAUSED:
//...show a simple PAUSE bitmap
break;

// add other states if you need .......

}

return DD_OK;
}

You can make this for function like rendering, mouse & keyboard input, animating objects & whatever your want...

hope it helps !
Rob Loach
Rob Loach
Mescalito - I like your state system.

There must be a better way to manage such a thing. And how is the main menu/ingame menu managed? Could it also use this state system? Hardcoding everything is such a pain. I would suggest making a "Game" class to opperate this. What would it contain though? And how would it be set up?

- Rob Loach
OverTech Technologies
----------
"I don''t know half of you half as well as I should like; And I like less than half of you half as well as you deserve."
- Bilbo Baggins
Rob Loach [Website] [Projects] [

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.