Example #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // this check allows to freeze the game to display the victory/consolation message
            if (done == false)
            {
                // Move the ball and computer paddle
                // All the Move calls do is adjust velocity; position will be changed after
                // velocity adjustments.

                ball.Move(player_paddle, computer_paddle);
                computer_paddle.Move(ball);

                if (ball.collision_occured && ball.playit)
                {
                    ballhit.Play();
                    ball.playit = false;
                }

                //TODO: play sounds for paddle miss and kill shots
                // This will require ball and paddle to tell us when to do this

                // Now adjust postion
                ball.position            += ball.velocity;
                computer_paddle.position += computer_paddle.velocity;

                // Move the player paddle
                // Change the player paddle position using the left thumbstick, mouse or keyboard

                //Thumbstick
                // Vector2 LeftThumb = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left;
                // player_paddle.position += new Vector2(LeftThumb.X, -LeftThumb.Y) * 5;

                //  Change the player paddle position using the keyboard
                KeyboardState keyboardState = Keyboard.GetState();
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    if (player_paddle.position.Y > 0)  // don't run off the edge
                    {
                        player_paddle.position += new Vector2(0, -8);
                    }
                }
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    if (player_paddle.position.Y < (graphics.PreferredBackBufferHeight - player_paddle.size.Y)) // don't run off the edge
                    {
                        player_paddle.position += new Vector2(0, 8);
                    }
                }

                //  Make the player paddle follow the mouse, but only in Y

                //if (player_paddle.position.Y < Mouse.GetState().Y)
                //    player_paddle.position += new Vector2(0, 5);
                //if (player_paddle.position.Y > Mouse.GetState().Y)
                //    player_paddle.position += new Vector2(0, -5);
            }

            if (ball.scorePlayer > ball.scoreFinal)
            {
                victory = "Congratulations!  You Win!  Your Score: " + ball.scorePlayer + "     Computer Score: " + ball.scoreComputer;
                done    = true;
            }
            else if (ball.scoreComputer > ball.scoreFinal)
            {
                victory = "Better luck next time!  Your Score: " + ball.scorePlayer + "     Computer Score: " + ball.scoreComputer;
                done    = true;
            }

            if (done == false)
            {
                base.Update(gameTime);
            }
        }
Example #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }
            // this check allows to freeze the game to display the victory/consolation message
            if (done == false)
            {
                // Move the ball and computer paddle
                // All the Move calls do is adjust velocity; position will be changed after
                // velocity adjustments.
                ball.Move(player_paddle, computer_paddle, Border);
                computer_paddle.Move(ball);

                //Sound for when collision ball and paddle happens
                if (ball.collision_occured && ball.playit)
                {
                    ballhit.Play();
                    ball.playit = false;
                }

                //ball velocity and position. Needs work...
                ball.position            += ball.velocity;
                computer_paddle.position += computer_paddle.velocity;
                KeyboardState keyboardState  = Keyboard.GetState();
                KeyboardState keyboardState2 = Keyboard.GetState();


                if (blockUp.isDown == true)
                {
                    if (keyboardState2.IsKeyDown(Keys.Up))
                    {
                        blockUp.position += new Vector2(0, -100);
                        blockUp.isDown    = false;
                    }
                }

                if (ball.velocity.X < 0)
                {
                    blockUp.position = new Vector2(503, 0);
                }

                if (keyboardState.IsKeyDown(Keys.W))
                {
                    if (player_paddle.position.Y > 0)  // don't run off the edge
                    {
                        player_paddle.position += new Vector2(0, -8);
                    }
                }
                if (keyboardState.IsKeyDown(Keys.S))
                {
                    if (player_paddle.position.Y < (graphics.PreferredBackBufferHeight - player_paddle.size.Y)) // don't run off the edge
                    {
                        player_paddle.position += new Vector2(0, 8);
                    }
                }
            }
            if (ball.scorePlayer > ball.scoreFinal)
            {
                victory = "Congratulations!  You Win!  Your Score: " + ball.scorePlayer + "     Computer Score: " + ball.scoreComputer;
                done    = true;
            }
            else if (ball.scoreComputer > ball.scoreFinal)
            {
                victory = "Better luck next time!  Your Score: " + ball.scorePlayer + "     Computer Score: " + ball.scoreComputer;
                done    = true;
            }
            if (done == false)
            {
                base.Update(gameTime);
            }
        }