Sign in to follow this  
eektor

How do you pause a game?

Recommended Posts

Hi, I am trying to implement a pause for my pong game. I was wondering how you go about doing it. I tried already and I've been having problems. Anyways this is an abstraction of my game loop right now
While (user hasn’t quit)

{    Read input

     If user quits,
	Quit game

     Handle user input

     Computer AI

     Check Collisions

     Check for scoring

     Update positions on sprites

     Render sprites

     Control Framerate
}
What I've been trying to do to make a pause is this:
While (user hasn’t quit)

{    do 
        Read input

        If user quits,
	   Quit game
 
        Handle user input
     while ( game is paused and user hasn't quit)

     Computer AI

     Check Collisions

     Check for scoring

     Update positions on sprites

     Render sprites

     Control Framerate
}
Is this the right direction? or am I going about this wrong?

Share this post


Link to post
Share on other sites
Try something like -

While (user hasn’t quit)

{ Read input

If user quits,
Quit game

Check to see if game pause state has updated

if ( currently unpaused ) {

Handle user input

Computer AI

Check Collisions

Check for scoring

Update positions on sprites

Render sprites

}

Control Framerate
}


Share this post


Link to post
Share on other sites
You could also implement a state system to manage all of your different game states:


class State
{
Enter()
Exit()
Update()
Draw()
ReadInput()
}

class Pause : State
{
Draw()
{
Print on Screen ("Pausued!")
}
Update()
{
check if player presses ESC to set currentState to InGame state
}
}

class InGame : State
{
Update()
{
Move character and enemies
check if player presses ESC to set currentState to pause state
}
Draw()
{
Draw character and enemies
}
}

State currentState

While (user hasn’t quit)
{
currentState.ReadInput()
currentState.Update()
currentState.Draw()
Control Framerate
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
Try something like -

While (user hasn’t quit)

{ Read input

If user quits,
Quit game

Check to see if game pause state has updated

if ( currently unpaused ) {

Handle user input

Computer AI

Check Collisions

Check for scoring

Update positions on sprites

Render sprites

}

Control Framerate
}




Might want to still render sprites and handle input out side of the if block

Share this post


Link to post
Share on other sites
so, you have the logic part, read input, update states, AI, etc, and the drawing part. When you pause the game, don't execute the logic part except reading input of course.

Share this post


Link to post
Share on other sites
In a pong game you only need to stop the player/computer paddle and the boll movement. Let's say you have a bool variable named "started". Make if statements in the movement code of the ball and the paddles. When you want to pause/unpause the game, you can do something like this:

// input code
if (keydown(P)) {
if (started)
started=false;
else
started=true;
}

Where you replace keydown(P) with the function you use for getting input.

Share this post


Link to post
Share on other sites
Thanks for the input.

@Rob Loach I think that is something I would like to do but in my next project. Thanks a lot, you gave me a good starting point to set up a state system manager. Is there a book or a tutorial that talks more about managing game states? Right now all I have for game states are Menu, Instructions, and Game.

@Mushu Thanks, I originally had the check to see if it was paused in the handle keyboard input, but I think its best to just separate it.

@Grain Why would I want to render sprites and handle input outside of the if block? This is for a simple pong game.

@password I have something similar. I have a bool variable named quit and one named pause.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by eektor
Thanks for the input.

@Rob Loach I think that is something I would like to do but in my next project. Thanks a lot, you gave me a good starting point to set up a state system manager. Is there a book or a tutorial that talks more about managing game states? Right now all I have for game states are Menu, Instructions, and Game.

@Mushu Thanks, I originally had the check to see if it was paused in the handle keyboard input, but I think its best to just separate it.

@Grain Why would I want to render sprites and handle input outside of the if block? This is for a simple pong game.

@password I have something similar. I have a bool variable named quit and one named pause.


you can make some cool effects while its paused :) (scroll the pause text or something).

as for handling input, well you want to handle input even if the game is paused (since you would want to be able to detect when the player unpauses)

Share this post


Link to post
Share on other sites
Also,
you might want to render outside the ifblock because what if you want the player to see the paddle and ball still? I did that when I made my pong clone (about a year ago now...lol).



Chad

Share this post


Link to post
Share on other sites
Quote:
Original post by eektor
Is there a book or a tutorial that talks more about managing game states? Right now all I have for game states are Menu, Instructions, and Game.


You should check out Managing Game States in C++, i think it's quite a good tutorial about that topic.

-Raven

Share this post


Link to post
Share on other sites
Well i'm a newb, but the way I do it... When ur collecting input if they press say Space, just call a function that doesn't return until they hit space again. Since everyone else has a lot more complicated ways mine is prolly newbish and has a problem... but I don't see what, does everything I need it 2.

Share this post


Link to post
Share on other sites
If you want to stop some behaviour from happening (e.g. the ball moving) then testing a flag inside the movement code itself is less efficient than simply not calling the movement function in the first place. It's also messier.

If you were to implement time-based movement (so that the ball moves at the same rate regardless of framerate) then you could also approach this by simply telling the ball that no time has passed. You literally 'stop the clock.'

Share this post


Link to post
Share on other sites
I can vouch for the link RavenMokel posted, using a stack-oriented state machine confused the life out of me until I saw this link in another post. Very simple examples and source as well as a good example of polymorphism at work.

Share this post


Link to post
Share on other sites
Thank you Raven for the link. I might get the books listed as resources too. I got the first one already.

Thank you superpig you just made me realize an error. I was wondering sometimes when it was paused once I unpaused it someone will score and the ball starts at the middle again. I forgot I didn't stop the timer and the ball's movement is time-based.

@Cleric Kain I was thinking of doing that but I wanted to give the user the option to quit at any time and not have to unpause then quit.

Share this post


Link to post
Share on other sites
Ok, I think I got the pause working alright except for one bug. If a person press p sometimes it stops like its supposed to but other times it pauses and then resumes. I believe somehow the program reads the p key being pressed down more than once when you only pressed it down once.

How can I avoid this? I use SDL and I was trying to put a SDL_Delay like this:


// If user presses 'p' pause or unpause the game
if (g_input->getInput(SDLK_p))
{
if (g_pause == false)
g_pause = true;
else
g_pause = false;

SDL_Delay(75);
}


I've been trying to vary the SDL_Delay as well but with no success. Any ideas?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this