Introduction: Using a Mouse in QBasic Programs
As anyone who has programmed with QBasic knows, it is a fun, easy and ( for it's time ) quite powerful programming tool! Admittedly, it is rather slow, but given that it has never cost a dime, I suppose that's an acceptable concession. As I mentioned, QBasic includes a wide range of simple, powerful commands, although it's one glaring shortcoming is it's complete lack of mouse support!
For the longest time, I had wanted to find a way to use the mouse in my own QBasic programs. While I eventually found several fine examples of code on-line that did in fact interface with the mouse, they all seemed very complicated to me, with multiple sub-routines, functions and procedure calls. So, I decided to "boil it all down" to the simplest, bare-minimum method, and to my knowledge, what I came up with is still the quickest, easiest way to incorporate mouse functions into a QBasic program!
One might think of my code as a kind of "black box" or "plug-in" of sorts; you simply copy my "Mouse" sub-routine into your program, and it immediately provides rudimentary mouse support! Interfacing with the mouse is then accomplished by way of three simple functions; "Mouse 1" shows the mouse cursor, "Mouse 2" hides it again, and "Mouse 3" reads the mouse's current button status as well as it's location. This information is communicated to the user's program through 3 global variables; "B" represents the mouse buttons, "H" contains it's horizontal co-ordinates, and "V" returns it's vertical co-ordinates. For a 2-button mouse, the variable "B" will return the following values:
VALUE: BUTTON(S) PRESSED:
The following is the actual mouse code segment which provides mouse support:
SUB Mouse (Funk) STATIC
SHARED B, H, V
IF Funk = 1 THEN Crsr = 1
IF Funk = 2 AND Crsr = 0 THEN EXIT SUB
IF Funk = 2 AND Crsr = 1 THEN : Crsr = 0
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
B = PEEK(&HAAA)
H = (PEEK(&HBBB) + PEEK(&HBBC) * 256)
V = (PEEK(&HCCC) + PEEK(&HCCD) * 256)
The following statements should be the first two commands in any program which uses this code:
DECLARE SUB Mouse (Funk) ' Declare Mouse sub-program.
Along with this instructable, I have included a bare-bones "template" of sorts ( "MOUSE.SUB" ), which contains the Mouse sub-routine and a blank main program, where you would enter your own code, along with a few sample programs; ( "QBMOUSE.BAS", "BUTTONS.BAS" and "PONG3.BAS" ), which should give you enough examples to get you started. Should you have any questions, concerns or just gripes regarding this code, please feel free to contact me at; email@example.com. I welcome any feedback, and I hope you enjoy creating exciting new mouse-enabled QBasic programs with the help of this sub-routine. When you're ready to begin, read on to the first step, and above all...... Have Fun!!!
Step 1: Download Files
As the title of this step implies, all we'll be doing in this step is downloading the "template" file ( "MOUSE.SUB" ), which is a bare-bones skeleton file, used for creating a new mouse-aware program, and a few sample programs ( "QBMOUSE.BAS"), which demonstrates the mouse sub-routine in action! I've also included ("Buttons.bas"), a simple "menu" style program and ("Pong3.bas"), a version of the classic "Pong" game. Simply start by downloading these files into the folder on your hard-drive that contains the QBasic executable file. ( "QBASIC.EXE" )
When you're finished doing this, move on to Step 2........
Step 2: Load the Template File ( MOUSE.SUB )
Assuming that you are at least familiar with programming in QBasic, I don't imagine I need to tell you how to open an existing Basic file! However, if you'll notice, our template file, "MOUSE.SUB", has the extension ".SUB", so it won't show up in the list of basic files you may already have on your hard-drive. Therefore, you'll need to type the full name of the file into the "File Name" box, and then press "Enter" ( Or, click the "OK" button ). This is intentional; it sort of "keeps it out of the way" while you're searching through your source files, and prevents you from accidentally opening ( and editing ) the file. Plus, as you create your own sub-routines, you can build a "library" of sorts; essentially a collection of files, each with a handy, re-usable sub-routine, and give them all the ".SUB" extension. Then, you can just copy these routines & "pop" them into any future project you may create!
Once you've got the template file loaded, move on to the next step.....
Step 3: Editing the Template File
Awright, looks like we're just about ready to go! Now, the first thing you'll want to do is CHANGE THE FILE NAME! This step is important - it keeps the template file in it's original condition, so that should any thing go wrong, you'll be able to start over with the original template file. Also, you'll want to give your new file the ".BAS" extension, so that it will show up in the file list box, next time you wish to load it! To change the file name, we simply click on the "File" menu in the top left corner of the screen, and then click "Save As..." from the drop menu. This will bring up an input box, where you'll supply the name you wish to give your new project.
Now, we're ready to begin editing. To do this, simply move the cursor down to the line that reads "Enter your code here" and, you guessed it; enter your code here! As to what code you should enter, well, that depends on what type of program you wish to create, although I will typically start with the command "Mouse 1", which just displays the mouse cursor as mentioned before.
Keep in mind however, that you may not want to display the cursor at all! For example, suppose you want to create the latest, greatest SLAMMIN' new version of the arcade classic "Pong". For a game like this, you can use the mouse to move your "paddle" around, simply by reading the "H" co-ordinate ( with the command "Mouse 3" ), and then placing the paddle accordingly. In this situation, the mouse cursor would just get in the way if it were visible, so there's no need to display it at all!
For a menu- or button- driven application however, you will certainly want to display the cursor, so that you know what you are clicking on! Just remember to hide the cursor ( using "Mouse 2" ) before you send any text or graphics to the screen, so that it doesn't garble any of your artwork. You can always turn the cursor back on by calling "Mouse 1" at any time!
To make your program respond to specific actions, such as button clicks, you might use an "IF" statement like the following;
"IF B = 1 THEN......" This causes the program to do something whenever the left button is pressed. Use "B = 2" to intercept the right button, "B = 3" for both, or even "B > 0" to activate on ANY combination of buttons! See the sample file "QBMOUSE.BAS" for more examples of this and other techniques for interfacing with the mouse.
If at any point it seems that your program just isn't responding to the mouse at all, it's probably because you haven't called "Mouse 3" before trying to use the B, H or V values. Remember, you MUST read the mouse first, in order to ensure that these values are correct and current! That's why I typically make the command the first statement within a loop, just so I know the mouse variables are always up-to-date!
Another handy trick I have found is to put the following line somewhere within the main loop of your program:
"Mouse 3: Locate 1,1: Print B;H;V" This just reads the mouse and prints the values of the mouse buttons, horizontal & vertical co-ordinates up in the top left corner of the screen, so that you can instantly see what's happening with the mouse. This helps immensely with development and de-bugging, and once your project is finished, you simply delete the line, and you're all set!
That should just about do it! As I mentioned before, check out the sample program, and really try to get a handle on just what's happening with the mouse before you get started. And as always, please contact me at; firstname.lastname@example.org with any questions or comments! But above all, HAVE FUN!!!
Update: For those who find it easier to learn visually, I have just uploaded the following video to Youtube:
Please feel free to leave any questions or comments....( and TRY to stay awake! [Yawn! ;^] )