Skip to content

Commit 12095dc

Browse files
committed
Restructure chapters
Make chapters consistent in that they now introduce the concepts and then update the game code at the end instead of gradually updating game code while introducing concepts. This also allows for the full game1.cs file to be displayed at the end of the chapter for reference.
1 parent 56ee949 commit 12095dc

File tree

114 files changed

+5461
-3895
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+5461
-3895
lines changed

articles/tutorials/building_2d_games/01_what_is_monogame/index.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,15 @@ Regardless of which .NET language used, developers should have a foundational un
6969

7070
1. Name one of the advantages of using the MonoGame framework to develop games.
7171

72-
<details>
73-
<summary>Question 1 Answer</summary>
74-
75-
> Any of the following are advantages of using the MonoGame
76-
> 1. It provides cross-platform support, allowing developers to target multiple platforms from a single code base.
77-
>
78-
> 2. It offers a set of libraries and APIs common for game development tasks, such as graphics rendering, input handling, audio, and content management
79-
>
80-
> 3. It is a "bring your own tools" framework, giving developers flexibility in their working environment.
81-
82-
</details><br />
72+
:::question-answer
73+
Any of the following are advantages of using the MonoGame
74+
1. It provides cross-platform support, allowing developers to target multiple platforms from a single code base.
75+
2. It offers a set of libraries and APIs common for game development tasks, such as graphics rendering, input handling, audio, and content management
76+
3. It is a "bring your own tools" framework, giving developers flexibility in their working environment.
77+
:::
8378

8479
2. What programming languages can be used when creating a game with MonoGame?
8580

86-
<details>
87-
<summary>Question 2 Answer</summary>
88-
89-
> The primary language used is C#, which is the same language that the MonoGame framework is developed in. However, any .NET language can be used, such as F# or Visual Basic.
90-
91-
</details><br />
81+
:::question-answer
82+
The primary language used is C#, which is the same language that the MonoGame framework is developed in. However, any .NET language can be used, such as F# or Visual Basic.
83+
:::
Loading

articles/tutorials/building_2d_games/02_getting_started/index.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ While the environment setup process is similar to the standard setup process for
1717
The first thing we need to do is install the .NET *Software Development Kit* (SDK). At the time of this writing, MonoGame targets the .NET 8.0 SDK. To install it, follow the instructions based on your operating system below
1818

1919
### [Windows](#tab/windows)
20+
2021
1. Open a web browser and navigate to https://dotnet.microsoft.com/en-us/download.
2122
2. Click the *Download .NET SDK x64* button to start the download of the .NET SDK Installer.
2223
3. Once the download finishes, run the installer
2324

2425
### [macOS](#tab/macos)
26+
2527
1. Open a web browser and navigate to https://dotnet.microsoft.com/en-us/download.
2628
2. Click the *Download .NET SDK x64 (Intel)* button start the download of the .NET SDK Installer
2729
3. Once the download finishes, run the installer.
@@ -152,7 +154,7 @@ Now that you have your development environment setup, it's time to create your f
152154
3. Type `.NET New Project` in the *Command Palette* and choose the *.NET New Project* command
153155
4. Next you'll be shown a list of the available .NET project templates. Enter `MonoGame` into the prompt to filter the project templates to only show the MonoGame ones, then choose the *MonoGame Cross-Platform Desktop Application* project template.
154156
5. After choosing the template, a dialog window will appear asking you to choose a location to save the project.
155-
6. Next you'll be prompted to enter a name for the project. Enter the name `MonoGameSnake`.
157+
6. Next you'll be prompted to enter a name for the project. Enter the name `DungeonSlime`.
156158
7. Finally, select the *Create Project* prompt.
157159

158160
After selecting *Create Project*, a new C# project will be created based on the MonoGame template we choose and opened automatically in VSCode.
@@ -199,27 +201,18 @@ Now that your development environment is setup and ready to go, you can dive in
199201

200202
1. What version of the .NET SDK is currently targeted by MonoGame applications?
201203

202-
<details>
203-
<summary>Question 1 Answer</summary>
204-
205-
> .NET 8.0
206-
207-
</details><br />
204+
:::question-answer
205+
.NET 8.0
206+
:::
208207

209208
2. What is the current version of MonoGame?
210209

211-
<details>
212-
<summary>Question 2 Answer</summary>
213-
214-
> 3.8.2.1105
215-
216-
</details><br />
210+
:::question-answer
211+
3.8.2.1105
212+
:::
217213

218214
3. What is the color of the game window when you run a MonoGame project for the first time?
219215

220-
<details>
221-
<summary>Question 3 Answer</summary>
222-
223-
> Cornflower Blue
224-
225-
</details><br />
216+
:::question-answer
217+
Cornflower Blue
218+
:::

articles/tutorials/building_2d_games/03_the_game1_file/index.md

Lines changed: 22 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ description: Explore the contents of the Game1 file generated when creating a ne
55

66
After you created a new MonoGame project using the *MonoGame Cross-Platform Desktop Application* template in [Chapter 02](../02_getting_started/index.md#creating-your-first-monogame-application), you will notice the generated files and project structure that serve as a starting point for your game application. While MonoGame offers different templates based on target platform, all projects will contain the *Game1.cs* file.
77

8-
> [!TIP]
9-
> For an in-depth look at all files created in a MonoGame project when using the MonoGame templates, refer to [Appendix 02: MonoGame Project Overview](#).
10-
118
## Exploring the Game1 Class
129

1310
At the core of a MonoGame project is the [**Game**](xref:Microsoft.Xna.Framework.Game) class. This class handles the initialization of graphics services, initialization of the game, loading content, updating, and rendering the game. When you create a new Monogame project, this [**Game**](xref:Microsoft.Xna.Framework.Game) class is implemented as the `Game1` class that you can customize as needed for your specific game.
@@ -17,54 +14,10 @@ At the core of a MonoGame project is the [**Game**](xref:Microsoft.Xna.Framework
1714
1815
Locate the *Game1.cs* file that was generated when you created the MonoGame project and open it. The default content will be:
1916

20-
```cs
21-
using Microsoft.Xna.Framework;
22-
using Microsoft.Xna.Framework.Graphics;
23-
using Microsoft.Xna.Framework.Input;
24-
25-
namespace MonoGameSnake;
26-
27-
public class Game1 : Game
28-
{
29-
private GraphicsDeviceManager _graphics;
30-
private SpriteBatch _spriteBatch;
31-
32-
public Game1()
33-
{
34-
_graphics = new GraphicsDeviceManager(this);
35-
Content.RootDirectory = "Content";
36-
IsMouseVisible = true;
37-
}
38-
39-
protected override void Initialize()
40-
{
41-
base.Initialize();
42-
}
43-
44-
protected override void LoadContent()
45-
{
46-
_spriteBatch = new SpriteBatch(GraphicsDevice);
47-
}
48-
49-
protected override void Update(GameTime gameTime)
50-
{
51-
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
52-
Exit();
53-
54-
base.Update(gameTime);
55-
}
56-
57-
protected override void Draw(GameTime gameTime)
58-
{
59-
GraphicsDevice.Clear(Color.CornflowerBlue);
60-
61-
base.Draw(gameTime);
62-
}
63-
}
64-
65-
```
17+
[!code-csharp[](./files/game1.cs)]
6618

6719
This class provides the following structure:
20+
6821
1. **Graphics and Rendering**: The class declares two core graphics components; the [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) for interacting with the Graphics Processing Unit (GPU) and the [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) for 2D rendering.
6922
2. **Initialization**: The constructor and [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method handle the game's setup sequence.
7023
3. **Content Loading**: The [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method manages game asset loading during startup.
@@ -77,76 +30,43 @@ Figure 3-1 below shows the lifecycle of a MonoGame game including the [**Update*
7730
| **Figure 3-1: Lifecycle of a MonoGame game** |
7831

7932
## Graphics and Rendering
33+
8034
The graphics pipeline in monogame starts with two components: the [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) and [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch).
8135

82-
```cs
83-
private GraphicsDeviceManager _graphics;
84-
private SpriteBatch _spriteBatch;
85-
```
36+
[!code-csharp[](./files/Game1.cs?start=9&end=10)]
8637

8738
The [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) initializes and the connection to the graphics hardware. It handles tasks such as setting the screen resolution, toggling between fullscreen and windowed mode, and managing the [**GraphicsDevice**](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice), which is the interface between your game and the Graphics Processing Unit (GPU) the game is running on. The [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) optimizes 2D rendering by batching similar draw calls together, improving draw performance when rendering multiple sprites.
8839

8940
## Initialization
9041

9142
MonoGame's initialization process for your game follows a specific sequence. The constructor runs first, which handles basic setup like creating the [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager), setting the content directory, and the visibility of the mouse.
9243

93-
```cs
94-
public Game1()
95-
{
96-
_graphics = new GraphicsDeviceManager(this);
97-
Content.RootDirectory = "Content";
98-
IsMouseVisible = true;
99-
}
100-
```
44+
[!code-csharp[](./files/Game1.cs?start=12&end=17)]
10145

10246
After that, the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method executes, providing a dedicated place for additional configuration and initializations.
10347

104-
```cs
105-
protected override void Initialize()
106-
{
107-
base.Initialize();
108-
}
109-
```
48+
[!code-csharp[](./files/Game1.cs?start=19&end=22)]
11049

11150
This separation allows you to perform setup tasks in a logical order; core systems in the constructor and game-specific initializations in the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method. The call to `base.Initialize()` should never be removed, as this is where the graphics device is initialized for the target platform.
11251

11352
> [!TIP]
11453
> You may be wondering why there is an [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method instead of performing all initializations in the constructor. The [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method is a `virtual` method that is overridden, and [it is advised to not call overridable methods from within a constructor](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2214), as this can lead to unexpected states in object constructor when called. Additionally, when the constructor is called, the base constructor will instantiate properties and services based on the target platform that may be needed first before performing initializations for the game itself.
11554
11655
## Content Loading
56+
11757
The [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method serves as the place for asset management. Here you can load textures, sound effects, music, and other game assets. We will cover loading assets in the coming chapters as we discuss each asset type that can be loaded. In a new project, the only task it performs is initializing a new instance of the [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch).
11858

119-
```cs
120-
protected override void LoadContent()
121-
{
122-
_spriteBatch = new SpriteBatch(GraphicsDevice);
123-
}
124-
```
59+
[!code-csharp[](./files/Game1.cs?start=24&end=27)]
12560

12661
This method is only call once during the startup of the game, but *when* it is called can be a little confusing at first. In the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method shown above, when the `base.Initialize` call is executed, the final task it performs is calling the [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method. This means any initializations you need to perform that have a dependency on assets being loaded should be done *after* the `base.Initialize` call and not *before* it.
12762

12863
## The Game Loop
12964

13065
MonoGame implements a *game loop* by calling [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) and [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) over and over until the game is told to exit. Recall at the end of [Chapter 02](../02_getting_started/index.md#creating-your-first-monogame-application) when you ran the project for the first time, I mentioned that there is a lot going on behind the scenes? This game loop is what I was referring to.
13166

132-
MonoGame is executing the [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) method and then the [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) method 60 times per second.
133-
134-
```cs
135-
protected override void Update(GameTime gameTime)
136-
{
137-
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
138-
Exit();
67+
MonoGame is executing the [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) method and then the [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) method 60 times per second.
13968

140-
base.Update(gameTime);
141-
}
142-
143-
protected override void Draw(GameTime gameTime)
144-
{
145-
GraphicsDevice.Clear(Color.CornflowerBlue);
146-
147-
base.Draw(gameTime);
148-
}
149-
```
69+
[!code-csharp[](./files/Game1.cs?start=29&end=42)]
15070

15171
The [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) method at the moment is not doing much, only checking for input from a controller or keyboard to determine if the game should exit. However, the [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) method is doing more than what it appears to at first glance.
15272

@@ -172,44 +92,28 @@ Here is a review of what was accomplished in this chapter:
17292

17393
In the next chapter, you will start working with sprites and learn how to load and render them.
17494

175-
## See Also
176-
177-
This chapter briefly touched on the *Game1.cs* file and the [**Game**](xref:Microsoft.Xna.Framework.Game) class. For an in-depth detailed discussion of all files created in a MonoGame project, including a full overview of the order of execution for a MonoGame game, see [Appendix 02: MonoGame Project Overview](#).
178-
17995
## Test Your Knowledge
18096

18197
1. Can the `Game1` class be renamed or is it required to be called `Game1`
18298

183-
<details>
184-
<summary>Question 1 Answer</summary>
185-
186-
> It is not a requirement that it be called `Game1`. This is just the default name given to it by the templates when creating a new MonoGame game project.
187-
188-
</details><br />
99+
:::question-answer
100+
It is not a requirement that it be called `Game1`. This is just the default name given to it by the templates when creating a new MonoGame game project.
101+
:::
189102

190103
2. What is the [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) used for?
191104

192-
<details>
193-
<summary>Question 2 Answer</summary>
194-
195-
> The [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) provides an optimized method of rendering 2D graphics, like sprites, onto the screen
196-
197-
</details><br />
105+
:::question-answer
106+
The [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) provides an optimized method of rendering 2D graphics, like sprites, onto the screen
107+
:::
198108

199109
3. When is the [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method executed and why is it important to know this?
200110

201-
<details>
202-
<summary>Question 3 Answer</summary>
203-
204-
> [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) is executed during the `base.Initialize()` method call within the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method. It is important to know this because anything being initialized that is dependent on content loaded should be done **after** the `base.Initialize()` call and not **before**.
205-
206-
</details><br />
111+
:::question-answer
112+
[**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) is executed during the `base.Initialize()` method call within the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method. It is important to know this because anything being initialized that is dependent on content loaded should be done **after** the `base.Initialize()` call and not **before**.
113+
:::
207114

208115
4. How does MonoGame provide a *delta time* value?
209116

210-
<details>
211-
<summary>Question 4 Answer</summary>
212-
213-
> Through the [**GameTime**](xref:Microsoft.Xna.Framework.GameTime) parameter that is given to both the [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) and the [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) methods.
214-
215-
</details><br />
117+
:::question-answer
118+
Through the [**GameTime**](xref:Microsoft.Xna.Framework.GameTime) parameter that is given to both the [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) and the [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) methods.
119+
:::
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.Xna.Framework;
2+
using Microsoft.Xna.Framework.Graphics;
3+
using Microsoft.Xna.Framework.Input;
4+
5+
namespace DungeonSlime;
6+
7+
public class Game1 : Game
8+
{
9+
private GraphicsDeviceManager _graphics;
10+
private SpriteBatch _spriteBatch;
11+
12+
public Game1()
13+
{
14+
_graphics = new GraphicsDeviceManager(this);
15+
Content.RootDirectory = "Content";
16+
IsMouseVisible = true;
17+
}
18+
19+
protected override void Initialize()
20+
{
21+
base.Initialize();
22+
}
23+
24+
protected override void LoadContent()
25+
{
26+
_spriteBatch = new SpriteBatch(GraphicsDevice);
27+
}
28+
29+
protected override void Update(GameTime gameTime)
30+
{
31+
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
32+
Exit();
33+
34+
base.Update(gameTime);
35+
}
36+
37+
protected override void Draw(GameTime gameTime)
38+
{
39+
GraphicsDevice.Clear(Color.CornflowerBlue);
40+
41+
base.Draw(gameTime);
42+
}
43+
}

0 commit comments

Comments
 (0)