Skip to content

Commit

Permalink
Snake speed options (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsrk09 committed Oct 17, 2023
1 parent b490e31 commit 0eb8e52
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
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,
}

}

0 comments on commit 0eb8e52

Please sign in to comment.