Пример #1
0
        private static void Main()
        {
            _run = true;
            Console.CursorVisible = false;
            Console.Clear();
            _display = new DisplayElement[Console.WindowWidth - 2][];
            for (var item = 0; item < _display.Length; item++)
            {
                var widthCol = new DisplayElement[Console.WindowHeight - 2];
                for (var i = 0; i < widthCol.Length; i++)
                {
                    widthCol[i] = new DisplayElement {
                        Value = Empty, Point = new Point(item, i)
                    };
                    OutPutDisplayItem(widthCol[i]);
                }

                _display[item] = widthCol;
            }

            GenFood();
            _snake = new List <Point>
            {
                new Point(_display.Length / 2 - 3, (Console.WindowHeight - 2) / 2),
                new Point(_display.Length / 2 - 2, (Console.WindowHeight - 2) / 2),
                new Point(_display.Length / 2 - 1, (Console.WindowHeight - 2) / 2),
                new Point(_display.Length / 2, (Console.WindowHeight - 2) / 2),
            };
            foreach (var item in _snake)
            {
                UpdateDisplayElementFromPoint(item, SnakeChar);
            }

            var task = Task.Run(GameLoop);

            while (_run)
            {
                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey(true);

                    switch (key.Key)
                    {
                    case ConsoleKey.UpArrow:
                        if (_lastDirection != Direction.South)
                        {
                            _direction = Direction.North;
                        }

                        break;

                    case ConsoleKey.DownArrow:
                        if (_lastDirection != Direction.North)
                        {
                            _direction = Direction.South;
                        }

                        break;

                    case ConsoleKey.LeftArrow:
                        if (_lastDirection != Direction.East)
                        {
                            _direction = Direction.West;
                        }

                        break;

                    case ConsoleKey.RightArrow:
                        if (_lastDirection != Direction.West)
                        {
                            _direction = Direction.East;
                        }

                        break;
                    }
                }
            }
            Console.WriteLine($"Game Over Score:{_score}");
            return;
        }
Пример #2
0
 private static void OutPutDisplayItem(DisplayElement displayElement)
 {
     Console.SetCursorPosition(displayElement.Point.X, displayElement.Point.Y + 1);
     Console.Write(displayElement.Value);
 }