Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Console.Title         = Constants.Name;
            Console.CursorVisible = false;
            Console.SetWindowSize(Constants.PlaygroundWidth + 2 * Constants.Margin, Constants.PlaygroundHeight + 2 * Constants.Margin);
            Console.BufferWidth  = Constants.PlaygroundWidth + 2 * Constants.Margin;
            Console.BufferHeight = Constants.PlaygroundHeight + 2 * Constants.Margin;

            InitializeWorld();

            _snake.Initialize();

            while (true)
            {
                Console.SetCursorPosition(0, 0);
                Console.Write($"Score: {_score}");

                if (!_food.HasValue)
                {
                    _food = GenerateFood();
                    _food.Value.Position.Draw();
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo input;
                    do
                    {
                        Console.SetCursorPosition(Console.BufferWidth - 2, Console.BufferHeight - 1);
                        input = Console.ReadKey(false);
                    } while (Console.KeyAvailable);

                    switch (input.Key)
                    {
                    case ConsoleKey.LeftArrow:
                        _snake.Direction = Direction.Left;
                        break;

                    case ConsoleKey.RightArrow:
                        _snake.Direction = Direction.Right;
                        break;

                    case ConsoleKey.UpArrow:
                        _snake.Direction = Direction.Up;
                        break;

                    case ConsoleKey.DownArrow:
                        _snake.Direction = Direction.Down;
                        break;
                    }
                }

                if (_snake.CanEat(_food.Value))
                {
                    _snake.AddValue(_food.Value.Value);
                    _score += _food.Value.Value;
                    _food   = null;
                }

                _snake.Move();

                if (_snake.IsIntersectingItself())
                {
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Game over!");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"Your score is {_score}");

                    ConsoleKeyInfo key;
                    do
                    {
                        key = Console.ReadKey(false);
                    } while (key.Key != ConsoleKey.Enter);

                    return;
                }

                Thread.Sleep(50);
            }
        }