Skip to content

Chapter 22: Snake Game Mechanics and Chapter 23: Completing the Game #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1df09e1
Initial draft
AristurtleDev Apr 15, 2025
c1b2888
Aded video to end
AristurtleDev Apr 15, 2025
35c260f
renamed to chapter 22 since adding other UI chapters
AristurtleDev Apr 24, 2025
a0f66a2
Make game controller static
AristurtleDev Apr 24, 2025
268b538
Captilazation
AristurtleDev Apr 24, 2025
762bf34
Merge branch '2d-tutorial-bounty' into 20-the-game
AristurtleDev Apr 26, 2025
2426354
Renamed directory to chapter title
AristurtleDev Apr 28, 2025
b6767b1
Rewrite
AristurtleDev Apr 28, 2025
796a1c0
Update toc title
AristurtleDev Apr 28, 2025
9353458
Update title in index table
AristurtleDev Apr 28, 2025
08e8e38
Resolve bug in position away from slime code
AristurtleDev Apr 29, 2025
5dc2ce1
Update images with less text and more descriptive captions
AristurtleDev Apr 30, 2025
a8af406
Specify directory
AristurtleDev Apr 30, 2025
b41b7e0
Specify directory
AristurtleDev Apr 30, 2025
d0f96f0
Specify directory
AristurtleDev Apr 30, 2025
c1fc470
Add important admonition about struct boxing
AristurtleDev Apr 30, 2025
7511af5
Be more explicit as to where methods are placed in which order, with …
AristurtleDev Apr 30, 2025
b6f3e0b
Have method return type instead of adding element within method
AristurtleDev Apr 30, 2025
c7684f0
Split into two chapters
AristurtleDev May 1, 2025
1929b58
Added input buffering to end of chapter
AristurtleDev May 1, 2025
82bab89
Remove the old part of chapter that was split (oops)
AristurtleDev May 1, 2025
8e5f34d
Remove the overlay since we will use shader in next chapter
AristurtleDev May 2, 2025
472ef39
Rasterize images to half size
AristurtleDev May 2, 2025
fe67a8a
IN to In
AristurtleDev May 2, 2025
52fa8e3
IN to In
AristurtleDev May 2, 2025
a68ff87
Fix 'hen' to 'when'
AristurtleDev May 2, 2025
0255deb
Break down into individual sections instead of one after another addi…
AristurtleDev May 2, 2025
d3756b6
Move input buffer overview to chapter 10
AristurtleDev May 2, 2025
1b6e43b
Ensure that it is clear that it is an example
AristurtleDev May 5, 2025
d255669
Ensure that it is clear that it is an example
AristurtleDev May 5, 2025
0bc0034
We will implement
AristurtleDev May 5, 2025
0e35506
Reduce use of 'First,
AristurtleDev May 5, 2025
c13ed8c
This to the
AristurtleDev May 5, 2025
1269cde
hightlight to highlight
AristurtleDev May 5, 2025
aea2cc3
Merge branch '2d-tutorial-bounty' into 20-the-game
AristurtleDev May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
href: tutorials/building_2d_games/20_implementing_ui_with_gum/
- name: "21: Customizing Gum UI"
href: tutorials/building_2d_games/21_customizing_gum_ui/
- name: "22: Snake Game Mechanics"
href: tutorials/building_2d_games/22_snake_game_mechanics/
- name: "23: Completing the Game"
href: tutorials/building_2d_games/23_completing_the_game/
- name: Console Access
href: console_access.md
- name: Help and Support
Expand Down

Large diffs are not rendered by default.

846 changes: 423 additions & 423 deletions articles/tutorials/building_2d_games/10_handling_input/index.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Use a queue directly for input buffering
private Queue<Vector2> _inputBuffer;
private const int MAX_BUFFER_SIZE = 2;

// In initialization code:
_inputBuffer = new Queue<Vector2>(MAX_BUFFER_SIZE);

// In the input handling code:
KeyboardState keyboard = Keyboard.GetState();
Vector2 newDirection = Vector2.Zero;

if(keyboard.IsKeyDown(Keys.Up))
{
newDirection = -Vector2.UnitY;
}
else if(keyboard.IsKeyDown(Keys.Down))
{
newDirection = Vector2.UnitY;
}
else if(keyboard.IsKeyDown(Keys.Left))
{
newDirection = -Vector2.UnitX;
}
else if(keyboard.IsKeyDown(Keys.Right))
{
newDirection = Vector2.UnitX;
}

// Only add if a valid direction and does not exceed the buffer size
if(newDirection != Vector2.Zero && _inputBuffer.Count < MAX_BUFFER_SIZE)
{
_inputBuffer.Enqueue(newDirection);
}

// In movement update code
if(_inputBuffer.COunt > 0)
{
Vector2 nextDirection = _inputBuffer.Dequeue();
_position += nextDirection * _speed;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading