- XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
- Kurt Jaegers
- 258字
- 2021-08-20 15:50:41
Time for action – handling mouse input
- Add the
HandleMouseInput()
helper method to theGame1
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.
- 漫話大數(shù)據(jù)
- Word 2010中文版完全自學(xué)手冊
- Visual Studio 2015 Cookbook(Second Edition)
- 數(shù)亦有道:Python數(shù)據(jù)科學(xué)指南
- ZeroMQ
- Python金融數(shù)據(jù)分析(原書第2版)
- “互聯(lián)網(wǎng)+”時代立體化計算機組
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- 視覺大數(shù)據(jù)智能分析算法實戰(zhàn)
- Oracle數(shù)據(jù)庫管理、開發(fā)與實踐
- 數(shù)據(jù)庫與數(shù)據(jù)處理:Access 2010實現(xiàn)
- Access 2016數(shù)據(jù)庫應(yīng)用基礎(chǔ)
- AndEngine for Android Game Development Cookbook
- PostgreSQL高可用實戰(zhàn)
- 數(shù)據(jù)中臺實戰(zhàn):手把手教你搭建數(shù)據(jù)中臺