static void Main(string[] args) { InitialSetup.InitializeWindow(); SnakePlayer snakePlayer = new SnakePlayer("Nikolay"); AppleHandler appleHandler = new AppleHandler(); Timer timer = new Timer(x => appleHandler.GenerateApple(), null, 1000, 3000); Print.PrintWindow(snakePlayer); while (true) { if (!snakePlayer.IsAlive) { Print.PrintEndGame(snakePlayer); break; } Print.ClearSnake(snakePlayer); if (!Console.KeyAvailable) { snakePlayer.Move(appleHandler); } else { ConsoleKeyInfo keyPressed = Console.ReadKey(); snakePlayer.Move(keyPressed, appleHandler); } Print.PrintApples(appleHandler); Print.PrintSnake(snakePlayer); Thread.Sleep(100); } }
public static void PrintApples(AppleHandler appleHandler) { foreach (Apple apple in appleHandler.Apples) { Console.SetCursorPosition(apple.Position.X, apple.Position.Y); Console.Write(apple.Symbol); } }
private void UpdatePlayerPosition(Direction direction, AppleHandler appleHandler) { Direction = direction; Point newHeadPosition = new Point(HeadPosition.X, HeadPosition.Y); switch (direction) { case Direction.Left: newHeadPosition.X--; break; case Direction.Right: newHeadPosition.X++; break; case Direction.Up: newHeadPosition.Y--; break; case Direction.Down: newHeadPosition.Y++; break; default: break; } if (HasCollidedWithWall()) { Die(); return; } HeadPosition = newHeadPosition; if (HasCollidedWithApple(appleHandler.Apples)) { EatApple(HeadPosition, appleHandler); } else { Body.Insert(0, newHeadPosition); Body.RemoveAt(Body.Count() - 1); } }
public void Move(ConsoleKeyInfo keyPressed, AppleHandler appleHandler) { Direction newDirection = SwitchDirection(keyPressed); UpdatePlayerPosition(newDirection, appleHandler); }
public void Move(AppleHandler appleHandler) { UpdatePlayerPosition(this.Direction, appleHandler); }
private void EatApple(Point position, AppleHandler appleHandler) { Body.Insert(0, position); appleHandler.RemoveApple(position); }