figure out the width and height of each grid cell in pixels. Then to determine which grid coordinate you are located in, just use gridx = floor(x/cellwidth) and gridy = floor(y/cellheight). To get the upper-left corner coordinate of a grid cell, just multiply the result by the cell height/width. cellx - gridx * cellwidth and celly = gridy * cellheight
example using a cell size of 40x40
// Project: test
// Created: 2018-12-02
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "test" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
DrawEllipse(20,20,20,20,MakeColor(255,0,0),MakeColor(0,0,255),1)
sprite = createsprite(GetImage(0,0,40,40))
SetSpriteSize(sprite,40,40)
do
for x = 0 to 1024 step 40
DrawLine(x,0,x,769,0xffffffff,0xffffffff)
next
for y = 0 to 768 step 40
drawline(0,y,1024,y,0xffffffff,0xffffffff)
next
gridx = floor(GetPointerX()/40)
gridy = floor(GetPointerY()/40)
cellx = gridx * 40
celly = gridy * 40
SetSpritePosition(sprite,cellx,celly)
Print( ScreenFPS() )
Sync()
loop