From 256a05b930e7af919874a0d54e3522f070e70820 Mon Sep 17 00:00:00 2001 From: morpha Date: Fri, 24 Mar 2023 17:52:50 +0100 Subject: [PATCH] Rudimentary rendering and movement implemented --- ConsoleSnake/Program.cs | 69 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/ConsoleSnake/Program.cs b/ConsoleSnake/Program.cs index f7e57f9..a0b59a0 100644 --- a/ConsoleSnake/Program.cs +++ b/ConsoleSnake/Program.cs @@ -2,9 +2,76 @@ { internal class Program { + public enum Direction + { + Up, + Down, + Left, + Right + } + + private static Direction _direction = Direction.Up; static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + Console.SetCursorPosition(Console.WindowWidth/2, Console.WindowHeight/2); + for(int i = 1; ;++i) + { + HandleInput(); + ProcessMovement(); + DrawSnake(); + Thread.Sleep(100); + } + } + + private static void DrawSnake() + { + Console.Write('#'); + --Console.CursorLeft; + } + + private static void ProcessMovement() + { + switch (_direction) + { + case Direction.Up: + --Console.CursorTop; + break; + case Direction.Down: + ++Console.CursorTop; + break; + case Direction.Right: + ++Console.CursorLeft; + break; + case Direction.Left: + --Console.CursorLeft; + break; + } + } + + static void HandleInput() + { + if (Console.KeyAvailable) + { + switch(Console.ReadKey(true).Key) + { + case ConsoleKey.UpArrow: + case ConsoleKey.W: + _direction = Direction.Up; + break; + case ConsoleKey.DownArrow: + case ConsoleKey.S: + _direction = Direction.Down; + break; + case ConsoleKey.LeftArrow: + case ConsoleKey.A: + _direction = Direction.Left; + break; + case ConsoleKey.RightArrow: + case ConsoleKey.D: + _direction = Direction.Right; + break; + } + } } } } \ No newline at end of file