上QQ阅读APP看书,第一时间看更新
- Add the
HandleMouseInput()
helper method to the Game1 class:private void HandleMouseInput(MouseState mouseState) { int x = ((mouseState.X - (int)gameBoardDisplayOrigin.X) / GamePiece.PieceWidth); int y = ((mouseState.Y - (int)gameBoardDisplayOrigin.Y) / GamePiece.PieceHeight); if ((x >= 0) && (x < GameBoard.GameBoardWidth) && (y >= 0) && (y < GameBoard.GameBoardHeight)) { if (mouseState.LeftButton == ButtonState.Pressed) { gameBoard.RotatePiece(x, y, false); timeSinceLastInput = 0.0f; } if (mouseState.RightButton == ButtonState.Pressed) { gameBoard.RotatePiece(x, y, true); timeSinceLastInput = 0.0f; } } }
The MouseState class reports the X
and Y
position of the mouse relative to the upper left corner of the window. What we really need to know is what square on the game board the mouse was over.
We calculate this by taking the mouse position and subtracting the gameBoardDisplayOrigin
from it and then dividing the remaining number by the size of a game board square.
If the resulting X
and Y
locations fall within the game board, the left and right mouse buttons are checked. If the left button is pressed, the piece is rotated counterclockwise. The right button rotates the piece clockwise. In either case, the input delay timer is reset to 0.0f
since input was just processed.