Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    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    (0)

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



This article has been view 5279 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)