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
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/17_scenes/index.md
+25-32Lines changed: 25 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -109,28 +109,23 @@ The key changes here are:
109
109
> [!TIP]
110
110
> Notice that we use a two-step process for scene transitions with separate `_activeScene` and `_nextScene` fields. This design allows the current scene to complete its update/draw cycle before the transition occurs, preventing potential issues that could arise from changing scenes in the middle of processing. The actual transition happens at a controlled point in the game loop, ensuring clean disposal of the old scene before initializing the new one.
111
111
112
-
## Adding Scenes To Our Game
112
+
## Updating the Game
113
113
114
-
With the scene architecture in place, the game can be broken down into scenes. We will create two scenes; a title scene and a gameplay scene.
114
+
With the scene architecture in place, the game can now be updated so that it is broken down into scenes. We'll create two scenes; a title scene and a gameplay scene.
115
115
116
116
### The Title Scene
117
117
118
-
The title scene that will serve as our game's main menu. This scene will display the game title, a prompt to start the game, and an animated slime to give the scene some life.
118
+
The title scene serves as the game's initial starting point, making the first impression on the player when they first launch the game. For our game, this scene will display stylized text for the title of the game and a prompt for an action for the user to perform to start the game. The stylized text is a graphic that was created and added to the texture atlas which features the title of the game with a drop shadow effect on the text. So first, let's update the texture atlas to the new version with the title graphic. Download the new texture atlas below by right-clicking the following image and saving it as *atlas.png* in the *Content/images* directory of the game project, overwriting the existing one:
119
119
120
-
Before we implement the Title Scene, we need to create a font for the title text. Open the *Content.mgcb* content project in the MGCB Editor and:
120
+
||
Next, download the *04B_30.ttf* font below by right-clicking it, choosing "Save Link as...", and saving it in the same directory that you just created the *titleFont.spritefont* file.
130
-
131
-
-[04B_30.ttf](./files/04B_30.TTF)
132
-
133
-
Next, create the `TitleScene` class file. In the main game project:
128
+
With the atlas now updated, create the `TitleScene` class file. In the main game project:
134
129
135
130
1. Create a new directory named *Scenes*. We'll put all of our game specific scenes here.
136
131
2. Add a new class file named *TitleScene.cs* to the *Scenes* directory you just created.
@@ -144,11 +139,11 @@ Add the following fields to the `TitleScene` class:
144
139
145
140
[!code-csharp[](./snippets/titlescene.cs#fields)]
146
141
147
-
- The `TITLE` and `PRESS_ENTER`constants are the text that we'll draw to display the game title and the prompt to press enter.
148
-
- The `_titleFont` and `_standardFont` fields are the two fonts we'll load and use to draw the game title and the prompt with.
149
-
-`_titlePos`, `_titleOrigin`, `_pressEnterPos`, and `_pressEnterOrigin` fields will contain the precalculated positions and origins to use when rendering the game title and the press enter prompt.
150
-
- The `_slime` field will store the animated sprite for the slime that will be drawn.
151
-
- The `_simePos` field will store the precalculated positionto draw the slime at.
142
+
- The `PRESS_ENTER`constant is the text we'll draw for the press enter prompt for the user.
143
+
- The `_font` field stores a reference to the sprite font we'll load to render the press enter prompt with.
144
+
-The `_titleSprite` field stores a reference the sprite we'll render for the stylized title from the texture atlas
145
+
- The `_titlePos` and `_pressEnterPos` fields store the precalculated positions for the title sprite and the press enter prompt text when they are drawn. Since they are stationary, we can just calculate the positions once and store it instead of calculating it each frame.
146
+
- The `_pressEnterOrigin` field stores the precalculated origin for hte press enter prompt text when it is drawn. Like with the position, we only need to calculate this once and store it instead of calculating it each frame.
152
147
153
148
#### Title Scene Methods
154
149
@@ -161,20 +156,19 @@ Add the following override for the `Initialize` method to the `TitleScene` class
- We set the `Core.ExitOnEscape` to true to allow players to exit the game when on the title screen by pressing the escape key.
164
-
- A reference to the graphics device from the `Core` instance is captured so we don't have to type `Core.Instance.GraphicsDevice` each time to use it.
165
-
- The position and origins of the title text and the press enter prompt are precalculated. Their positions and origins are static as they don't move around on the screen, so we can pre-calculate them here instead of doing it each cycle in the `Draw` method.
166
-
- The position of the slime is also precalculated as it doesn't move.
167
-
- The scale of the slime is set to `5.0f` since we're on the title screen to fill in more space.
159
+
- The bounds of the screen is captures by using the `Core.GraphicsDevice.PresentationParameters.Bounds` value.
160
+
- The position to draw the title sprite is precalculated so that it will be centered horizontally and 80px down from the top of the screen. The origin is set to center.
161
+
- The position to draw the press enter prompt is precalculated so that it will be centered horizontally and 100 px above the bottom of the screen. The string is measured and used to center the origin for the text.
168
162
169
163
##### Title Scene LoadContent
170
164
171
165
Add the following override for the `LoadContent` method to the `TitleScene` class:
-We capture a reference to the content manager from the `Core` class. This content manager is used to load content that is used in multiple scenes, so it acts as a global content manager.
176
-
- The fonts are loaded. The title font is only used on the title screen, so it is loaded using the scene's content manager, while the standard font is loaded using the global content manager.
177
-
- The texture atlas is loaded and the slime animated sprite is created.
169
+
-The font used to draw the press enter prompt is loaded.
170
+
- The texture atlas is loaded using the XML configuration file.
171
+
- The `_titleSprite`is generated from the `"title-card"` region in the atlas.
178
172
179
173
> [!TIP]
180
174
> Recall from [Chapter 05](../05_content_pipeline/index.md#contentmanager-methods) that when a [**ContentManager**](xref:Microsoft.Xna.Framework.Content.ContentManager) loads an asset for the first time, it caches it internally and the subsequent calls to load that asset will return the cached one instead of performing another disk read.
@@ -187,7 +181,6 @@ Add the following override for the `Update` method to the `TitleScene` class:
187
181
188
182
[!code-csharp[](./snippets/titlescene.cs#update)]
189
183
190
-
- The animated slime is updated.
191
184
- A check is made to see if the enter key is pressed, and if so, the `Core` is told to change to the game scene.
192
185
193
186
> [!NOTE]
@@ -199,9 +192,9 @@ Add the following override for the `Draw` method to the `TitleScene` class:
199
192
200
193
[!code-csharp[](./snippets/titlescene.cs#draw)]
201
194
202
-
- A reference to the graphics device from the `Core` instance is captured so we don't have to type `Core.Instance.GraphicsDevice` each time to use it.
203
195
- The back buffer is cleared.
204
-
- The title text, press enter prompt, and the animated slime are drawn using the sprite batch provided from the `Core` class.
196
+
- The title sprite is drawn at its precalculated position.
197
+
- The press enter prompt is drawn at its precalculated position.
205
198
206
199
### The Game Scene
207
200
@@ -305,9 +298,9 @@ The `Game1` class is now much simpler as most of the game logic has been moved t
305
298
306
299
Running the game now, we can see that once the game screen comes up, the title scene is displayed with the animated slime and the press enter prompt. The background music starts playing on this scene as well. Pressing enter from here will switch to the game scene where the game starts and we can play the game implemented thus far.
307
300
308
-
||
|**Figure 17-1: The game launching with the title screen first, then transitioning to the game play screen when enter is pressed**|
301
+
||
0 commit comments