static void Main(string[] args) { string segChar = "0"; SnakeSegment[] segments = new SnakeSegment[4] { new SnakeSegment(3, 7, "#"), new SnakeSegment(3, 6, segChar), new SnakeSegment(3, 5, segChar), new SnakeSegment(3, 4, segChar) }; Random rnd = new Random(); Snake snake = new Snake(ref segments, segChar); Apple apple = new Apple(10, 10, "A", rnd); Graphics g = new Graphics(15, 25, snake, apple); g.Draw(); ConsoleKeyInfo key; Direction dir = 0; while (snake.CheckSnakeAlive()) { WaitForPressKey(2, ref dir); if (dir == Direction.Right) { snake.MoveSegments(snake.GetFirstX(), snake.GetFirstY() + 1); } else if (dir == Direction.Left) { snake.MoveSegments(snake.GetFirstX(), snake.GetFirstY() - 1); } else if (dir == Direction.Up) { snake.MoveSegments(snake.GetFirstX() - 1, snake.GetFirstY()); } else if (dir == Direction.Down) { snake.MoveSegments(snake.GetFirstX() + 1, snake.GetFirstY()); } if (snake.GetFirstX() == apple.GetX() && snake.GetFirstY() == apple.GetY()) { snake.AddSegment(); apple.Relocate(); } g.Draw(); } Console.WriteLine($"GAME OVER!"); WaitForExit(); }
/// <summary> /// /// обновляет расположение объектов на экране /// /// </summary> private void Update() { for (int i = 0; i < SnakeSegment.length; i++) { gameField[snake.GetSegment(i).GetX(), snake.GetSegment(i).GetY()] = snake.GetSegment(i).GetChar(); } gameField[snake.GetSegment(SnakeSegment.length - 1).GetX(), snake.GetSegment(SnakeSegment.length - 1).GetY()] = " "; gameField[apple.GetX(), apple.GetY()] = apple.GetChar(); }