Ejemplo n.º 1
0
 public SnakeGame(Options options)
 {
     _options   = options;
     _border    = new Border(options);
     _snakeHead = new SnakeHead(options);
     _berry     = new Berry(options);
     _gameBoard = new GameBoard(options);
 }
Ejemplo n.º 2
0
        private void InitializeGame()
        {
            this.SetRecord();

            this.Points = 0;
            this.random = new Random();
            this.Snake  = new Snake(0, 0);
            this.Berry  = new Berry();
            this.Berry.InstantiateBerryIfWasDestroyed();

            this.DtLastMove = DateTime.Now;
            this.Direction  = Direction.Right;

            this.SetGameSpeed();
        }
Ejemplo n.º 3
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);
                }
            }
        }
Ejemplo n.º 4
0
 public bool Hits(Berry berry) => berry._currentXPosition == XPos && berry._currentYPosition == YPos;