示例#1
0
 public void Run()
 {
     ball.Move();
     ball.Bounce();
     ball.Draw();
     CheckForScore();
     paddle.Draw();
 }
示例#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)
        {
            inputState.Update();

            // Exit game
            if (inputState.CurrentGamePadState.Buttons.Back == ButtonState.Pressed || inputState.CurrentKeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // Update GameObjects
            paddleOne.Update(gameTime, inputState, GraphicsDevice);
            paddleTwo.Update(gameTime, inputState, GraphicsDevice);
            ball.Update(gameTime);

            // Check collisions
            if (ball.BoundingBox.Intersects(paddleOne.BoundingBox) || ball.BoundingBox.Intersects(paddleTwo.BoundingBox))
            {
                ball.Bounce(true);
            }

            if (ball.BoundingBox.Intersects(topWall) || ball.BoundingBox.Intersects(bottomWall))
            {
                ball.Bounce(false);
            }

            // Winning conditions
            if (ball.BoundingBox.Intersects(leftWall))
            {
                paddleTwo.Score++;
                ball.Reset(GraphicsDevice);
            }

            if (ball.BoundingBox.Intersects(rightWall))
            {
                paddleOne.Score++;
                ball.Reset(GraphicsDevice);
            }

            base.Update(gameTime);
        }
示例#3
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ball.boundingBox.IntersectsWith(paddle1.boundingBox))
            {
                Point offset = ball.Bounce();
                xVelocity = xVelocity * -1;
                ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
            }
            else if (ball.boundingBox.IntersectsWith(paddle2.boundingBox))
            {
                Point offset = ball.Bounce();
                xVelocity = xVelocity * -1;
                ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
            }
            else if (ball.boundingBox.IntersectsWith(rightSide))
            {
                if (xVelocity > 0)
                {
                    Point offset = ball.Bounce();
                    xVelocity = xVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                    scoreKeeper.Player1Goal();

                    Player1Score.Text = scoreKeeper.player1Score.ToString();
                    if (scoreKeeper.player1Score == 15)
                    {
                        resetGame();
                    }
                    else
                    {
                        ball.Reset();
                    }
                }
            }
            else if (ball.boundingBox.IntersectsWith(leftSide))
            {
                if (xVelocity < 0)
                {
                    Point offset = ball.Bounce();
                    xVelocity = xVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                    scoreKeeper.Player2Goal();
                    Player2Score.Text = scoreKeeper.player2Score.ToString();
                    if (scoreKeeper.player2Score == 15)
                    {
                        resetGame();
                    }
                    else
                    {
                        ball.Reset();
                    }
                }
            }
            else if (ball.boundingBox.IntersectsWith(bottomSide))
            {
                if (yVelocity > 0)
                {
                    Point offset = ball.Bounce();
                    yVelocity = yVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                }
            }
            else if (ball.boundingBox.IntersectsWith(topSide))
            {
                if (yVelocity < 0)
                {
                    Point offset = ball.Bounce();
                    yVelocity = yVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                }
            }
            if (paddle2.boundingBox.Y < ball.currentLocation.Y)
            {
                if (!paddle2.boundingBox.IntersectsWith(bottomSide))
                {
                    paddle2.MovePaddleDown(PADDLE_SPEED);
                }
            }
            else if (paddle2.boundingBox.Y > ball.currentLocation.Y)
            {
                if (!paddle2.boundingBox.IntersectsWith(topSide))
                {
                    paddle2.MovePaddleUp(PADDLE_SPEED);
                }
            }
            ball.MoveBall(2 * xVelocity, 1 * yVelocity);
            this.Refresh();
        }