Skip to content

Commit 14acbb1

Browse files
Remove exiting from Game1 class and make ExitOnEscape true by default in the core class (#163)
1 parent 760a567 commit 14acbb1

File tree

16 files changed

+21
-29
lines changed

16 files changed

+21
-29
lines changed

articles/tutorials/building_2d_games/11_input_management/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,23 +366,24 @@ Now that we have our input management system complete, we will update our game t
366366

367367
The `Core` class serves as our base game class, so we will update it to add and expose the `InputManager` globally. Open the *Core.cs* file in the *MonoGameLibrary* project and update it to the following:
368368

369-
[!code-csharp[](./snippets/core.cs?highlight=5-6,39-47,103-104,107-118)]
369+
[!code-csharp[](./snippets/core.cs?highlight=5-6,39-47,91-92,106–107,110–121)]
370370

371371
The key changes to the `Core` class are:
372372

373373
1. Added the `using MonoGameLibrary.Input;` directive to access the `InputManager` class.
374374
2. Added a static `Input` property to provide global access to the input manager.
375375
3. Added a static `ExitOnEscape` property to set whether the game should exit when the Escape key on the keyboard is pressed.
376-
4. In `Initialize` the input manager is created.
377-
5. Added an override for the `Update` method where:
376+
4. In the `Core` constructor, `ExitOnEscape` is set to true by default to mirror how the default MonoGame `Game1` class template has this functionality by default.
377+
5. In `Initialize` the input manager is created.
378+
6. Added an override for the `Update` method where:
378379
1. The input manager is updated
379380
2. A check is made to see if `ExitOnEscape` is true and if the Escape keyboard key is pressed.
380381

381382
### Updating the Game1 Class
382383

383384
Now we can update our `Game1` class to use the new input management system through the `Core` class. Open `Game1.cs` in the game project and update it to the following:
384385

385-
[!code-csharp[](./snippets/game1.cs?highlight=1,7,75,81,87,93,99,107,112,115,119,125,127-128,133,139,145,151)]
386+
[!code-csharp[](./snippets/game1.cs?highlight=1,7,73,79,85,91,97,105,110,113,117,123,125-126,131,137,143,149)]
386387

387388
The key changes to the `Game1` class are:
388389

articles/tutorials/building_2d_games/11_input_management/snippets/core.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public Core(string title, int width, int height, bool fullScreen)
8787

8888
// Mouse is visible by default.
8989
IsMouseVisible = true;
90+
91+
// Exit on escape is true by default
92+
ExitOnEscape = true;
9093
}
9194

9295
protected override void Initialize()

articles/tutorials/building_2d_games/11_input_management/snippets/game1.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ protected override void LoadContent()
5050

5151
protected override void Update(GameTime gameTime)
5252
{
53-
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
54-
Exit();
55-
5653
// Update the slime animated sprite.
5754
_slime.Update(gameTime);
5855

articles/tutorials/building_2d_games/12_collision_detection/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ If you run the game right now and move the slime around, you will notice a few i
311311

312312
We can now implement these features using collision detection and response in our game. In the *DungeonSlime* project (your main game project), open the `Game1.cs` file and make the following changes to the `Game1` class:
313313

314-
[!code-csharp[](./snippets/game1.cs?highlight=1,5,25-29,40-45,79-179,184-196,296-297)]
314+
[!code-csharp[](./snippets/game1.cs?highlight=1,5,25-29,40-45,76–176,181–193,293–294)]
315315

316316
The key changes made here are:
317317

articles/tutorials/building_2d_games/12_collision_detection/snippets/game1.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ protected override void LoadContent()
6161

6262
protected override void Update(GameTime gameTime)
6363
{
64-
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
65-
Exit();
66-
6764
// Update the slime animated sprite.
6865
_slime.Update(gameTime);
6966

articles/tutorials/building_2d_games/13_working_with_tilemaps/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ This tilemap configuration creates a simple dungeon layout with walls around the
186186

187187
With all of the assets now in place and configured, we can update the `Game1` class to load the tilemap and draw it. We will also need to update the collision logic so that the boundary is no longer the edge of the screen, but instead the edges of the wall tiles of the tilemap. Open `Game1.cs` and make the following updates:
188188

189-
[!code-csharp[](./snippets/game1.cs?highlight=31-35,46-61,80-82,112,114,116,128,121,123,125,127,145,148,150,153,156,159,161,164,179-181,303-304)]
189+
[!code-csharp[](./snippets/game1.cs?highlight=31-35,46-61,80-82,109,111,113,125,118,120,122,124,142,145,147,150,153,156,158,161,176–178,300–301)]
190190

191191
The key changes to the `Game1` class include:
192192

articles/tutorials/building_2d_games/13_working_with_tilemaps/snippets/game1.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ protected override void LoadContent()
8484

8585
protected override void Update(GameTime gameTime)
8686
{
87-
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
88-
Exit();
89-
9087
// Update the slime animated sprite.
9188
_slime.Update(gameTime);
9289

articles/tutorials/building_2d_games/14_soundeffects_and_music/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Add these files to your content project using the MGCB Editor:
175175

176176
Next, open the `Game1.cs` file and update it to the following:
177177

178-
[!code-csharp[](./snippets/game1.cs?highlight=3,6,39-43,92-111,203-204,222-223)]
178+
[!code-csharp[](./snippets/game1.cs?highlight=3,6,39-43,92-111,200-201,219-220)]
179179

180180
The key changes here are:
181181

articles/tutorials/building_2d_games/14_soundeffects_and_music/snippets/game1.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ protected override void LoadContent()
113113

114114
protected override void Update(GameTime gameTime)
115115
{
116-
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
117-
Exit();
118-
119116
// Update the slime animated sprite.
120117
_slime.Update(gameTime);
121118

articles/tutorials/building_2d_games/15_audio_controller/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Now that we have the audio controller class complete, we can update the game to
117117

118118
The `Core` class serves as our base game class, so we will update it first to add and expose the `AudioController` globally. Open the `Core.cs` file in the *MonoGameLibrary* project and update it to the following:
119119

120-
[!code-csharp[](./snippets/core.cs?highlight=6,50-53,112-113,116-122,129-130)]
120+
[!code-csharp[](./snippets/core.cs?highlight=6,50-53,115–116,119–125,132–133)]
121121

122122
The key changes made here are:
123123

@@ -131,7 +131,7 @@ The key changes made here are:
131131

132132
Next, update the `Game1` class to use the audio controller for audio playback. Open `Game1.cs` and make the following updates:
133133

134-
[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,104-105,197-198,216-217,270-288)]
134+
[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,104-105,194–195,213–214,267–285)]
135135

136136
> [!NOTE]
137137
> Note there were a lot of replacements in the `LoadContent` method, switching from loading and initializing the background Song and replacing it with a call to the new `AudioController` to do all the work managing the Song reference. Much cleaner.

0 commit comments

Comments
 (0)