/// <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(); } // TODO: Add your update logic here //mouse resets score when clicked mouseStateCurrent = Mouse.GetState(); if (mouseStateCurrent.LeftButton == ButtonState.Pressed && mouseStatePrev.LeftButton == ButtonState.Released) { leftCount = rightCount = 0; } mouseStatePrev = mouseStateCurrent; left.Update(gameTime); right.Update(gameTime); mBall.Update(gameTime); // new logic if (mBall.center.X - mBall.Source.Width <= left.center.X && mBall.center.X - mBall.Source.Width >= left.center.X - CUSHION) // range "in" the paddle where the paddle can return the ball { float dist = mBall.center.Y - left.center.Y + mBall.Radius(); if (Math.Abs(dist) <= left.Source.Height / 2) { mBall.mSpeed.Y = (float)Math.Pow(Math.Abs(dist), 1.5); mBall.mDirection.X = 1; if (dist > 0) { mBall.mDirection.Y = 1; } else { mBall.mDirection.Y = -1; } mBall.mSpeed.X += X_SCALE / (Math.Abs(dist) + .5f); // the .5f is there so we don't divide by 0 } } if (mBall.center.X + mBall.Source.Width >= right.center.X && mBall.center.X + mBall.Source.Width <= right.center.X + CUSHION) { float dist = mBall.center.Y - right.center.Y + mBall.Radius(); if (Math.Abs(dist) <= right.Source.Height / 2) { mBall.mSpeed.Y = (float)Math.Pow(Math.Abs(dist), 1.5); mBall.mDirection.X = -1; if (dist > 0) { mBall.mDirection.Y = 1; } else { mBall.mDirection.Y = -1; } mBall.mSpeed.X += X_SCALE / (Math.Abs(dist) + .5f); } } if (mBall.Position.Y > GraphicsDevice.Viewport.Height - mBall.Source.Height || mBall.Position.Y < 0) //top and bottom { mBall.mDirection.Y *= -1; } if (mBall.mSpeed.X >= mBall.MaxSpeed()) { mBall.mSpeed.X = mBall.MaxSpeed(); } if (mBall.mSpeed.Y >= mBall.MaxSpeed()) { mBall.mSpeed.Y = mBall.MaxSpeed(); } if (mBall.Position.X < 0) { leftCount++; //score mBall.Position.X = GraphicsDevice.Viewport.Width / 2 - mBall.Source.Width / 2; mBall.Position.Y = GraphicsDevice.Viewport.Height / 2 - mBall.Source.Height / 2; mBall.mSpeed = Vector2.Zero; mBall.mDirection = Vector2.Zero; left.speedReset(); right.speedReset(); } if (mBall.Position.X + mBall.Source.Width > GraphicsDevice.Viewport.Width) { rightCount++; //score mBall.Position.X = GraphicsDevice.Viewport.Width / 2 - mBall.Source.Width / 2; mBall.Position.Y = GraphicsDevice.Viewport.Height / 2 - mBall.Source.Height / 2; mBall.mSpeed = Vector2.Zero; mBall.mDirection = Vector2.Zero; left.speedReset(); right.speedReset(); } //disallow paddles from going off screen if (left.Position.Y + left.Source.Height > GraphicsDevice.Viewport.Height) { left.Position.Y = GraphicsDevice.Viewport.Height - left.Source.Height; } if (left.Position.Y < 0) { left.Position.Y = 0; } if (right.Position.Y + right.Source.Height > GraphicsDevice.Viewport.Height) { right.Position.Y = GraphicsDevice.Viewport.Height - right.Source.Height; } if (right.Position.Y < 0) { right.Position.Y = 0; } //tickCounter++; //interval to increase paddle speed //if (tickCounter == 100) //{ // left.speedUp(); // right.speedUp(); // tickCounter = 0; //} base.Update(gameTime); }