You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/01_what_is_monogame/index.md
+9-17Lines changed: 9 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -69,23 +69,15 @@ Regardless of which .NET language used, developers should have a foundational un
69
69
70
70
1. Name one of the advantages of using the MonoGame framework to develop games.
71
71
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
+
:::
83
78
84
79
2. What programming languages can be used when creating a game with MonoGame?
85
80
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.
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/02_getting_started/index.md
+12-19Lines changed: 12 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -17,11 +17,13 @@ While the environment setup process is similar to the standard setup process for
17
17
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
18
18
19
19
### [Windows](#tab/windows)
20
+
20
21
1. Open a web browser and navigate to https://dotnet.microsoft.com/en-us/download.
21
22
2. Click the *Download .NET SDK x64* button to start the download of the .NET SDK Installer.
22
23
3. Once the download finishes, run the installer
23
24
24
25
### [macOS](#tab/macos)
26
+
25
27
1. Open a web browser and navigate to https://dotnet.microsoft.com/en-us/download.
26
28
2. Click the *Download .NET SDK x64 (Intel)* button start the download of the .NET SDK Installer
27
29
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
152
154
3. Type `.NET New Project` in the *Command Palette* and choose the *.NET New Project* command
153
155
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.
154
156
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`.
156
158
7. Finally, select the *Create Project* prompt.
157
159
158
160
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
199
201
200
202
1. What version of the .NET SDK is currently targeted by MonoGame applications?
201
203
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
+
:::
208
207
209
208
2. What is the current version of MonoGame?
210
209
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
+
:::
217
213
218
214
3. What is the color of the game window when you run a MonoGame project for the first time?
@@ -5,9 +5,6 @@ description: Explore the contents of the Game1 file generated when creating a ne
5
5
6
6
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.
7
7
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
-
11
8
## Exploring the Game1 Class
12
9
13
10
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
17
14
18
15
Locate the *Game1.cs* file that was generated when you created the MonoGame project and open it. The default content will be:
19
16
20
-
```cs
21
-
usingMicrosoft.Xna.Framework;
22
-
usingMicrosoft.Xna.Framework.Graphics;
23
-
usingMicrosoft.Xna.Framework.Input;
24
-
25
-
namespaceMonoGameSnake;
26
-
27
-
publicclassGame1 : Game
28
-
{
29
-
privateGraphicsDeviceManager_graphics;
30
-
privateSpriteBatch_spriteBatch;
31
-
32
-
publicGame1()
33
-
{
34
-
_graphics=newGraphicsDeviceManager(this);
35
-
Content.RootDirectory="Content";
36
-
IsMouseVisible=true;
37
-
}
38
-
39
-
protectedoverridevoidInitialize()
40
-
{
41
-
base.Initialize();
42
-
}
43
-
44
-
protectedoverridevoidLoadContent()
45
-
{
46
-
_spriteBatch=newSpriteBatch(GraphicsDevice);
47
-
}
48
-
49
-
protectedoverridevoidUpdate(GameTimegameTime)
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
-
protectedoverridevoidDraw(GameTimegameTime)
58
-
{
59
-
GraphicsDevice.Clear(Color.CornflowerBlue);
60
-
61
-
base.Draw(gameTime);
62
-
}
63
-
}
64
-
65
-
```
17
+
[!code-csharp[](./files/game1.cs)]
66
18
67
19
This class provides the following structure:
20
+
68
21
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.
69
22
2.**Initialization**: The constructor and [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method handle the game's setup sequence.
70
23
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*
77
30
|**Figure 3-1: Lifecycle of a MonoGame game**|
78
31
79
32
## Graphics and Rendering
33
+
80
34
The graphics pipeline in monogame starts with two components: the [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) and [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch).
81
35
82
-
```cs
83
-
privateGraphicsDeviceManager_graphics;
84
-
privateSpriteBatch_spriteBatch;
85
-
```
36
+
[!code-csharp[](./files/Game1.cs?start=9&end=10)]
86
37
87
38
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.
88
39
89
40
## Initialization
90
41
91
42
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.
After that, the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) method executes, providing a dedicated place for additional configuration and initializations.
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.
112
51
113
52
> [!TIP]
114
53
> 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.
115
54
116
55
## Content Loading
56
+
117
57
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).
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.
127
62
128
63
## The Game Loop
129
64
130
65
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.
131
66
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
-
protectedoverridevoidUpdate(GameTimegameTime)
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.
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.
152
72
@@ -172,44 +92,28 @@ Here is a review of what was accomplished in this chapter:
172
92
173
93
In the next chapter, you will start working with sprites and learn how to load and render them.
174
94
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
-
179
95
## Test Your Knowledge
180
96
181
97
1. Can the `Game1` class be renamed or is it required to be called `Game1`
182
98
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
+
:::
189
102
190
103
2. What is the [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) used for?
191
104
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
+
:::
198
108
199
109
3. When is the [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method executed and why is it important to know this?
200
110
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
+
:::
207
114
208
115
4. How does MonoGame provide a *delta time* value?
209
116
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.
0 commit comments