How To: Pause a Game

Demonstrates how to add pause functionality to a game.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Adding Pause Functionality to a Game

Typically, there are two circumstances when you want to pause your game—at the request of the user, and when the Guide appears on the screen (obscuring the playfield). A user request could take several different forms. For example, the user might launch a menu, or the user might execute a specific keystroke. You have to decide for yourself how users can pause your game.

Checking for the presence of the Guide is more straightforward. You can just query the Guide.IsVisible property.

When the user requests a pause, you don't want to unpause until the user tells you to do so. When you pause for the Guide, you should unpause as soon as the Guide is dismissed.

Pausing your game often means more than just halting your simulation. It could also require pausing or muting any sounds that might be playing, halting controller vibrations, sending a network message, and so on. To handle those tasks, you need to declare a BeginPause and EndPause method. EndPause resumes anything that was halted by BeginPause.

To add pause functionality to a game

  1. Add a variable to track the pause state.

  2. Add a variable to track the state of the pause key.

  3. Add a variable to track if the pause is due to the Guide or to user action.

    private bool paused = false;
    private bool pauseKeyDown = false;
    private bool pausedForGuide = false;
  4. Add a BeginPause method to initiate a pause, setting the variables appropriately:

    private void BeginPause(bool UserInitiated)
        paused = true;
        pausedForGuide = !UserInitiated;
        //TODO: Pause audio playback
        //TODO: Pause controller vibration
  5. Add an EndPause method to resume from a paused state, resetting variables appropriately:

    private void EndPause()
        //TODO: Resume audio
        //TODO: Resume controller vibration
        pausedForGuide = false;
        paused = false;
  6. Add a function to poll the state of the pause key with Keyboard.GetState and KeyboardState.IsKeyDown.

    If the key has changed from down to up, toggle the pause state using BeginPause or EndPause.

    private void checkPauseKey(KeyboardState keyboardState, 
        GamePadState gamePadState)
        bool pauseKeyDownThisFrame = (keyboardState.IsKeyDown(Keys.P) ||
            (gamePadState.Buttons.Y == ButtonState.Pressed));
        // If key was not down before, but is down now, we toggle the
        // pause setting
        if (!pauseKeyDown && pauseKeyDownThisFrame)
            if (!paused)
        pauseKeyDown = pauseKeyDownThisFrame;
  7. Add a function to poll the state of the Guide.

    If the Guide is newly visible, call BeginPause.

    If the Guide is not visible, but the game was paused for the guide, call EndPause.

    private void checkPauseGuide()
        // Pause if the Guide is up
        if (!paused && Guide.IsVisible)
        // If we paused for the guide, unpause if the guide
        // went away
        else if (paused && pausedForGuide && !Guide.IsVisible)
  8. During Update, check to see if the user paused, or if the Guide is active.

    Add a conditional around any update code so it will be called only if the game is not paused. Be sure to call base.Update even if the simulation is paused.

    // Check to see if the user has paused or unpaused
    checkPauseKey(keyboardState, gamePadState);
    // If the user hasn't paused, Update normally
    if (!paused)

Bb195026.bp(en-us,XNAGameStudio.31).gifBest Practice

You might choose to implement a menu of options to display when a game is paused. This might include options to resume, purchase, save, or quit the game. In multiplayer games, pausing will usually disable local input without pausing the game for remote players.

Some situations in which you may want to pause the simulation update or input are when:

Community Additions