Skip to content

Commit 0f0d41a

Browse files
Merge branch '2d-tutorial-bounty' into 24-shaders
2 parents a4f4e2b + ab6c4ee commit 0f0d41a

Some content is hidden

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

65 files changed

+3617
-429
lines changed

articles/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@
154154
href: tutorials/building_2d_games/20_implementing_ui_with_gum/
155155
- name: "21: Customizing Gum UI"
156156
href: tutorials/building_2d_games/21_customizing_gum_ui/
157+
- name: "22: Snake Game Mechanics"
158+
href: tutorials/building_2d_games/22_snake_game_mechanics/
159+
- name: "23: Completing the Game"
160+
href: tutorials/building_2d_games/23_completing_the_game/
157161
- name: "24: Shaders"
158162
href: tutorials/building_2d_games/24_shaders/
159163
- name: Console Access

articles/tutorials/building_2d_games/10_handling_input/10_handling_input.md

Lines changed: 465 additions & 0 deletions
Large diffs are not rendered by default.

articles/tutorials/building_2d_games/10_handling_input/index.md

Lines changed: 423 additions & 423 deletions
Large diffs are not rendered by default.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Use a queue directly for input buffering
2+
private Queue<Vector2> _inputBuffer;
3+
private const int MAX_BUFFER_SIZE = 2;
4+
5+
// In initialization code:
6+
_inputBuffer = new Queue<Vector2>(MAX_BUFFER_SIZE);
7+
8+
// In the input handling code:
9+
KeyboardState keyboard = Keyboard.GetState();
10+
Vector2 newDirection = Vector2.Zero;
11+
12+
if(keyboard.IsKeyDown(Keys.Up))
13+
{
14+
newDirection = -Vector2.UnitY;
15+
}
16+
else if(keyboard.IsKeyDown(Keys.Down))
17+
{
18+
newDirection = Vector2.UnitY;
19+
}
20+
else if(keyboard.IsKeyDown(Keys.Left))
21+
{
22+
newDirection = -Vector2.UnitX;
23+
}
24+
else if(keyboard.IsKeyDown(Keys.Right))
25+
{
26+
newDirection = Vector2.UnitX;
27+
}
28+
29+
// Only add if a valid direction and does not exceed the buffer size
30+
if(newDirection != Vector2.Zero && _inputBuffer.Count < MAX_BUFFER_SIZE)
31+
{
32+
_inputBuffer.Enqueue(newDirection);
33+
}
34+
35+
// In movement update code
36+
if(_inputBuffer.COunt > 0)
37+
{
38+
Vector2 nextDirection = _inputBuffer.Dequeue();
39+
_position += nextDirection * _speed;
40+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ The `Update` method for the `InputManager` calls update for each device so that
347347

348348
## Implementing the InputManager Class
349349

350-
Now tha we have our input management system complete, we will update our game to use it. We will do this in two steps:
350+
Now that we have our input management system complete, we will update our game to use it. We will do this in two steps:
351351

352352
1. First, update the `Core` class to add the `InputManager` globally.
353353
2. Update the `Game1` class to use the global input manager from `Core`.
@@ -356,7 +356,7 @@ Now tha we have our input management system complete, we will update our game to
356356

357357
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:
358358

359-
[!code-csharp[](./snippets/core.cs?highlight=6,39-47,103-104,112-115)]
359+
[!code-csharp[](./snippets/core.cs?highlight=5-6,39-47,103-104,107-118)]
360360

361361
The key changes to the `Core` class are:
362362

@@ -372,7 +372,7 @@ The key changes to the `Core` class are:
372372

373373
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:
374374

375-
[!code-csharp[](./snippets/game1.cs?highlight=76,82,88,94,100,108,113,116,120,126,128-129,134,140,146,152)]
375+
[!code-csharp[](./snippets/game1.cs?highlight=6,76,82,88,94,100,108,113,116,120,126,128-129,134,140,146,152)]
376376

377377
The key changes to the `Game1` class are:
378378

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class Core : Game
3737
public static new ContentManager Content { get; private set; }
3838

3939
/// <summary>
40-
/// Gets a reference to to the input management system.
40+
/// Gets a reference to the input management system.
4141
/// </summary>
4242
public static InputManager Input { get; private set; }
4343

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ To calculate the squared distance between to points, MonoGame provides the [**Ve
6666

6767
Rectangles, often called *bounding boxes*, typically uses what is called *Axis-Aligned Bounding Box* (AABB) collision detection to determine if two rectangle shapes overlap. Unlike circles, to perform AABB collision detection, the x- and y-axes of both rectangles must be aligned with the x- and y-axes of the screen. This is just another way of saying that the rectangles cannot be rotated.
6868

69-
| ![Figure 12-2: The rectangle on the left is axis-aligned since both the axes are aligned with the screen axes. The rectangle on the right is non axis-aligned sine it is rotated and the axes do not align with the screen axe.](./images/aabb-vs-non-aabb.svg) |
69+
| ![Figure 12-2: The rectangle on the left is axis-aligned since both the axes are aligned with the screen axes. The rectangle on the right is non axis-aligned since it is rotated and the axes do not align with the screen axe.](./images/aabb-vs-non-aabb.svg) |
7070
| :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
71-
| **Figure 12-2: The rectangle on the left is axis-aligned since both the axes are aligned with the screen axes. The rectangle on the right is non axis-aligned sine it is rotated and the axes do not align with the screen axes** |
71+
| **Figure 12-2: The rectangle on the left is axis-aligned since both the axes are aligned with the screen axes. The rectangle on the right is non axis-aligned since it is rotated and the axes do not align with the screen axes** |
7272

7373
MonoGame provides the [**Rectangle**](xref:Microsoft.Xna.Framework.Rectangle) struct which represents a rectangle by its position (X,Y) and size (Width,Height). The following table shows some of the properties of the [**Rectangle**](xref:Microsoft.Xna.Framework.Rectangle) struct:
7474

Loading
Lines changed: 119 additions & 0 deletions
Loading
Loading

0 commit comments

Comments
 (0)