Beispiel #1
0
        public static void Main()
        {
PlayGame:

            Console.Title         = "Snake Game";
            Console.CursorVisible = false;

            ConsolePrinter.DrawField(HighScoreManager.GetCurrentScore());
            ConsolePrinter.PrintSnake(snake);

            Point oldDirection = DirectionManager.GetNextMoveDirection(ConsoleKey.RightArrow);
            Point newDirection = DirectionManager.GetNextMoveDirection(ConsoleKey.RightArrow);

            Point food = PositionManager.SpawnFood();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo currDirection = Console.ReadKey();

                    if (ConsoleKeyManager.IsArrowKey(currDirection))
                    {
                        newDirection = DirectionManager.GetNextMoveDirection(currDirection);
                    }

                    //Check if "Esc" key is pressed
                    if (currDirection.Key == ConsoleKey.Escape)
                    {
                        ConsolePrinter.PrintPauseMenu();
                        currDirection = Console.ReadKey();

                        while (!ConsoleKeyManager.PauseMenyAllowedKeys(currDirection.Key))
                        {
                            if (currDirection.Key == ConsoleKey.Escape)
                            {
                                goto Print;
                            }

                            currDirection = Console.ReadKey();
                        }
                    }

                    var isValidMove = PositionManager.CheckIfMoveIsValid(oldDirection, newDirection);

                    if (isValidMove)
                    {
                        newDirection = oldDirection.Coordinates();
                    }

                    oldDirection = newDirection.Coordinates();
                }

                ateFood = PositionManager.CheckIfSnakeAte(snake, food);

                try
                {
                    snake = SnakeManager.Update(snake, newDirection, ateFood);
                    food  = ateFood ? PositionManager.SpawnFood() : food;
                    HighScoreManager.IncreaseScore(ateFood);
                    goto Print;
                }
                catch (GameOverException goe)
                {
                    ConsolePrinter.PrintGameOverScreen(goe.Message);
                    ConsolePrinter.PrintPlayerScore(HighScoreManager.GetCurrentScore());
                    ConsolePrinter.PrintContinueScreen(Constants.ContinueMessage);

                    goto GameOver;
                }

Print:

                ConsolePrinter.DrawField(HighScoreManager.GetCurrentScore());
                ConsolePrinter.PrintFood(food);
                ConsolePrinter.PrintSnake(snake);
                SnakeSpeedManager.CurrentSpeed(ateFood);
            }

GameOver:

            ConsoleKeyInfo playerChoice = Console.ReadKey();

            if (ConsoleKeyManager.IsValidContinueGameKey(playerChoice))
            {
                goto PlayGame;
            }
            else
            {
                Environment.Exit(0);
            }
        }
        public static void GamePlay(int parsedDifficultyKey, bool areTwoPlayersSelected)
        {
            PlayerRocketManager.CreatePlayerRockets(parsedDifficultyKey, areTwoPlayersSelected);

            if (!areTwoPlayersSelected)
            {
                HighScoreManager.ResetPlayerPoints();
            }

            var changeDirection = false;

            var ballMovementSpeed = (5 * parsedDifficultyKey) + 50;
            var baseScore         = ballMovementSpeed;

            //default ball starting position
            var pongBall = new Point(Console.BufferHeight / 2, 1);

            //default forward and up direction of the pong ball
            var ballDirection = DirectionManager.GetStartingDirection();

            while (true)
            {
                ReadPlayersInputKey(areTwoPlayersSelected);

                var  isHittingFirstPlayerRocket  = BallBounceValidator.IsHittingPlayerRocket(pongBall, ballDirection, PlayerRocketManager.LeftPlayerRocket);
                bool isHittingSecondPlayerRocket = false;

                if (areTwoPlayersSelected)
                {
                    isHittingSecondPlayerRocket = BallBounceValidator.IsHittingPlayerRocket(pongBall, ballDirection, PlayerRocketManager.RightPlayerRocket);
                }

                if ((isHittingFirstPlayerRocket || isHittingSecondPlayerRocket) && changeDirection)
                {
                    ballMovementSpeed -= (int)0.5;

                    if (!areTwoPlayersSelected)
                    {
                        HighScoreManager.IncreasePlayerScore(baseScore);
                    }
                    else if (BallBounceValidator.IsHittingEdge(pongBall, ballDirection, areTwoPlayersSelected))
                    {
                        ballDirection = DirectionManager.GetReversedDiagonalDirection(pongBall, ballDirection);
                    }

                    ballDirection = DirectionManager.GetDiagonalDirection(pongBall, ballDirection);
                }
                else if (BallBounceValidator.IsHittingBorder(pongBall, ballDirection, areTwoPlayersSelected))
                {
                    ballDirection = DirectionManager.GetDiagonalDirection(pongBall, ballDirection);
                }

                try
                {
                    ConsolePrinter.DeleteElement(pongBall.Y, pongBall.X);
                    pongBall = BallManager.MoveBall(pongBall, ballDirection);
                }
                catch (GameOverException ex)
                {
                    if (areTwoPlayersSelected)
                    {
                        ConsolePrinter.PrintGameOverScreen(ex.Message, ex.InnerException.Message);
                    }
                    else
                    {
                        ConsolePrinter.PrintGameOverScreen(ex.Message);
                    }

                    break;
                }

                changeDirection = true;

                ConsolePrinter.PrintPlayerRocket(PlayerRocketManager.LeftPlayerRocket);
                ConsolePrinter.PrintPongBall(pongBall);

                if (areTwoPlayersSelected)
                {
                    ConsolePrinter.PrintPlayerRocket(PlayerRocketManager.RightPlayerRocket);
                }
                else
                {
                    ConsolePrinter.PrintPlayerScore(HighScoreManager.GetPlayerScore);
                }

                Thread.Sleep(ballMovementSpeed);
            }
        }