官术网_书友最值得收藏!

Time for action – handling mouse input

  1. Add the HandleMouseInput() helper method to the Game1 class:
    Private Sub HandleMouseInput(mouseInfo As MouseState)
      Dim x, y As Integer
    
      x = mouseInfo.X - CInt(gameBoardDisplayOrigin.X)
      y = mouseInfo.Y - CInt(gameBoardDisplayOrigin.Y)
    
      x = x \ GamePiece.PieceWidth
      y = y \ GamePiece.PieceHeight
    
      If (x >= 0) And (x <= GameBoard.GameBoardWidth) And
        (y >= 0) And (y <= GameBoard.GameBoardHeight) Then
        If mouseInfo.LeftButton = ButtonState.Pressed Then
          _gameBoard.RotatePiece(x, y, False)
          timeSinceLastInput = 0
        End If
        If mouseInfo.RightButton = ButtonState.Pressed Then
          _gameBoard.RotatePiece(x, y, True)
          timeSinceLastInput = 0
        End If
      End If
    End Sub

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 break the mouse position into X and Y components and subtract the corresponding parts of gameBoardDisplayOrigin from them. Next, we use the Integer Division operator (\) to divide the result 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 counter clockwise. The right button rotates the piece clockwise. In either case, the input delay timer is reset to 0, 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.

主站蜘蛛池模板: 长葛市| 廊坊市| 仪陇县| 会理县| 石林| 延吉市| 石河子市| 金寨县| 舞阳县| 淮滨县| 诏安县| 珲春市| 炉霍县| 蒙山县| 富锦市| 正蓝旗| 清涧县| 黎城县| 周至县| 西宁市| 安西县| 云龙县| 大邑县| 兴化市| 旬邑县| 古丈县| 凌海市| 北宁市| 崇州市| 新巴尔虎左旗| 房山区| 辉县市| 定西市| 绥棱县| 璧山县| 额尔古纳市| 泾源县| 利辛县| 涪陵区| 黎平县| 西丰县|