So far our "Milo" lottery number generator just creates a set of numbers, waits for a key-press and then exits. All well and good, but what if we don't like the numbers and want another set? As it stands, we'd have to run the program all over again. A better way would be to put in a simple menu, with options for "Generate numbers" and "Exit", whereupon the user could decide what to do next while still within the program. And while we're at it, we might as well add menu options for "About Milo" and "Help".
The basic technique needed to integrate a menu into a program is called an 'event loop'. In a later lesson I'll introduce you to a real event loop (using the GETEVENT command), but to keep things simple for now we'll go for an easier to understand version.
The idea behind this sort of a loop is that the program keeps going round and round, each time waiting for you to specify what you want to do and then acting upon your wishes. See if you can understand this logic:
Start of Event Loop
Get a keystroke from the user
If it's the Menu key, display the menu
If the "Generate number" option is chosen, then do it!
If the "Exit" option is chosen, stop the program.
If the "Help" or "About" options are chosen, display the appropriate information
End of menu choices
Go back to the start of the loop
Do you see how it works? The challenge now is to somehow implement all this in our OPL code.
You'll remember we learnt all about procedures in the last tutorial. They are an excellent way of packaging code that does a particular job away on its own where it doesn't get in the way or cause confusion. The very first thing we need to do to our OPL code is make a new procedure with all the graphics setup and number generation loops tucked inside.
Put the cursor at the start of the line "setup:" and highlight down to the end of the line "ENDWH" as shown below:
'Cut' the code and move to the bottom of the file. Insert a
few extra blank lines after the last procedure and then 'Paste' the code back
in. Before the start of the pasted in section, type in a few header lines for
our new procedure:
PROC generate:
REM Generate set of 6 unique, random lottery numbers and display!
LOCAL i%
and at the end finish the procedure off with:
RETURN
ENDP
Your new generate: procedure should now look something like this:
Thus we've neatly packaged everything we need for the
generation of the random lottery numbers into this single procedure (which in
turn calls the procedures "setup" and "picknum" to do even more specific jobs)
and feel confident that we can call it up simply from the menu we're about to
create.
Move back up to the top of the program code. The "BUSY" and "GET" lines can both be deleted, as our new menu interface won't need them.
Let's put the bare bones of the system in place. Type in the following code:
WHILE 1
ENDWH
Which should leave your screen looking something like this:
Note the %g, %x etc. This technique is an OPL short-cut way
of quoting "the ASCII value of the character that follows the % sign". You
could always quote the 'raw' ASCII numbers directly, but the above convention
makes for a much more readable program.
While this code will provide our basic on-screen menu, we need to put in some lines to tell it what to do should any of the menu options be selected. We'll test j% and (depending on what it's set to) then go to the appropriate procedure. After the line with MENU in, type:
IF j%=%g
generate:
ELSEIF j%=%x
shutdown:
ELSEIF j%=%h
help:
ELSEIF j%=%a
about:
ENDIF
Astute readers will notice that we haven't actually
got any procedures called shutdown:, help: or about: yet! This is
not a problem, we'll go and make some right now...
Move to the bottom of the program, add some blank lines and then add in three simple new procedures:
PROC shutdown:
REM Shutdown the program nicely
STOP
ENDP
PROC help:
gIPRINT "Help not written yet!"
RETURN
ENDP
PROC about:
gIPRINT "Copyright Your Name Here 1998"
RETURN
ENDP
Check the screenshot if you're not too sure of how it should look:
Notice a few things:
Translate and run the program. You should see a blank screen, but don't panic! Just press the MENU key and your first menu will appear. Use the cursor keys and Enter to make your selection. After each operation press Menu again to do something else. Pretty cool, huh?
Although the menu works properly, you can't yet do Control-g
for "Generate numbers" etc. unless the menu's already displayed on screen. In
this respect it doesn't behave quite right, and we'll cover Control-key
shortcuts in a future session.
That's it for now. You've added a real, working menu system to program "Milo"! In session 7, we'll take a sideways step by sorting the generated lottery numbers into numerical order.
Go to next lesson | Programming index