static void Main(string[] args) { BoardStartX = 5; BoardEndX = 20; BoardStartY = 5; BoardEndY = 20; Console.CursorVisible = false; bool exit = false; double frameRate = 1000 / 5.0; DateTime lastDate = DateTime.Now; Meal meal = new Meal(BoardStartX, BoardEndX, BoardStartY, BoardEndY); Snake snake = new Snake(BoardStartX, BoardEndX, BoardStartY, BoardEndY, meal); Snake = snake; while (!exit) { if (Console.KeyAvailable) { ConsoleKeyInfo input = Console.ReadKey(); switch (input.Key) { case ConsoleKey.Escape: exit = true; break; case ConsoleKey.LeftArrow: snake.Direction = Direction.Left; break; case ConsoleKey.RightArrow: snake.Direction = Direction.Right; break; case ConsoleKey.UpArrow: snake.Direction = Direction.Up; break; case ConsoleKey.DownArrow: snake.Direction = Direction.Down; break; } } if ((DateTime.Now - lastDate).TotalMilliseconds >= frameRate) { //game action snake.Move(); if (snake.GameOver) { Console.Clear(); Console.WriteLine($"GAME OVER. YOUR SCORE: {snake.Score}"); exit = true; Console.ReadLine(); } lastDate = DateTime.Now; } } }
static void Main(string[] args) { MealDrawn = false; BoardStartX = 5; BoardEndX = 20; BoardStartY = 5; BoardEndY = 20; Lives = 2; Console.CursorVisible = false; bool exit = false; double frameRate = 1000 / 5.0; DateTime lastDate = DateTime.Now; Meal meal = new Meal(BoardStartX, BoardEndX, BoardStartY, BoardEndY); Snake snake = new Snake(BoardStartX, BoardEndX, BoardStartY, BoardEndY, meal); Snake = snake; while (!exit) { if (Console.KeyAvailable) { ConsoleKeyInfo input = Console.ReadKey(); switch (input.Key) { case ConsoleKey.Escape: exit = true; break; case ConsoleKey.LeftArrow: snake.Direction = Direction.Left; break; case ConsoleKey.RightArrow: snake.Direction = Direction.Right; break; case ConsoleKey.UpArrow: snake.Direction = Direction.Up; break; case ConsoleKey.DownArrow: snake.Direction = Direction.Down; break; } } if ((DateTime.Now - lastDate).TotalMilliseconds >= frameRate) { //game action snake.Move(); DrawBoard(); if (meal.Eaten) { Console.SetCursorPosition(meal.CurrentTarget.X, meal.CurrentTarget.Y); Console.Write("@"); MealDrawn = false; meal.RegeneratePosition(); while (snake.Tail.Any(b => b.X == meal.CurrentTarget.X && b.Y == meal.CurrentTarget.Y)) { meal.RegeneratePosition(); } frameRate /= 1.1; } if (snake.GameOver) { if (Lives == 0) { Console.Clear(); Console.WriteLine($"GAME OVER. YOUR SCORE: {snake.Score}"); exit = true; Console.ReadLine(); } else { Lives--; Console.Clear(); MealDrawn = false; meal.RegeneratePosition(); snake.Regenerate(); } } if (!MealDrawn) { meal.Draw(); MealDrawn = true; } lastDate = DateTime.Now; } } }