protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); //Update our Mouse UpdateMouse(); //Update our Ball UpdateBall(); base.Update(gameTime); } protected void UpdateMouse() { mouseState = Mouse.GetState(); mousePosition.X = mouseState.X; mousePosition.Y = mouseState.Y; // Restrict the Mouse so that it stays inside the current display if (mousePosition.X < 0) mousePosition.X = 0; if (mousePosition.X > Window.ClientBounds.Width) mousePosition.X = Window.ClientBounds.Width; if (mousePosition.Y < 0) mousePosition.Y = 0; if (mousePosition.Y > Window.ClientBounds.Height) mousePosition.Y = Window.ClientBounds.Height; } protected void UpdateBall() { Rectangle balBox = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ballTexture.Height, ballTexture.Width); Rectangle mouseBox = new Rectangle((int)mousePosition.X, (int)mousePosition.Y, mousePointer.Width, mousePointer.Height); if ( Intersect(balBox,mouseBox) ) { if (mouseState.LeftButton == ButtonState.Pressed) { int difference = (int) (mousePosition.X - ballPosition.X); if (difference > 0) { ballPosition.X = mousePosition.X - difference; } else { ballPosition.X = mousePosition.X + difference; } } } } public static bool Intersect(Rectangle rectangle1, Rectangle rectangle2) { if (((rectangle1.X < (rectangle2.X + rectangle2.Width)) && (rectangle2.X < (rectangle1.X + rectangle1.Width))) && (rectangle1.Y < (rectangle2.Y + rectangle2.Height))) { return (rectangle2.Y < (rectangle1.Y + rectangle1.Height)); } return false; }