## Lock the mouse to a grid.

Pritchard
Posts: 5485
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

### Lock the mouse to a grid.

This has been posted before in the forums. I asked help on this when I was making CHAOS Map Creator, but here's a much simplified version that puts the mouse into a blocked range. I just feel it's important to have it in tips & tricks somewhere.

Code: Select all

`Type MapScreen  BlockWidth      As Integer  BlockHeight     As Integer   ScreenWidth     As Integer  ScreenHeight    As IntegerEnd TypeType Mouse  X               As Integer  Y               As Integer  Btn             As IntegerEnd TypeDim As MapScreen WndDim As Mouse Ms  '' Our current tile positions.  Locks the mouse to a grid.Dim As Integer CurTileX, CurTileYWnd.BlockWidth = 64Wnd.BlockHeight = 32Wnd.ScreenWidth = 640Wnd.ScreenHeight = 480  '' Set up the screenScreenres Wnd.ScreenWidth, Wnd.ScreenHeight, 32  '' Lock the page so we can draw on it.screenlockDo     '' Get the mouse position  GetMouse Ms.X, Ms.Y, , Ms.Btn     '' Set the current tile positions.  CurTileX = Ms.X \ Wnd.BlockWidth  CurTileY = Ms.Y \ Wnd.BlockHeight    '' Loop through all of the blocks.    '' Horizontal blocks  For XVar As Integer = 0 To (Wnd.ScreenWidth - Wnd.BlockWidth ) Step Wnd.BlockWidth      '' Vertical blocks    For YVar As Integer = 0 To (Wnd.ScreenHeight - Wnd.BlockHeight ) Step Wnd.BlockHeight             '' Draw a box around the block.      Line (XVar, YVar)-(XVar + ( Wnd.BlockWidth - 1 ), YVar + ( Wnd.BlockHeight - 1 ) ), rgb(255, 0, 0), b    Next  Next     '' Put the mouse in the proper block.    ''    '' Multiply current tile we're on by the tile's size to find the positions.    ''  Line ( CurTileX * Wnd.BlockWidth, CurTileY * Wnd.BlockHeight) - ( (CurTileX * Wnd.BlockWidth) + (Wnd.BlockWidth - 1), (CurTileY * Wnd.BlockHeight) + (Wnd.BlockHeight - 1) ), rgb(255, 255, 255), bf     '' How do we exit this program anyways? :P  Locate 1, 1  Print "Click Left-Mouse to Exit"  Print "Current Tile: " & CurTileX & ", " & CurTileY     '' Page Flip  screenunlock  screenlock  Cls   '' Mouse is in window when the click is being done.Loop Until ( ( Ms.X > 0 ) <> 0 ) And ( ( Ms.Btn And 1 ) <> 0 )screenunlock`

How it's Done:

- Integer Divide the mouse axis (either x or y) by the tile's size to find which tile we're on

- Multiply the tile we're on by tile size and we're on the tile's start position.