Get the Mouse Cursor Position in Basic4GL

How to Get the Mouse Cursor Position in Basic4GL

The Concept

The mouse cursor position is reported by Mouse_X() and Mouse_Y(). However, these functions return the mouse position as a number between 0 and 1, so to get the real mouse cursor location, you must multiply the number by the size of the game screen — which is always 640x480.

Mouse_X() * 640
Mouse_Y() * 480

Complete Source Code

TextMode(TEXT_BUFFERED)

Do  
    Cls
        Printr "Mouse X : " ; Mouse_X() * 640
        Printr "Mouse Y : " ; Mouse_Y() * 480
    DrawText()
Loop

Saving Your Fingers

Rather than do this every time you need to get the mouse position, you can calculate it once at the beginning of your game loop and afterwards use the calculations. This will save a bit of typing and make your code a bit more understandable.

MouseX = Mouse_X() * 640
MouseY = Mouse_Y() * 480

Complete Source Code

TextMode(TEXT_BUFFERED)

Dim MouseX
Dim MouseY

Do 
    MouseX = Mouse_X() * 640
    MouseY = Mouse_Y() * 480
    Cls
        Printr "Mouse X : " ; MouseX
        Printr "Mouse Y : " ; MouseY
    DrawText()
Loop
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.