In this final chapter we will add a pause and resume button to our game. Let’s start off by adding the images to the content project.


These images can be loaded into the content pipeline from the LoadGame() method. Adjust it so it looks like this:
1: void LoadGame()
2: {
3: //load the game images into the content pipeline
4: orb = Content.Load<Texture2D>(@"orb");
5: pauseButton = Content.Load<Texture2D>(@"pause");
6: resumeButton = Content.Load<Texture2D>(@"resume");
7: resumeButtonPosition = new Vector2((GraphicsDevice.Viewport.Width / 2) -
8: (resumeButton.Width / 2),
9: (GraphicsDevice.Viewport.Height / 2)-(resumeButton.Height / 2));
10:
11: //set the position of the orb in the middle of the gamewindow
12: orbPosition = new Vector2((GraphicsDevice.Viewport.Width / 2) - (OrbWidth / 2),
13: (GraphicsDevice.Viewport.Height / 2) - (OrbHeight / 2));
14:
15: //since this will go to fast for this demo's purpose, wait for 3 seconds
16: Thread.Sleep(3000);
17:
18: //start playing
19: gameState = GameState.Playing;
20: isLoading = false;
21: }
Next is the Draw() method, make it look like this:
1: protected override void Draw(GameTime gameTime)
2: {
3: GraphicsDevice.Clear(Color.CornflowerBlue);
4:
5: spriteBatch.Begin();
6:
7: //draw the start menu
8: if (gameState == GameState.StartMenu)
9: {
10: spriteBatch.Draw(startButton, startButtonPosition, Color.White);
11: spriteBatch.Draw(exitButton, exitButtonPosition, Color.White);
12: }
13:
14: //show the loading screen when needed
15: if (gameState == GameState.Loading)
16: {
17: spriteBatch.Draw(loadingScreen, new Vector2((GraphicsDevice.Viewport.Width / 2) -
18: (loadingScreen.Width / 2), (GraphicsDevice.Viewport.Height / 2) -
19: (loadingScreen.Height / 2)), Color.YellowGreen);
20: }
21:
22: //draw the the game when playing
23: if (gameState == GameState.Playing)
24: {
25: //orb
26: spriteBatch.Draw(orb, orbPosition, Color.White);
27:
28: //pause button
29: spriteBatch.Draw(pauseButton, new Vector2(0, 0), Color.White);
30: }
31:
32: //draw the pause screen
33: if (gameState == GameState.Paused)
34: {
35: spriteBatch.Draw(resumeButton, resumeButtonPosition, Color.White);
36: }
37:
38: spriteBatch.End();
39:
40: base.Draw(gameTime);
41: }
The Update() method doesn’t need any change because it already checks for mouse click events, so the only thing left is to adjust the MouseClick() method.
1: void MouseClicked(int x, int y)
2: {
3: //creates a rectangle of 10x10 around the place where the mouse was clicked
4: Rectangle mouseClickRect = new Rectangle(x, y, 10, 10);
5:
6: //check the startmenu
7: if (gameState == GameState.StartMenu)
8: {
9: Rectangle startButtonRect = new Rectangle((int)startButtonPosition.X,
10: (int)startButtonPosition.Y, 100, 20);
11: Rectangle exitButtonRect = new Rectangle((int)exitButtonPosition.X,
12: (int)exitButtonPosition.Y, 100, 20);
13:
14: if (mouseClickRect.Intersects(startButtonRect)) //player clicked start button
15: {
16: gameState = GameState.Loading;
17: isLoading = false;
18: }
19: else if (mouseClickRect.Intersects(exitButtonRect)) //player clicked exit button
20: {
21: Exit();
22: }
23: }
24:
25: //check the pausebutton
26: if (gameState == GameState.Playing)
27: {
28: Rectangle pauseButtonRect = new Rectangle(0, 0, 70, 70);
29:
30: if (mouseClickRect.Intersects(pauseButtonRect))
31: {
32: gameState = GameState.Paused;
33: }
34: }
35:
36: //check the resumebutton
37: if (gameState == GameState.Paused)
38: {
39: Rectangle resumeButtonRect = new Rectangle((int)resumeButtonPosition.X,
40: (int)resumeButtonPosition.Y, 100, 20);
41:
42: if (mouseClickRect.Intersects(resumeButtonRect))
43: {
44: gameState = GameState.Playing;
45: }
46: }
47: }
And that’s about it, we’ve just created a game that uses states to determine if it should pause, play or show the main menu. For my complete solution you can click the link below and feel free to post comments if you have any questions.
Thank you for reading!
Source Code