Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Now that we have our `Core` class, we can modify our game project to use it. Do

Open the `Game1.cs` file and make the following changes:

[!code-csharp[](./snippets/game1.cs?highlight=4,8,10,22-25)]
[!code-csharp[](./snippets/game1.cs?highlight=4,8,10,22,24)]

The key changes made here are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ MonoGame uses a coordinate system where (0, 0) is at the screen's upper-left cor

To center content on the screen, we need to find the window's center point. We can access this using the [**Window.ClientBounds**](xref:Microsoft.Xna.Framework.GameWindow.ClientBounds) property from the [**Game**](xref:Microsoft.Xna.Framework.Game) class, which represents the rectangular bounds of the game window. [**Window.ClientBounds**](xref:Microsoft.Xna.Framework.GameWindow.ClientBounds) exposes both [**Width**](xref:Microsoft.Xna.Framework.Rectangle.Width) and [**Height**](xref:Microsoft.Xna.Framework.Rectangle.Height) properties for the window's dimensions in pixels. By dividing these dimensions in half, we can calculate the window's center coordinates. We can update our [**Draw**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch.Draw(Microsoft.Xna.Framework.Graphics.Texture2D,Microsoft.Xna.Framework.Rectangle,Microsoft.Xna.Framework.Color)) method to use this:

[!code-csharp[](./snippets/draw_center_wrong.cs?highlight=9-16)]
[!code-csharp[](./snippets/draw_center_wrong.cs?highlight=9-17)]

> [!TIP]
> In the example above, we multiply the [**Vector2**](xref:Microsoft.Xna.Framework.Vector2) created by `0.5f` to halve the values instead of dividing it by `2.0f`. If you are not used to seeing this, it might seem strange at first, but it is actually an optimization technique. CPUs are able to perform multiplication operations much faster than division operations and reading `* 0.5f` is easily understood to be the same thing as `/ 2.0f`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ The key changes to the `Core` class are:

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:

[!code-csharp[](./snippets/game1.cs?highlight=1,7,72,78,84,90,96,104,109,112,116,122,124-125,130,136,142,148)]
[!code-csharp[](./snippets/game1.cs?highlight=1,7,72,78,84,90,96,109,112,122,125,130,136,142,148)]

The key changes to the `Game1` class are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ If you run the game right now and move the slime around, you will notice a few i

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:

[!code-csharp[](./snippets/game1.cs?highlight=1,5,25-29,40-45,76-177,182-194,294-295)]
[!code-csharp[](./snippets/game1.cs?highlight=1,5,25-29,40-45,76-177,182-195,294-295)]

The key changes made here are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ This tilemap configuration creates a simple dungeon layout with walls around the

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:

[!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,177-179,301-302)]
[!code-csharp[](./snippets/game1.cs?highlight=31-35,46-61,80-82,109,111,125,118,120,142,145,147,150,153,156,158,161,177-178,301-302)]

The key changes to the `Game1` class include:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The key changes made here are:

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

[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,104-105,195-196,214-215,268-286)]
[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,104-105,195-196,214-215,268-287)]

> [!NOTE]
> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,11 @@ To pause the game, first we will create a method that makes the pause panel visi

Next, update the `CheckKeyboardInput` method so that when the escape key is pressed, instead of returning to the title scene, we now pause the game:

[!code-csharp[](./snippets/gamescene/checkkeyboardinput.cs?highlight=6-10)]
[!code-csharp[](./snippets/gamescene/checkkeyboardinput.cs?highlight=6-11)]

Finally, update the `CheckGamePadInput` method so that when the start button is pressed, it pauses the game:

[!code-csharp[](./snippets/gamescene/checkgamepadinput.cs?highlight=6-10)]
[!code-csharp[](./snippets/gamescene/checkgamepadinput.cs?highlight=6-11)]

#### Creating the Pause Panel

Expand Down