Exemplo n.º 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!!!");
            }
        }
Exemplo n.º 2
0
        public override void gameLogic()
        {
            //control left shell
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.W) && Math.Round(leftShell.getVelocityDY()) == 0)
            {
                leftShell.setVelocity(leftShell.getVelocityDX(), -191f);
            }
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.A))
            {
                leftShell.setVelocity(-55, leftShell.getVelocityDY());
            }
            else if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.D))
            {
                leftShell.setVelocity(55, leftShell.getVelocityDY());
            }
            else
            {
                leftShell.setVelocity(0, leftShell.getVelocityDY());
            }



            //control right shell
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Up) && Math.Round(rightShell.getVelocityDY()) == 0)
            {
                rightShell.setVelocity(rightShell.getVelocityDX(), -191f);
            }
            if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Left))
            {
                rightShell.setVelocity(-55, rightShell.getVelocityDY());
            }
            else if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Right))
            {
                rightShell.setVelocity(55, rightShell.getVelocityDY());
            }
            else
            {
                rightShell.setVelocity(0, rightShell.getVelocityDY());
            }

            //check ball state
            if (ball.physicsCollide(sand) && ball.getX() < 200)
            {
                //update score
                rightPoints++;
                //change score text to reflect this
                rightScore.changeText(Convert.ToString(rightPoints));
                //put ball above winner
                ball.setPosition(340, 100);
                //put both shells back at their start position
                rightShell.setPosition(340, 340);
                leftShell.setPosition(40, 340);
                //set ball's velocity to 0 so that it just falls down
                ball.setVelocity(0, 0);
            }
            if (ball.physicsCollide(sand) && ball.getX() > 200)
            {
                leftPoints++;
                leftScore.changeText(Convert.ToString(leftPoints));
                ball.setPosition(60, 100);
                rightShell.setPosition(340, 340);
                leftShell.setPosition(40, 340);
                ball.setVelocity(0, 0);
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        public override void gameLogic()
        {
            if (stage == 0)
            {
                state.changeText("C-Create Session");
                state1.changeText("J-Join Session");
                if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.C))
                {
                    this.net.CreateSession();
                    //this.net.SignInGamer();
                }
                if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.J))
                {
                    //this.net.SignInGamer();
                    this.net.FindSession();
                }
                netState.changeText(this.net.Message);
                if (this.net.netState == 1)
                {//a state of means the player has joined is in play mode
                    stage = 3;
                }
            }
            else if (stage == 3)//game stage
            {
                state.changeText(net.Message);

                //control left shell
                if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.W) && Math.Round(leftShell.getVelocityDY()) == 0)
                {
                    leftShell.setVelocity(leftShell.getVelocityDX(), -191f);
                }
                if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.A))
                {
                    leftShell.setVelocity(-55, leftShell.getVelocityDY());
                }
                else if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    leftShell.setVelocity(55, leftShell.getVelocityDY());
                }
                else
                {
                    leftShell.setVelocity(0, leftShell.getVelocityDY());
                }



                //control right shell
                if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Up) && Math.Round(rightShell.getVelocityDY()) == 0)
                {
                    rightShell.setVelocity(rightShell.getVelocityDX(), -191f);
                }
                if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Left))
                {
                    rightShell.setVelocity(-55, rightShell.getVelocityDY());
                }
                else if (this.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.Right))
                {
                    rightShell.setVelocity(55, rightShell.getVelocityDY());
                }
                else
                {
                    rightShell.setVelocity(0, rightShell.getVelocityDY());
                }

                //check ball state
                if (ball.physicsCollide(sand) && ball.getX() < 200)
                {
                    //update score
                    rightPoints++;
                    //change score text to reflect this
                    rightScore.changeText(Convert.ToString(rightPoints));
                    //put ball above winner
                    ball.setPosition(340, 100);
                    //put both shells back at their start position
                    rightShell.setPosition(340, 340);
                    leftShell.setPosition(40, 340);
                    //set ball's velocity to 0 so that it just falls down
                    ball.setVelocity(0, 0);
                }
                if (ball.physicsCollide(sand) && ball.getX() > 200)
                {
                    leftPoints++;
                    leftScore.changeText(Convert.ToString(leftPoints));
                    ball.setPosition(60, 100);
                    rightShell.setPosition(340, 340);
                    leftShell.setPosition(40, 340);
                    ball.setVelocity(0, 0);
                }


                //end of stage 3
            }
        }