Esempio n. 1
0
        public Game(Options options, Berry berry, Border border, GameBorder gameBorder, Movement movement, )
        {
            var gameover = false;
            var xPosBody = new List <int>();
            var yPosBody = new List <int>();

            double gameSpeed = 150;

            int BerrysEaten = 0;

            var currentMovement = Movement.Right;

            while (true)
            {
                gameborder.Clear(options);
                border.Render();
                berry.Render();
                snakeHead.Render();

                if (snakeHead.Hits(border))
                {
                    gameover = true;
                    SetCursorPosition(28, 20);
                }

                Console.ForegroundColor = options.BodyColor;
                if (snakeHead.Hits(berry))
                {
                    BerrysEaten++;
                    berry.PutBerryAtRandomPosition();
                    gameSpeed *= 0.925;
                }

                for (int i = 0; i < xPosBody.Count(); i++)
                {
                    Console.SetCursorPosition(xPosBody[i], yPosBody[i]);
                    Console.Write(options.Block);
                    if (xPosBody[i] == snakeHead.XPos && yPosBody[i] == snakeHead.YPos)
                    {
                        gameover = true;
                    }
                }



                var sw = Stopwatch.StartNew();
                while (sw.ElapsedMilliseconds <= gameSpeed)
                {
                    currentMovement = ReadMovement(currentMovement);
                }

                // Assign the current head position to the body
                xPosBody.Add(snakeHead.XPos);
                yPosBody.Add(snakeHead.YPos);

                // Move head to the next position
                switch (currentMovement)
                {
                case Movement.Up:
                    snakeHead.YPos--;
                    break;

                case Movement.Down:
                    snakeHead.YPos++;
                    break;

                case Movement.Left:
                    snakeHead.XPos--;
                    break;

                case Movement.Right:
                    snakeHead.XPos++;
                    break;
                }

                if (xPosBody.Count() > BerrysEaten)
                {
                    xPosBody.RemoveAt(0);
                    yPosBody.RemoveAt(0);
                }

                if (gameover)
                {
                    // Game over screen, shows score based on the lenght of your snake
                    SetCursorPosition(options.BoardWidth / 5, options.BoardHeight / 2);
                    WriteLine("Game over, Score: " + BerrysEaten);
                    SetCursorPosition(options.BoardWidth / 5, options.BoardHeight / 2 + 1);
                    WriteLine("Press enter to continue.");
                    BerrysEaten = 0;
                    ReadLine();
                    Clear();
                    ShowMenu(out userAction);
                }
            }
        }
Esempio n. 2
0
        public void Play()
        {
            var xPosBody = new List <int>();
            var yPosBody = new List <int>();

            var currentMovement = Movement.Right;

            while (true)
            {
                _gameBoard.Clear(_options);
                _border.Render();
                _berry.Render();
                _snakeHead.Render();

                if (_snakeHead.Hits(_border))
                {
                    gameover = true;
                    SetCursorPosition(28, 20);
                }

                ForegroundColor = _options.BodyColor;
                if (_snakeHead.Hits(_berry))
                {
                    BerrysEaten++;
                    _berry.PutBerryAtRandomPosition();
                    _options.gameSpeed *= 0.925;
                }

                for (int i = 0; i < xPosBody.Count(); i++)
                {
                    SetCursorPosition(xPosBody[i], yPosBody[i]);
                    Write(_options.SnakeBody);
                    if (xPosBody[i] == _snakeHead.XPos && yPosBody[i] == _snakeHead.YPos)
                    {
                        gameover = true;
                    }
                }

                // How often the game checks movement
                var sw = Stopwatch.StartNew();
                while (sw.ElapsedMilliseconds <= _options.gameSpeed)
                {
                    currentMovement = ReadMovement(currentMovement);
                }

                // Assign the current head position to the body
                xPosBody.Add(_snakeHead.XPos);
                yPosBody.Add(_snakeHead.YPos);

                // Move head to the next position
                switch (currentMovement)
                {
                case Movement.Up:
                    _snakeHead.YPos--;
                    break;

                case Movement.Down:
                    _snakeHead.YPos++;
                    break;

                case Movement.Left:
                    _snakeHead.XPos--;
                    break;

                case Movement.Right:
                    _snakeHead.XPos++;
                    break;
                }

                if (xPosBody.Count() > BerrysEaten)
                {
                    xPosBody.RemoveAt(0);
                    yPosBody.RemoveAt(0);
                }

                // Creates the "Game Over" screen
                if (gameover)
                {
                    SetCursorPosition(_options.BoardWidth / 13, _options.BoardHeight / 2);
                    WriteLine("Game over, the snake had an accident. Your score is: " + BerrysEaten * 100);
                    SetCursorPosition(_options.BoardWidth / 13, _options.BoardHeight / 2 + 1);
                    WriteLine("Press enter to continue.");
                    ReadLine();
                    BerrysEaten = 0;
                    Clear();
                    return;
                }
            }
        }