public static Possition GetMovement(ConsoleKey key) { Possition possition = new Possition(0, 0); switch (key) { case ConsoleKey.UpArrow: possition = new Possition(0, -1); break; case ConsoleKey.DownArrow: possition = new Possition(0, 1); break; case ConsoleKey.LeftArrow: possition = new Possition(-1, 0); break; case ConsoleKey.RightArrow: possition = new Possition(1, 0); break; } return(possition); }
public void Start() { isStarded = true; Possition movement = new Possition(0, 0); while (isStarded == true) { BoundriesHelper.CheckBoundaries(Snake.SnakeBody.Head.Value, movement); Snake.Move(movement); if (Snake.CheckSelfCanibalism()) { Console.Clear(); ConsoleHelper.Write(new Possition(0, 0), "Game Over"); isStarded = false; break; } if (Console.KeyAvailable) { var key = Console.ReadKey(false).Key; movement = ReadUserInput.GetMovement(key); } Thread.Sleep(200); gameItems.ForEach(i => i.Draw()); } }
public void Move(Possition possition) { if (possition.X == 0 && possition.Y == 0) { return; } ConsoleHelper.Clear(SnakeBody.Tail.Value); SnakeBody.ReverseForEach(node => { if (node.Previous != null) { node.Value.X = node.Previous.Value.X; node.Value.Y = node.Previous.Value.Y; } }); SnakeBody.Head.Value.ChangePossition(possition); for (int i = 0; i < Foods.Count; i++) { if (Foods[i].Possition == SnakeBody.Head.Value) { Foods[i].EatFood(); Grow(possition); SpawnFood(); } } }
public void Grow(Possition possition) { var oldPOssition = SnakeBody.Head.Value; var newHead = new Node(new Possition(oldPOssition.X, oldPOssition.Y)); //newHead.Value.ChangePossition(possition); BoundriesHelper.CheckBoundaries(newHead.Value, possition); SnakeBody.AddHead(newHead); }
public Snake(Possition headPossition, Action spawnFood) { this.SpawnFood = spawnFood; SnakeBody = new LinkedList(); SnakeBody.AddHead(new Node(headPossition)); Foods = new List <Food>(); for (int i = 1; i <= 10; i++) { SnakeBody.AddLast(new Node(new Possition(headPossition.X + i, headPossition.Y))); } }
public Node(Possition value) { this.Value = value; }
public Food(Possition possition, char symbol = '#') { this.Symbol = symbol; this.Possition = possition; }