Example #1
0
        public override void gameLogic()
        {
            //ball bounces off walls and paddles
            if (ball.getX() > 390)
            {
                ball.setVelocity(-ball.getVelocityDX(), ball.getVelocityDY());

                //update score
                playerPoints++;
                //reassign text in playersScore text object to reflect this change
                playerScore.changeText(Convert.ToString(playerPoints));
            }
            if (ball.getX() < 0)
            {
                ball.setVelocity(-ball.getVelocityDX(), ball.getVelocityDY());
                //update score
                computerPoints++;
                //reassign text in computerScore text object to reflect this change
                computerScore.changeText(Convert.ToString(computerPoints));
            }
            if (ball.getY() > 390)
            {
                ball.setVelocity(ball.getVelocityDX(), -ball.getVelocityDY());
            }
            if (ball.getY() < 0)
            {
                ball.setVelocity(ball.getVelocityDX(), -ball.getVelocityDY());
            }
            if (ball.pixelCollidesWith(leftPaddle))
            {
                ball.setVelocity(-ball.getVelocityDX(), ball.getVelocityDY());
            }


            //left player moves using Q and A
            if (ball.pixelCollidesWith(rightPaddle))
            {
                ball.setVelocity(-ball.getVelocityDX(), ball.getVelocityDY());
            }
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Q))
            {
                leftPaddle.setVelocity(0, -100);
            }
            else if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.A))
            {
                leftPaddle.setVelocity(0, 100);
            }
            else
            {
                leftPaddle.setVelocity(0, 0);
            }



            //autonomouse computer paddle on right
            paddleM = rightPaddle.getY() + rightPaddle.getHeight() / 2;
            if (ball.getY() < paddleM && ball.getX() > 150)
            {
                rightPaddle.setVelocity(0, -170);
            }
            else if (ball.getY() > paddleM && ball.getX() > 150)
            {
                rightPaddle.setVelocity(0, 170);
            }
            else
            {
                rightPaddle.setVelocity(0, 0);
            }
        }