示例#1
0
        protected override void Update(GameTime gameTime)
        {
            keyboardInputs();

            if (gameState == state.END)
            {
                timer.Start();

                if (timer.GetSeconds() == 0)
                {
                    gameState = state.START;
                    resetGameScore();
                    timer.Stop();
                }
            }

            if (gameState == state.TRANS)
            {
                timer.Start();

                if (timer.GetSeconds() == 0)
                {
                    gameState = state.PLAY;
                    resetScore();
                    timer.Stop();
                }
            }
            else if (gameState == state.SCORE)
            {
                timer.Start();

                if (timer.GetSeconds() == 0)
                {
                    gameState = state.PLAY;
                    timer.Stop();
                }
            }
            //else if (gameLevel > 0 && gameLevel <= 5)
            else if (gameState == state.PLAY)
            {
                // TODO remove this IF statement I think
                //if (currentGameLevel == 7)
                //{
                //    RestartGame();
                //currentGameLevel = 0;
                //}

                BallCollision collisionRed = AdjustBallRedPositionWithScreenBounds(ref ballRedRect, ref ballRedVelocity);
                BallCollision collisionBlue = AdjustBallBluePositionWithScreenBounds(ref ballBlueRect, ref ballBlueVelocity);

                if (collisionRed > 0)
                {
                    passedCenter = false;

                    float newY = (new Random().Next(80) + 1) / 10.0f;
                    ballRedVelocity.Y = ballRedVelocity.Y > 0 ? newY : -newY;
                }

                if (collisionBlue > 0)
                {
                    passedCenter = false;

                    float newY = (new Random().Next(80) + 1) / 10.0f;
                    ballBlueVelocity.Y = ballBlueVelocity.Y > 0 ? newY : -newY;
                }

                if (collisionRed == BallCollision.RightMiss || collisionRed == BallCollision.LeftMiss)
                {
                    //Changes the score to reflect who won the point (who missed the ball)
                    if (BallCollision.RightMiss == collisionRed)
                    {
                        player1Score++;
                        pointWinner = "Player";
                        timer = new CountdownTimer(3);
                        gameState = state.SCORE;
                    }
                    else if (BallCollision.LeftMiss == collisionRed)
                    {
                        player2Score++;
                        pointWinner = "Computer";
                        timer = new CountdownTimer(3);
                        gameState = state.SCORE;
                    }

                    RestartGame();
                } else if (collisionBlue == BallCollision.RightMiss || collisionBlue == BallCollision.LeftMiss)
                {
                    //Changes the score to reflect who won the point (who missed the ball)
                    if (BallCollision.RightMiss == collisionBlue)
                    {
                        player1Score++;
                        pointWinner = "Player";
                        timer = new CountdownTimer(3);
                        gameState = state.SCORE;
                    }
                    else if (BallCollision.LeftMiss == collisionBlue)
                    {
                        player2Score++;
                        pointWinner = "Computer";
                        timer = new CountdownTimer(3);
                        gameState = state.SCORE;
                    }

                    RestartGame();
                }

                //send blue ball out once red ball has passed the center after hitting the AI's paddle
                if (gameState == state.PLAY && (gameLevel == 4 || gameLevel == 5)) {

                    if (passedCenter == false && ballRedVelocity.X < 0 && (ballRedRect.X + kBallWidth <= GraphicsDevice.Viewport.Bounds.Center.X))
                    {
                        if (ballBlueVelocity == zeroVelocity)
                        {
                            ballBlueVelocity = RandomVelocity();
                        }
                    }
                }

                if (passedCenter == false && ballRedVelocity.X > 0 && (ballRedRect.X + kBallWidth >= GraphicsDevice.Viewport.Bounds.Center.X))
                {
                    SimulateRestOfTurn();
                    passedCenter = true;
                }

                if (passedCenter == false && ballBlueVelocity.X > 0 && (ballBlueRect.X + kBallWidth >= GraphicsDevice.Viewport.Bounds.Center.X))
                {
                    SimulateRestOfTurn();
                    passedCenter = true; //breakpoint please
                }

                int ballCenterRed = (int)predictedBallRedHeight + (kBallHeight / 2);
                int ballCenterBlue = (int)predictedBallBlueHeight + (kBallHeight / 2);

                //AI Red Paddle
                int aiPaddleCenterRed = aiPaddleRectRed.Center.Y;

                if (predictedBallRedHeight > 0 && ballCenterRed != aiPaddleCenterRed)
                {

                    if ((aiPaddleRectRed.Y + aiPaddleRectRed.Height) <= kGameHeight)
                    {
                        if (ballCenterRed < aiPaddleCenterRed)
                        {
                            aiPaddleRectRed.Y -= kMaxAIPaddleVelocity;
                        }
                        else if (ballCenterRed > aiPaddleCenterRed)
                        {
                            aiPaddleRectRed.Y += kMaxAIPaddleVelocity;
                        }

                        if (Math.Abs(ballCenterRed - aiPaddleCenterRed) < kMaxAIPaddleVelocity)
                        {
                            aiPaddleRectRed.Y = ballCenterRed - (aiPaddleRectRed.Height / 2);
                        }
                    }
                    else
                    {
                        aiPaddleRectRed.Y = kGameHeight - aiPaddleRectRed.Height;
                    }
                }

                //AI Blue Paddle
                int aiPaddleCenterBlue = aiPaddleRectBlue.Center.Y;

                if (predictedBallBlueHeight > 0 && ballCenterBlue != aiPaddleCenterBlue)
                {
                    if ((aiPaddleRectBlue.Y + aiPaddleRectBlue.Height) > kGameHeight)
                    {
                        aiPaddleRectBlue.Y = kGameHeight - aiPaddleRectBlue.Height;
                    }
                    else
                    {
                        if (ballCenterBlue < aiPaddleCenterBlue)
                        {
                            aiPaddleRectBlue.Y -= kMaxAIPaddleVelocity;
                        }
                        else if (ballCenterBlue > aiPaddleCenterBlue)
                        {
                            aiPaddleRectBlue.Y += kMaxAIPaddleVelocity;
                        }

                        if (Math.Abs(ballCenterBlue - aiPaddleCenterBlue) < kMaxAIPaddleVelocity)
                        {

                            aiPaddleRectBlue.Y = ballCenterBlue - (aiPaddleRectBlue.Height / 2);
                        }
                    }
                }
            }

            base.Update(gameTime);
        }
示例#2
0
        private void keyboardInputs()
        {
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Q))
                this.Exit();

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.N))
            {
                gameLevel = 0;
                gameState = state.START;
                gameMode = (int)playerMode.singlePlayer;
                resetGameScore();
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.NumPad1) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D1))
            {
                gameLevel = 1;
                resetScore();
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.RightShift) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.LeftShift))
                {
                    timer = new CountdownTimer(5);
                    timer.Start();
                    gameState = state.TRANS;
                }
                else
                {
                    gameState = state.PLAY;
                }
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.NumPad2) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D2))
            {
                gameLevel = 2;
                resetScore();
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.RightShift) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.LeftShift))
                {
                    timer = new CountdownTimer(5);
                    timer.Start();
                    gameState = state.TRANS;
                }
                else
                {
                    gameState = state.PLAY;
                }
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.NumPad3) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D3))
            {
                gameLevel = 3;
                resetScore();
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.RightShift) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.LeftShift))
                {
                    timer = new CountdownTimer(5);
                    timer.Start();
                    gameState = state.TRANS;
                }
                else
                {
                    gameState = state.PLAY;
                }
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.NumPad4) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D4))
            {
                gameLevel = 4;
                resetScore();
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.RightShift) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.LeftShift))
                {
                    timer = new CountdownTimer(5);
                    timer.Start();
                    gameState = state.TRANS;
                }
                else
                {
                    gameState = state.PLAY;
                }
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.NumPad5) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D5))
            {
                gameLevel = 5;
                gameState = state.PLAY;
                resetScore();
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.RightShift) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.LeftShift))
                {
                    timer = new CountdownTimer(5);
                    timer.Start();
                    gameState = state.TRANS;
                }
                else
                {
                    gameState = state.PLAY;
                }
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.NumPad6) || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D6))
            {
                gameLevel = 6;
                gameState = state.END;
                timer = new CountdownTimer(5);
                resetScore();
            }
        }
示例#3
0
        private void RestartGame()
        {
            //if (gameLevel == 0)
            if (gameState == state.START)
            {
                if (backgroundSound == null)
                {
                    backgroundSound = Content.Load<SoundEffect>("BackgroundMusic1");
                    backgroundSoundInstance = backgroundSound.CreateInstance();
                    backgroundSoundInstance.Volume = 0.5f;
                    backgroundSoundInstance.IsLooped = true;
                    backgroundSoundInstance.Play();
                }
                else if (backgroundSoundInstance.State == SoundState.Paused)
                {
                    backgroundSoundInstance.Resume();
                }
            }

            setPaddles();

            ballRedRect = new Rectangle(hMidPoint, vMidPoint, kBallWidth, kBallHeight);
            ballBlueRect = new Rectangle(hMidPoint, vMidPoint, kBallWidth, kBallHeight);

            if (player1Score >= 3)
            {
                //set text to player 1 wins
                //gameText = "Player 1 Wins!";
                ballRedVelocity = zeroVelocity;
                ballBlueVelocity = zeroVelocity;
                //Progress the game and tally "Game" score
                currentGameLevel = gameLevel;
                if (gameLevel == 5)
                {
                    //gameLevel = 6;
                    timer = new CountdownTimer(5);
                    gameState = state.END;
                    //currentGameLevel = 6;
                    backgroundSoundInstance.Pause();
                    gameoverSoundInstance.Play();
                    while (gameoverSoundInstance.State == SoundState.Playing)
                    {
                        continue;
                    }
                    backgroundSoundInstance.Resume();
                }
                else
                {
                    //gameLevel = 7;
                    gameState = state.TRANS;
                    gameLevel++;
                    setPaddles();
                    timer = new CountdownTimer(5);
                }
                player1GameScore++;
            }
            else if (player2Score >= 3)
            {
                //set text to player 2 wins
                //gameText = "AI Wins!";
                ballRedVelocity = zeroVelocity;
                ballBlueVelocity = zeroVelocity;
                //Progress the game and tally "Game" score
                currentGameLevel = gameLevel;
                if (gameLevel == 5)
                {
                    //gameLevel = 6;
                    gameState = state.END;
                    currentGameLevel = 6;
                    backgroundSoundInstance.Pause();
                    gameoverSoundInstance.Play();
                    while (gameoverSoundInstance.State == SoundState.Playing)
                    {
                        continue;
                    }
                    backgroundSoundInstance.Resume();
                }
                else
                {
                    //gameLevel = 7;
                    gameState = state.TRANS;
                    gameLevel++;
                    setPaddles();
                    timer = new CountdownTimer(5);
                }
                player2GameScore++;
            }
            else
            {
                ballRedVelocity = zeroVelocity;
                ballBlueVelocity = zeroVelocity;

                ballRedVelocity = RandomVelocity();
            }
        }