コード例 #1
0
        private static void ReadPlayersInputKey(bool areTwoPlayersSelected)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo movementDirectionKey = Console.ReadKey(true);

                Console.CursorVisible = false;

                if (GameKeyAuthenticator.IsArrowKey(movementDirectionKey))
                {
                    var newDirection = DirectionManager.GetNextDirection(movementDirectionKey);

                    var elementToDelete = new Point(0, 0);

                    if (areTwoPlayersSelected)
                    {
                        if (movementDirectionKey.Key == ConsoleKey.W || movementDirectionKey.Key == ConsoleKey.S)
                        {
                            elementToDelete = UpdateLeftRocket(true, newDirection);
                        }
                        else
                        {
                            elementToDelete = UpdateLeftRocket(false, newDirection);
                        }
                    }
                    else if (!areTwoPlayersSelected)
                    {
                        elementToDelete = UpdateLeftRocket(true, newDirection);
                    }
                }
            }
        }
コード例 #2
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);
            }
        }