Example #1
0
        public override void gameLogic()
        {
            if (gameOver == false)
            {
                //time tracker
                timeElapsed = this.getElapsedTime();
                timer.changeText(Convert.ToString(timeElapsed) + " seconds");
            }
            //accelerate the car
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Up) == true)
            {
                line1.applyForce(0, 25);
                line2.applyForce(0, 25);
                line3.applyForce(0, 25);
                line4.applyForce(0, 25);
                kid1.applyForce(0, 25);
                kid2.applyForce(0, 25);
                flag.applyForce(0, 25);
            }
            //reposition lines when they get off screen
            if (line1.getY() > 440)
            {
                line1.setPosition(145, -40);
            }
            else if (line2.getY() > 440)
            {
                line2.setPosition(145, -40);
            }
            else if (line3.getY() > 440)
            {
                line3.setPosition(145, -40);
            }
            else if (line4.getY() > 440)
            {
                line4.setPosition(145, -40);
            }
            if (kid1.getY() > 415)
            {
                kid1.setPosition(rX.Next(100, 200), rY.Next(-200, -100));
            }
            if (kid2.getY() > 415)
            {
                kid2.setPosition(rX.Next(100, 200), rY.Next(-200, -100));
            }

            //apply brakes
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Down) == true && line1.getVelocityDY() > 0)
            {
                line1.applyForce(0, -100);
                line2.applyForce(0, -100);
                line3.applyForce(0, -100);
                line4.applyForce(0, -100);
            }
            //move car left and right
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Left) == true)
            {
                car.setVelocity(-55, 0);
            }
            else if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Right) == true)
            {
                car.setVelocity(55, 0);
            }
            else
            {
                car.setVelocity(0, 0);
            }


            //finish line
            if (car.getY() < flag.getY())
            {
                finalStatus.changeText("Race Completed in " + Convert.ToString(timeElapsed) + " seconds");
                gameOver = true;
            }
            //hitting pedestrians
            if (car.physicsCollide(kid1))
            {
                finalStatus.changeText("GAME OVER-Pedestrian Squashed!!!");
            }
        }
Example #2
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);
            }
        }