internal void Update(GameTime gameTime) { var newDirection = GetNewDirection(); elapsedTimeSinceMove += gameTime.ElapsedGameTime.TotalSeconds; if (elapsedTimeSinceMove >= 1 / speed) { elapsedTimeSinceMove = 0.0; snake.Update(newDirection); if (snake.HasHitSelf() || snake.IsOutOfBounds(gridDescription)) { IsSnakeAlive = false; return; } if (snake.HasEatenFood(foodPosition)) { foodPosition = GenerateFoodPosition(); Score++; speed += 0.1f; snake.AddBodyPart(); } } }
public static void Main(string[] args) { Console.WindowWidth = ConsoleWidth; Console.WindowHeight = ConsoleHeight; Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; Console.CursorVisible = false; KeyboardControls keyboard = new KeyboardControls(); keyboard.OnLeftPress += HandleOnLeftPress; keyboard.OnRightPress += HandleOnRightPress; keyboard.OnUpPress += HandleOnUpPress; keyboard.OnDownPress += HandleOnDownPress; keyboard.OnEscape += HandleOnEscape; bool placeFood = false; Food food = new Food(RandomNumber.Generate(0, ConsoleWidth), RandomNumber.Generate(0, ConsoleHeight), '+'); while (runGame) { Console.Clear(); keyboard.ProcessInput(); snake.Draw(); snake.Update(); food.Draw(); if (placeFood) { food = new Food(RandomNumber.Generate(0, ConsoleWidth), RandomNumber.Generate(0, ConsoleHeight), '+'); placeFood = false; } if (food.Position.X == snake.Body[0].Position.X && food.Position.Y == snake.Body[0].Position.Y) { Console.Beep(); snake.AddBodyPart(); placeFood = true; } if (snake.Body[0].Position.X < 0 || snake.Body[0].Position.X >= ConsoleWidth || snake.Body[0].Position.Y < 0 || snake.Body[0].Position.Y >= ConsoleHeight) { runGame = false; } for (int i = 1; i < snake.Body.Count; i++) { if (snake.Body[0].Position.X == snake.Body[i].Position.X && snake.Body[0].Position.Y == snake.Body[i].Position.Y) { runGame = false; break; } } Thread.Sleep(ThreadSpeed); } Console.WriteLine("Thanks for playing"); }