QBasic:Mouse
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] OverviewQBasic is a tremendous improvement over previous versions of Basic interpreters, yet it still lacks any support for mouse functions outside the QBasic editor screen itself. Fortunately, while QBasic does not include any commands for accessing the mouse in user programs, it DOES include commands for incorporating machine language code into our programs, which in turn allows us to access mouse functions by way of the sub-program listed below: SUB Mouse (Funk) SHARED B, H, V POKE 100, 184: POKE 101, Funk: POKE 102, 0 POKE 103, 205: POKE 104, 51: POKE 105, 137 POKE 106, 30: POKE 107, 170: POKE 108, 10 POKE 109, 137: POKE 110, 14: POKE 111, 187 POKE 112, 11: POKE 113, 137: POKE 114, 22 POKE 115, 204: POKE 116, 12: POKE 117, 203 CALL Absolute(100) B = PEEK(&HAAA) H = PEEK(&HBBB) + PEEK(&HBBC) * 256 V = PEEK(&HCCC) + PEEK(&HCCD) * 256 END SUB [edit] RequirementsFor this sub-program to work properly, the following items are required;
[edit] Using the Mouse Sub-programTo use this sub-program in your own QBasic programs, simply copy and paste the code into your program, along with the following definition and declaration: DEFINT A-Z 'Make variables integer by default DECLARE SUB Mouse (Funk) 'Declare Mouse functions Then, call the sub-program with a parameter of 1, 2 or 3, which corresponds to the desired mouse function as follows;
[edit] Example ProgramThe following program displays the mouse cursor, paints the screen blue,then waits for the user to press one or both of the mouse buttons. When the left button is pressed, the program draws a yellow dot under the mouse cursor. Pressing the right button clears the screen, while pressing both ends the program. ''''''''''''''''''''''''''''' Initialize Program ''''''''''''''''''''''
DEFINT A-Z 'Make variables integer by default
DECLARE SUB Mouse (Funk) 'Declare Mouse functions
SCREEN 13 'Set screen to graphics mode
PALETTE 0, 3145728 'Set background to blue
Mouse 1 'Show mouse cursor
'''''''''''''''''''''''''''''' Main Program ''''''''''''''''''''''''
DO 'Start of main program loop
Mouse 3 'Read mouse buttons & position
IF B > 0 THEN 'If a button is pressed then,
Mouse 2 'Hide mouse cursor
Mouse 3 'Read mouse buttons & position
SELECT CASE B 'Which button is pressed?
CASE 1: PSET (H \ 2, V), 14 'Left button - draw a dot
CASE 2: CLS 'Right button - clear screen
CASE 3: SYSTEM 'Both buttons - end program
END SELECT '( End of Select structure )
Mouse 1 'Show mouse cursor
END IF '( End of If structure )
LOOP 'Back to start of program
'''''''''''''''''''''''''''''' Sub-Program '''''''''''''''''''''''
SUB Mouse (Funk)
SHARED B, H, V
POKE 100, 184: POKE 101, Funk: POKE 102, 0
POKE 103, 205: POKE 104, 51: POKE 105, 137
POKE 106, 30: POKE 107, 170: POKE 108, 10
POKE 109, 137: POKE 110, 14: POKE 111, 187
POKE 112, 11: POKE 113, 137: POKE 114, 22
POKE 115, 204: POKE 116, 12: POKE 117, 203
CALL Absolute(100)
B = PEEK(&HAAA)
H = PEEK(&HBBB) + PEEK(&HBBC) * 256
V = PEEK(&HCCC) + PEEK(&HCCD) * 256
END SUB
[edit] Important Notes!
If you wish to use these names for other variables elsewhere in your program, be sure to re-name the globals variables in the mouse sub-program.
|


