How To: Pause a Game
Demonstrates how to add pause functionality to a game.
The Complete Sample
The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.
To Add Pause Functionality to a Game
- Derive a class from Game.
- Add a variable to track the pause state.
- Add a variable to track the state of the pause key.
- 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.
- Add a conditional around any update code so it will be called only if the game is not paused. You should also pause when the Guide is active, as that will block user inputs from reaching the game.