From 0eb8e525c728b6b984f5ec20067de2d5625e9ce2 Mon Sep 17 00:00:00 2001 From: Alex JL <78103379+alexsrk09@users.noreply.github.com> Date: Tue, 17 Oct 2023 17:47:06 +0200 Subject: [PATCH] Snake speed options (#87) --- Projects/Snake/Program.cs | 33 +++++++++++++++++++++------ Projects/Website/Games/Snake/Snake.cs | 33 +++++++++++++++++++++------ 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Projects/Snake/Program.cs b/Projects/Snake/Program.cs index 80b37ae8..63fecca1 100644 --- a/Projects/Snake/Program.cs +++ b/Projects/Snake/Program.cs @@ -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(); @@ -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(); } @@ -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; } } diff --git a/Projects/Website/Games/Snake/Snake.cs b/Projects/Website/Games/Snake/Snake.cs index 2b8403db..6276e6da 100644 --- a/Projects/Website/Games/Snake/Snake.cs +++ b/Projects/Website/Games/Snake/Snake.cs @@ -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(); @@ -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(); } @@ -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; } } @@ -143,4 +161,5 @@ enum Tile Snake, Food, } + }