Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

A Simple Way to Pause Your Game
Date 12/21/2008    Tags XNA    (4)

Pausing your game is a nice feature to give the user (and this idea can be expanded to include in-game menus) and it turns out to be a very easy to implement feature too.

First, we will add an enumerator list with the items Normal and Paused.

enum GameStates
{
    Normal,
    Paused,
}
You then need to create a variable that will store these values.

GameStates GameState;
In the update method of your game loop, you will need some way to toggle between the two states (Normal and Paused). We will use the "P" key for this example. In addition, we will add some extra code to make sure that at least a little time has passed before a "P" key press can be registered a second time. If you don't include this code, your game will pause/unpause multiple times with each key press.
float KeyPressCheckDelay = 0.25f;
float TotalElapsedTime = 0;

protected override void Update(GameTime gameTime)
{
    // Needed for keyboard events
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    TotalElapsedTime += elapsed;
    ksKeyboardState = Keyboard.GetState();

    // Normal game loop
    if (GameState == GameStates.Normal)
    {
    // Do your normal game logic here.

        if (TotalElapsedTime >= KeyPressCheckDelay)
        {
            // Pause the current game
            if (ksKeyboardState.IsKeyDown(Keys.P))
            {
                GameState = GameStates.Paused;
                fTotalElapsedTime = 0.0f;
            }
        }
    }
    // Game is paused.
    else
    {
        if (TotalElapsedTime >= KeyPressCheckDelay)
        {
            // UnPause the current game
            if (ksKeyboardState.IsKeyDown(Keys.P))
            {
                GameState = GameStates.Normal;
                fTotalElapsedTime = 0.0f;
            }
            
            // Do any other fun stuff you want to do while the game is paused.

        }
    }

    base.Update(gameTime);
}
Since none of your game variables are being updated, you don't have to change anything in the draw method for this to work. Your game will continue to draw the scene exactly as it was when it was paused. On the game I am developing now, this creates a neat effect as enemies still animate but do not move.

You could easily add a check to the draw method in the event that you wanted to do something special when paused, like create an in-game menu. You could use this GameState enum for other things as well, like splash screens, cut scenes, and credits.

kick it on DotNetKicks.com


Wellplayed.net
This article has been view 94 times.

Advertisement:

Comments

George Clingerman

Avatar

1/5/2009 6:39:31 AM

Instead of adding in a key press delay. A more traditional method is to store the previous state of the keyboard. Then when checking to see if the P key is pressed, also check to make sure it was previously NOT pressed, i.e. released in that previous game state variable (lots of samples out there for that).

Makes for a bit cleaner code and doesn't create edge cases where someone is really quick to hit un-pause again but the game doesn't un-pause.

Hope that helps, awesome you're giving back to the community! I really got excited seeing a new XNA article out there.go.
Kris

Avatar

1/5/2009 8:27:58 AM

Thanks George, I may change my game (and possibly this tutorial) to do just that. I'm running into issues now when the user clicks start to begin the game, it gets paused right away and I think doing just what you said should be able to resolve this.

I should thank you for giving back to the community too, your Wizard tutorials were some of the first ones that I went through when I was learning all of this. I plan to write much more myself as I get more involved in XNA development, so this won't be the last you've heard from me by a long shot.i
jax26

Avatar

6/5/2009 12:01:19 PM

Hi,

thanks for this super way to pause the game. it works ;)

ive been trying to make this work with my menu so that when i press "p" while playing the game for it to show a player menu which has multiple selectable options.

Right now it shows the menu which is handled by an switch statement yet the selections dont happen coz the drawing code dosnt seem to be called. could u please tell a way for this to work.

kind regards,
Jaxt
jax26

Avatar

6/5/2009 12:12:27 PM

Hi,

thanks for this super way to pause the game. it works ;)

ive been trying to make this work with my menu so that when i press "p" while playing the game for it to show a player menu which has multiple selectable options.

Right now it shows the menu which is handled by an switch statement yet the selections dont happen coz the drawing code dosnt seem to be called. could u please tell a way for this to work.

kind regards,
Jaxt


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.