Time for action - handling mouse input

  1. 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;
    }
    }
    }
    

What just happened?

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.

Letting the player play!

Only one more section to go and you can begin playing Flood Control. We need to code the Update() method to tie together all of the game logic we have created so far.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.105.137