예제 #1
0
        public void RunGameLoop()
        {
            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.Y)
                    {
                        PlayerOne.MoveUp();
                    }

                    if (keyInfo.Key == ConsoleKey.S)
                    {
                        PlayerOne.MoveDown(Console.WindowHeight);
                    }

                    if (keyInfo.Key == ConsoleKey.UpArrow)
                    {
                        PlayerTwo.MoveUp();
                    }

                    if (keyInfo.Key == ConsoleKey.DownArrow)
                    {
                        PlayerTwo.MoveDown(Console.WindowHeight);
                    }
                }

                Physics.MoveBall(Console.WindowWidth, Console.WindowHeight);

                if (HasPlayerOneScored())
                {
                    ResetBallPosition(Console.WindowWidth, Console.WindowHeight);
                    Ball.RightDirection = false;
                    Ball.UpDirection    = true;
                    PlayerOne.Score++;
                    ConsoleBoard.PrintPlayerScoredMessage(PlayerOne);
                    Console.ReadKey();
                }

                if (HasPlayerTwoScored())
                {
                    ResetBallPosition(Console.WindowWidth, Console.WindowHeight);
                    Ball.RightDirection = true;
                    Ball.UpDirection    = true;
                    PlayerTwo.Score++;
                    ConsoleBoard.PrintPlayerScoredMessage(PlayerTwo);
                    Console.ReadKey();
                }
                Console.Clear();
                ConsoleBoard.DrawPlayerOne(PlayerOne);
                ConsoleBoard.DrawPlayerTwo(PlayerTwo, Console.WindowWidth);
                ConsoleBoard.DrawBall(Ball);
                ConsoleBoard.PrintScore(PlayerOne, PlayerTwo);
                Thread.Sleep(60);
            }
        }