Skip to content

Commit ab6c4ee

Browse files
Merge pull request #23 from AristurtleDev/20-the-game
Chapter 22: Snake Game Mechanics and Chapter 23: Completing the Game
2 parents e28e203 + aea2cc3 commit ab6c4ee

Some content is hidden

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

62 files changed

+3611
-423
lines changed

articles/toc.yml

+4
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: Console Access
158162
href: console_access.md
159163
- name: Help and Support

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

+465
Large diffs are not rendered by default.

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

+423-423
Large diffs are not rendered by default.
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+
}
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)