예제 #1
0
 // Renders every gameobject
 public void Render()
 {
     // Clear the window
     Console.Clear();
     RenderWalls();
     // Draw every object
     if (gameObjects.Count > 0)
     {
         foreach (var obj in gameObjects)
         {
             obj.Render();
         }
     }
     apple.Render();
     head.Render();
 }
예제 #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;
                }
            }
        }