Skip to content
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

Snake speed options #87

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 26 additions & 7 deletions Projects/Snake/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
using System.Collections.Generic;

Exception? exception = null;

int speedInput;
string prompt = $"Select speed [1], [2] (default), or [3]: ";
string? input;
Console.Write(prompt);
while (!int.TryParse(input = Console.ReadLine(), out speedInput) || speedInput < 1 || 3 < speedInput)
{
if (string.IsNullOrWhiteSpace(input))
{
speedInput = 2;
break;
}
else
{
Console.WriteLine("Invalid Input. Try Again...");
Console.Write(prompt);
}
}
int[] velocities = { 100, 70, 50 };
int velocity = velocities[speedInput - 1];
char[] DirectionChars = { '^', 'v', '<', '>', };
TimeSpan sleep = TimeSpan.FromMilliseconds(70);
TimeSpan sleep = TimeSpan.FromMilliseconds(velocity);
int width = Console.WindowWidth;
int height = Console.WindowHeight;
Random random = new();
Expand Down Expand Up @@ -53,7 +71,7 @@
Console.SetCursorPosition(X, Y);
Console.Write(DirectionChars[(int)direction!]);
snake.Enqueue((X, Y));
if (map[X, Y] == Tile.Food)
if (map[X, Y] is Tile.Food)
{
PositionFood();
}
Expand Down Expand Up @@ -85,14 +103,15 @@
}

void GetDirection()
// takes direction from arrow keys
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow: direction = Direction.Up; break;
case ConsoleKey.DownArrow: direction = Direction.Down; break;
case ConsoleKey.LeftArrow: direction = Direction.Left; break;
case ConsoleKey.UpArrow: direction = Direction.Up; break;
case ConsoleKey.DownArrow: direction = Direction.Down; break;
case ConsoleKey.LeftArrow: direction = Direction.Left; break;
case ConsoleKey.RightArrow: direction = Direction.Right; break;
case ConsoleKey.Escape: closeRequested = true; break;
case ConsoleKey.Escape: closeRequested = true; break;
}
}

Expand Down
33 changes: 26 additions & 7 deletions Projects/Website/Games/Snake/Snake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,27 @@ public class Snake
public async Task Run()
{
Exception? exception = null;

int speedInput;
string prompt = $"Select speed [1], [2] (default), or [3]: ";
string? input;
await Console.Write(prompt);
while (!int.TryParse(input = await Console.ReadLine(), out speedInput) || speedInput < 1 || 3 < speedInput)
{
if (string.IsNullOrWhiteSpace(input))
{
speedInput = 2;
break;
}
else
{
await Console.WriteLine("Invalid Input. Try Again...");
await Console.Write(prompt);
}
}
int[] velocities = { 50, 35, 20 };
int velocity = velocities[speedInput - 1];
char[] DirectionChars = { '^', 'v', '<', '>', };
TimeSpan sleep = TimeSpan.FromMilliseconds(35);
TimeSpan sleep = TimeSpan.FromMilliseconds(velocity);
int width = Console.WindowWidth;
int height = Console.WindowHeight;
Random random = new();
Expand Down Expand Up @@ -64,7 +82,7 @@ public async Task Run()
await Console.SetCursorPosition(X, Y);
await Console.Write(DirectionChars[(int)direction!]);
snake.Enqueue((X, Y));
if (map[X, Y] == Tile.Food)
if (map[X, Y] is Tile.Food)
{
await PositionFood();
}
Expand Down Expand Up @@ -100,11 +118,11 @@ async Task GetDirection()
{
switch ((await Console.ReadKey(true)).Key)
{
case ConsoleKey.UpArrow: direction = Direction.Up; break;
case ConsoleKey.DownArrow: direction = Direction.Down; break;
case ConsoleKey.LeftArrow: direction = Direction.Left; break;
case ConsoleKey.UpArrow: direction = Direction.Up; break;
case ConsoleKey.DownArrow: direction = Direction.Down; break;
case ConsoleKey.LeftArrow: direction = Direction.Left; break;
case ConsoleKey.RightArrow: direction = Direction.Right; break;
case ConsoleKey.Escape: closeRequested = true; break;
case ConsoleKey.Escape: closeRequested = true; break;
}
}

Expand Down Expand Up @@ -143,4 +161,5 @@ enum Tile
Snake,
Food,
}

}
Loading