home *** CD-ROM | disk | FTP | other *** search
- u
- DotBASIC Tutorial
- Part 2
- by Dave Moorman
-
-
- Now it is time to go back to
- B.QUIZ and write the Event Handling
- Routine. When you run B.QUIZ and get
- to your main screen, you can stop by
- clicking Exit.
-
- 1000 qn=0
-
- 1001 qt=0
-
- 1002 sc=0
-
- These variables will keep track of
- the scores. QN is Question Number (how
- many questions have been asked). QT is
- for Questions answered correctly. SC
- is for accumulating the scores given
- by the quiz.
-
- 1003 .ss,208
-
- .SS,Page -- Screen Stash the screen
- image and its color to memory,
- beginning at the given Page number.
- Pages 208 and 216 are perfect for
- this task.
-
- 1004 .b$,"$:q.*",dv,40960,127
-
- .B$,"$:string",drive,location,#of
- filenames -- This command Bloads
- Directory. The "$:string" is just like
- what you would use with
-
- LOAD"$:wild*",8.
-
- DotBASIC already has set the variable
- DV to the current drive number. You
- have to put the directory data
- somewhere; we are using the space
- under BASIC ROM, 40960 to 45056.
- Four Kilobytes has room for as many as
- 127 file names (at 32 characters for
- each directory line).
-
- 1006 .rk,40960
-
- .RK,location -- Rack data, beginning
- at given location. Rack turns an
- Edstar file or loaded directory into
- a virtual string array. The number of
- items is put in N%.
-
- 1007 if n%=0 then:.er:return
-
- So, if N%=0 then there was nothing in
- the file (no quiz files on the disk,
- in this case).
-
- .ER -- Event screen Restore put the
- original screen back.
-
- RETURN -- Returns to the Event Driven
- loop at line 100.
-
- Otherwise...
-
- 1008 .$l,45056
-
- .$L,location -- String Link at
- location. This sets up the ability to
- store strings in memory in a format
- that can be Racked. We are using
- space under BASIC ROM beginning at
- page 176 -- 45056 to 49151.
-
- 1010 for x=1 to n%
-
- 1012 .ri,x
-
- 1014 .$p,mid$(f$,3)
-
- 1016 next
-
- Now, we go through the Racked
- data, using the FOR-NEXT loop to index
-
- .RI,index -- Rack Index. The string
- is placed in W$. If this is a
- directory (as it is in this case),
- the filenames are placed in F$.
-
- .$P,string -- String Put puts the
- string in memory, beginning at the
- location set by .$L,location. The
- result is just like data bloaded from
- an Edstar file. In this case, we are
- scrubbing off the "P." prefix on each
- filename.
-
- 1018 .rk,45056
-
- And we Rack the strings we just put
- in memory.
-
- 1020 .az,1
-
- .AZ,position -- Alphabetize Racked
- data, beginning with given string
- position.
-
- 1021 .ml,12,27,5,22,6,14,1,1,0,"",""
-
- .ML,X1,X2,Y1,Y2,BX,IC,U,H,LOC,T$,B%
- -- Here is one of the most powerful
- and useful Objects available on
- DotBASIC -- Scrolling Menu. You give
- the four coordinates to place it on
- the screen, then the colors you want
- for the BoX, ICons, Unhighlighted
- items, and Highlighted items. The
- next parameter is LOCation of the
- data -- if the data has not already
- been Racked. In this case, we use 0,
- so we do not lose the alphabetization.
- T$ and B$ can be used to put text at
- the top and/or bottom of the menu. The
- user's selection will be in W$
-
- 1022 if w$="" then:.er:return
-
- So, if W$ is empty, we return to
- the Event Driven Loop at line 100.
- Otherwise...
-
- 1023 .sr,208
-
- .SR,Page -- Screen Restore -- puts
- the screen image and color tucked
- away at Page by .SS,Page back on the
- screen. Great way to get rid of
- dialog boxes and menus.
-
- 1024 .b0,"q." + w$,dv,40960
-
- .B0,filename$,drive,location --
- Bloads the file, as places a zero
- byte at the end. The zero byte is
- required by the Rack command (in
- order to find the end of the data).
-
- 1026 .rk,40960
-
- And here we Rack the file.
- Remember, this is the quiz file you
- created with Edstar or Mr. Edstar.
-
- 1028 o=1
-
- We will use the variable O as
- OFFSET, pointing to the beginning of
- each question in the quiz.
-
- 1029 .ri,o
-
- 1030 .tx,7+128
-
- 1032 .p@,22,0,w$
-
- So, we find the first line (0=1)
- of the data.
-
- .TX,color+rev -- This sets the color
- of the text to be printed (like
- POKE646,color). Add 128 to turn on
- Reverse.
-
- .P@,X,Y,string -- Print At screen
- coordinates X,Y. Only prints strings.
-
- Now we have the quiz loaded and
- the title (the first line of the data)
- displayed. We are ready to put the
- quiz data on the screen.
-
- 1100 .ri,o+1
-
- 1101 ifleft$(w$,8)="end quiz"then7100
-
- The first thing we need to do is
- check to see if this is the end of the
- quiz, marked in the data with the
- words "end quiz". If it is, we will go
- to line 7100. (We will get to 7100
- later. If you like, add
-
- 7100 .of:stop
-
- to your program for now.)
-
-
- 1102 .tx,3+128
-
- Set the text color to Cyan
- Reversed
-
- 1104 for x=1to17
-
- 1106 .ri,x+o
-
- 1108 a$(x)=w$
-
- 1110 next
-
- Each quiz question has 17 lines.
- For simplicity, we put them in an
- array. Of course, if we are going to
- use an array with more than 10 or 11
- items, we have to DIM it:
-
- 13 dim a$(20)
-
- 1120 for x=1to5
-
- 1121 .bx,0,38,x+1,x+1,160,3
-
- 1122 .pc,x+1,a$(x)
-
- 1124 next
-
- With this, we put the question
- portion on the screen.
-
- .BX,X1,X2,Y1,Y2,SC,CO -- draws a box
- on the screen between coordinates
- X1,X2,Y1,Y2, using Screen Code SC, in
- color CO. Here we use Screen Code 160
- (reversed space) and color 3 (Cyan).
-
- 1125 z=1
-
- 1126 for x=6to15 step3
-
- We are dividing up the rest of the
- data into the four answers
-
- 1128 a(z)=val(a$(x))
-
- A(Z) holds the score value for
- each answer.
-
- 1129 z=z+1
-
- Z is the answer number.
-
- 1130 for y=1to2
-
- 1131 .bx,0,38,x+y+z+1,x+y+z+1,160,3
-
- 1132 .pc,x+y+z+1,a$(x+y)
-
- 1134 next
-
- Then we draw a box over each line
- of each answer (a good way to wipe out
- what was there before).
-
- .PC,Y,string -- Print Center on row Y.
-
- Finding out how to get the answers
- to print into the boxes on the screen
- was a matter of trial and error.
- Hence, the X+Y+Z+1. Not elegant, but
- it works (at least for the way I
- created my main screen with VDOT.)
-
- 1135 next
-
- And then the next Answer.
-
-
- POINT AND CLICK
-
- Back when we were designing the
- screen, we mentioned that Event Driven
- code does not work so well for
- everything. So we will have to create
- our own point-and-click controls. This
- is not really that hard, thanks to the
- powerful commands of DotBASIC.
-
- 1200 pokemv+1,35
-
- The first step is to set up a
- different place in memory for the
- Regions we will use in this section.
- POKE MV+1,35 works just fine.
-
- 1201 .rd,1,0,38,10,11
-
- 1202 .rd,2,0,38,14,15
-
- 1203 .rd,3,0,38,18,19
-
- 1204 .rd,4,0,38,22,23
-
- 1205 .rd,5,8,13,0,0:
-
- .RD,#,X1,X2,Y1,Y2 -- Region Define,
- given the region number (#) and the
- screen coordinates. You can check
- each defined region with .RA,#,160,7.
- This will draw a yellow box in the
- region so numbered (#). In this case,
- regions 1-4 are answer boxes, while
- region 5 is the Exit command.
-
- 1206 .bx,0,38,0,0,255,7
-
- This BoX simply makes the menu bar
- at the top of the screen nice and
- yellow.
-
- 1300 .do
-
- 1310 .ma
-
- 1340 .un cr%
-
- 1341 ?cr%:.of:stop
-
- Ah hah! A DO-LOOP! These are so
- easy to write, you hardly need Event
- Driven technology at all.
-
- Try it out. The backgrounds do not
- change with rollovers, and hotkeys are
- not enabled yet, but if you click on
- an answer box, you stop the program
- and print the variable CR%. It will
- hold the Clicked Region.
-
- .DO starts the DO-LOOP
-
- .MA is Mouse Ask -- which brings all
- the information about the current
- mouse state into DotBASIC. Of all the
- stuff .MA can tell us, all we need
- right now is CR% -- was there a click
- over a region? (The regions defined in
- 1200-1205)
-
- .UN CR% -- and we keep looping until
- there is a click over a region. When
- CR% is Not Zero, the program falls
- through -- and we will handle the
- click.
-
- But first, we want to have the
- colors of the regions change with
- roll-overs. To complicate things, we
- need regions 1 - 4 change from cyan to
- light green, and region 5 change from
- yellow to orange.
-
- This sounds like a job for an
- array -- and an array we set up at the
- beginning of the program. Lets put it
- at line number 4000, then we can GOSUB
- to it at the top of the program.
-
- 4000 dim c(1,5)
-
- 4001 for x=1to4
-
- 4002 c(0,x)=3:c(1,x)=13
-
- 4003 next
-
- 4004 c(0,5)=7:c(1,5)=8
-
- 4005 return
-
- Actually, this is VERY straight-
- forward BASIC. We need each region to
- switch between color 0 to color 1. For
- the first 4, we use the FOR-NEXT loop
- the do the assignments. C(0,x) is the
- unhighlighted color. C(1,x) is the
- hightlighted color. For region 5, line
- 4004 does it just fine.
-
- Now add
-
- 98 gosub 4000
-
- and you are ready to add VERY SIMPLE
- roll-over color changes to your point
- and click.
-
- 1330 ifrg%>0andrg%<>og then:
- .ra,og,255,c(0,og):
- .ra,rg%,255,c(1,rg%):
- og=rg%
-
- This is a long line that does a
- bunch of stuff very quickly. First, it
- checks if the mouse arrow is over any
- of the regions (RG%). The variable OG
- holds the Old Region, so if RG% is
- greater than zero and RG% does not
- equal the Old Region OG, THEN:
-
- Note the colon after THEN. This is
- required whenever THEN is followed by
- a DotCommand. And in this case, the
- command is .RA -- Region Affect.
- First, we turn the Old Region color
- to the unhighlighted color
- (c(0,region)) then turn the new
- Region to the highlighted color
- (c(1,region)). Lastly, we make Old
- Region equal to the new Region.
-
- Try it again. The effect is very
- fluid and effective.
-
- Now to add hot keys. Step one is
- to make a string with the various hot
- keys. Make sure the hot key characters
- are in the same order as the region
- numbers:
-
- 1290 kp$="1234e"+chr$(13)
-
- We put this before the DO-LOOP.
- Or, it can be put in the set-up code
- at 4000 -- anywhere where the
- concatenation is not repeated too
- often.
-
- Now, during the DO-LOOP, we add
-
- 1320 .kp,kp$
-
- 1321 if i%>0 then cr%=i%
-
- .KP,string -- KeyPress. If a key has
- been pressed that matches one of the
- characters in the string, the
- position number of the character in
- the string is returned in I%.
-
- Now here is a trick I finally
- figured out that makes adding hot keys
- easy. if I% has a value (a key being
- pressed), then put that value into CR%
- -- as if it were a Clicked Region.
- Simple, huh?
-
- After you have checked this out,
- delete line 1341, and add:
-
- 1341 if cr%=6 then cr%=og
-
- 1342 if cr%=0then1300
-
- 1345 if cr%<5 then a=cr%:goto7000
-
- Line 1341 checks to see if the
- <RETURN> has been pressed. If so,
- whatever region is in OG (Old Region)
- is put in CR%. If CR%=0 (no region has
- been selected), the program loops back
- to the DO-LOOP again. If CR% is less
- than 5, the click is one of the
- answers -- which are handled at line
- 7000. If CR%>4, then the program falls
- through.
-
- Which means Exit has been chosen.
-
- 1349 .ss,208
-
- Stash the screen at 208.
-
- 1350 .bx,11,27,9,13,160,7
-
- Draw a box.
-
- 1351 .tx,7+128
-
- Set Text color to Yellow Reversed
-
- 1352 .pc,10,"Exit this Quiz?"
-
- Print Center.
-
- 1353 .yn,17,12,215,209
-
- .YN,X,Y,C1,C2 -- Yes/No Object, at
- screen coordinates X/Y. Color is like
- the Are You Sure object -- Color +
- 208. The result is in I%
-
- 1354 ifi%then:.er:pokemv+2,2:return
-
- So, if I% >0 then the program does
- an Event screen Restore, POKE MV+2,2,
- and a RETURN to the Event Driver at
- line 100.
-
- The POKE MV+2,2 returns the number
- of Event Regions to 2.
-
- If the response is No, then we
- fall through 1353, and
-
- 1356 .sr,208:goto1300
-
- Clear the Exit box off the screen
- and loop back to the top of the
- DO-LOOP.
-
- All that is left is handling the
- scoring...
-
- 7000 sc=sc+a(a)
-
- 7001 qn=qn+1
-
- 7002 ifsgn(a(a))>0thenqt=qt+1
-
- SC holds the score, the sum of the
- scores in the quiz data. QN is
- Question Number, adding up how many
- questions have been asked. If the
- score for the answer is greater than
- 0, then QT is incremented, giving the
- sum of correctly answered questions.
-
- 7003 .ra,og,255,c(0,og):og=0
-
- Return the clicked answer box to
- the unhighlighted color. Set OG (Old
- Region) to 0.
-
- 7010 .bx,15,19,0,0,160,7
-
- Clear the place on the menu bar
- where the score is to be displayed.
-
- 7015 .tx,7+128
-
- Set Text color to Yellow Reverse
-
- 7020 .p@,15,0,str$(sc)
-
- Print At X=15, Y=0 the string of
- the accumulated score.
-
- 7030 o=o+18:goto1100
-
- Add 18 to O (the offset value for
- the quiz data) and GOTO 1100 to get
- the next question and answers.
-
- If the next question is "end
- quiz", the program jumps to line 7100:
-
- 7100 .bx,08,31,9,18,160,7
-
- 7102 .pc,10,"You have just answered"
-
- 7103 .pc,11,"all"+str$(qn)+"
- questions,"
-
- 7104 .pc,12,"with"+
- str$(int((qt/qn)*100))+"% accuracy
-
- 7106 .pc,13,"Your TOTAL SCORE is:"
-
- 7108 .pc,14,str$(sc)
-
- 7110 .pc,16,"Another Quiz?
-
- 7112 .yn,17,17,215,209
-
- 7113 if i%<>-1 then39990
-
- 7114 pokemv+2,2:.er:return
-
- Actually, with the exception of
- calculating the percentage of right
- answers, there is nothing really new
- here. Draw a box. Fill it with text
- and ask a Yes/No question. Use .YN to
- get the answer. In this case, if the
- answer is No, jump to line 39990.
- Otherwise, set Event Regions to 2, and
- return to the Event Driver.
-
-
- 39990 print" ":.br,0:.of
-
- This simply clears the screen,
- makes the border black (.BR,color),
- and turns off the Event Driver.
-
- Lines 40000-40999 contain the
- normal RETURN TO LOADSTAR code. I have
- it placed it in a file on this disk,
- "LSCONNECT". All you have to do is
- load it and list it. Then load
- QUIZ.DOT and enter each line of the
- code that is on the screen.
-
- You will also need to figure out
- how to make the Exit command connect
- to line 39990. That will be your test.
-
- Finally, GOTO60000 to save the
- program.
-
-
- There you have it -- a complete,
- 4-answer Quiz program that can have
- almost any number of questions, and
- any sort of score rewards for correct
- or incorrect answers, with any number
- of quizzes on a disk.
-
- And you have had a chance to use
- some of the most powerful features of
- DotBASIC. By the way, there is NOT a
- version of this program on this disk.
- You [have] to write it yourself.
-
- Have fun.
-
- DMM
-
-
-