/// <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) { if (!IsActive) { return; } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } /* Controllers */ KeyboardState newKeyboardState = Keyboard.GetState(); MouseState newMouseState = Mouse.GetState(); /* Process mouse move */ if (_oldMouseState.X != newMouseState.X) { if (newMouseState.X >= 0 || newMouseState.X < _screenWidth) { _paddle.MoveTo(newMouseState.X); } } /* Process left-click */ if (newMouseState.LeftButton == ButtonState.Released && _oldMouseState.LeftButton == ButtonState.Pressed && _readyToServeBall) { ServeBall(); } /* Process keyboard events */ if (newKeyboardState.IsKeyDown(Keys.Left)) { _paddle.MoveLeft(); } if (newKeyboardState.IsKeyDown(Keys.Right)) { _paddle.MoveRight(); } if (_oldKeyboardState.IsKeyUp(Keys.Space) && newKeyboardState.IsKeyDown(Keys.Space) && _readyToServeBall) { ServeBall(); } _oldMouseState = newMouseState; // this saves the old state _oldKeyboardState = newKeyboardState; if (MediaPlayer.State == MediaState.Stopped) { _currentSongIndex = _currentSongIndex + 1 % gameContent.MusicList.Count; MediaPlayer.Play(gameContent.MusicList[_currentSongIndex]); } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (IsActive == false) { return; } KeyboardState newKeyboardState = Keyboard.GetState(); MouseState newMouseState = Mouse.GetState(); // process mouse move if (oldMouseState.X != newMouseState.X) { if (newMouseState.X >= 0 || newMouseState.X < screenWidth) { paddle.MoveTo(newMouseState.X); } } // process left click if (newMouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed && oldMouseState.X == newMouseState.X && oldMouseState.Y == newMouseState.Y && readyToServeBall) { ServeBall(); } // process keyboard events if (newKeyboardState.IsKeyDown(Keys.Left)) { paddle.MoveLeft(); } if (newKeyboardState.IsKeyDown(Keys.Right)) { paddle.MoveRight(); } if (oldKeyboardState.IsKeyUp(Keys.Space) && newKeyboardState.IsKeyDown(Keys.Space) && readyToServeBall) { ServeBall(); } oldMouseState = newMouseState; oldKeyboardState = newKeyboardState; base.Update(gameTime); }
/// <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) { if (IsActive == false) { return; //our window is not active don't update } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here KeyboardState newKeyboardState = Keyboard.GetState(); MouseState newMouseState = Mouse.GetState(); //process mouse move if (oldMouseState.X != newMouseState.X) { if (newMouseState.X >= 0 && newMouseState.X < screenWidth && newMouseState.Y >= 0 && newMouseState.Y < screenHeight) { paddle.MoveTo(newMouseState.X); } } //process left-click if (newMouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed && oldMouseState.X == newMouseState.X && oldMouseState.Y == newMouseState.Y && readyToServeBall) { ServeBall(); } //process keyboard events if (newKeyboardState.IsKeyDown(Keys.Left)) { paddle.MoveLeft(); } if (newKeyboardState.IsKeyDown(Keys.Right)) { paddle.MoveRight(); } if (oldKeyboardState.IsKeyUp(Keys.Space) && newKeyboardState.IsKeyDown(Keys.Space) && readyToServeBall) { ServeBall(); } oldMouseState = newMouseState; // this saves the old state oldKeyboardState = newKeyboardState; base.Update(gameTime); }