How do you pause a game?

Started by
15 comments, last by eektor 17 years, 8 months ago
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?
Advertisement
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}
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 currentStateWhile (user hasn’t quit){     currentState.ReadInput()     currentState.Update()     currentState.Draw()     Control Framerate}
Rob Loach [Website] [Projects] [Contact]
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
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.
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.
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.
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)
Good point.
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

This topic is closed to new replies.

Advertisement