home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 245 / 245.d81 / t.tutorial2 < prev    next >
Encoding:
Text File  |  2004-01-01  |  13.4 KB  |  649 lines

  1. u
  2.           DotBASIC Tutorial
  3.                 Part 2
  4.            by Dave Moorman
  5.  
  6.  
  7.     Now it is time to go back to
  8. B.QUIZ and write the Event Handling
  9. Routine. When you run B.QUIZ and get
  10. to your main screen, you can stop by
  11. clicking Exit.
  12.  
  13. 1000 qn=0
  14.  
  15. 1001 qt=0
  16.  
  17. 1002 sc=0
  18.  
  19.     These variables will keep track of
  20. the scores. QN is Question Number (how
  21. many questions have been asked). QT is
  22. for Questions answered correctly. SC
  23. is for accumulating the scores given
  24. by the quiz.
  25.  
  26. 1003 .ss,208
  27.  
  28. .SS,Page -- Screen Stash the screen
  29. image and its color to memory,
  30. beginning at the given Page number.
  31. Pages 208 and 216 are perfect for
  32. this task.
  33.  
  34. 1004 .b$,"$:q.*",dv,40960,127
  35.  
  36. .B$,"$:string",drive,location,#of
  37. filenames -- This command Bloads
  38. Directory. The "$:string" is just like
  39. what you would use with
  40.  
  41.            LOAD"$:wild*",8.
  42.  
  43. DotBASIC already has set the variable
  44. DV to the current drive number. You
  45. have to put the directory data
  46. somewhere; we are using the space
  47. under BASIC ROM, 40960 to 45056.
  48. Four Kilobytes has room for as many as
  49. 127 file names (at 32 characters for
  50. each directory line).
  51.  
  52. 1006 .rk,40960
  53.  
  54. .RK,location -- Rack data, beginning
  55. at given location. Rack turns an
  56. Edstar file or loaded directory into
  57. a virtual string array. The number of
  58. items is put in N%.
  59.  
  60. 1007 if n%=0 then:.er:return
  61.  
  62. So, if N%=0 then there was nothing in
  63. the file (no quiz files on the disk,
  64. in this case).
  65.  
  66. .ER -- Event screen Restore put the
  67. original screen back.
  68.  
  69. RETURN -- Returns to the Event Driven
  70. loop at line 100.
  71.  
  72.     Otherwise...
  73.  
  74. 1008 .$l,45056
  75.  
  76. .$L,location -- String Link at
  77. location. This sets up the ability to
  78. store strings in memory in a format
  79. that can be Racked. We are using
  80. space under BASIC ROM beginning at
  81. page 176 -- 45056 to 49151.
  82.  
  83. 1010 for x=1 to n%
  84.  
  85. 1012 .ri,x
  86.  
  87. 1014 .$p,mid$(f$,3)
  88.  
  89. 1016 next
  90.  
  91.     Now, we go through the Racked
  92. data, using the FOR-NEXT loop to index
  93.  
  94. .RI,index -- Rack Index. The string
  95. is placed in W$. If this is a
  96. directory (as it is in this case),
  97. the filenames are placed in F$.
  98.  
  99. .$P,string -- String Put puts the
  100. string in memory, beginning at the
  101. location set by .$L,location. The
  102. result is just like data bloaded from
  103. an Edstar file. In this case, we are
  104. scrubbing off the "P." prefix on each
  105. filename.
  106.  
  107. 1018 .rk,45056
  108.  
  109. And we Rack the strings we just put
  110. in memory.
  111.  
  112. 1020 .az,1
  113.  
  114. .AZ,position -- Alphabetize Racked
  115. data, beginning with given string
  116. position.
  117.  
  118. 1021 .ml,12,27,5,22,6,14,1,1,0,"",""
  119.  
  120. .ML,X1,X2,Y1,Y2,BX,IC,U,H,LOC,T$,B%
  121. -- Here is one of the most powerful
  122. and useful Objects available on
  123. DotBASIC -- Scrolling Menu. You give
  124. the four coordinates to place it on
  125. the screen, then the colors you want
  126. for the BoX, ICons, Unhighlighted
  127. items, and Highlighted items. The
  128. next parameter is LOCation of the
  129. data -- if the data has not already
  130. been Racked. In this case, we use 0,
  131. so we do not lose the alphabetization.
  132. T$ and B$ can be used to put text at
  133. the top and/or bottom of the menu. The
  134. user's selection will be in W$
  135.  
  136. 1022 if w$="" then:.er:return
  137.  
  138.     So, if W$ is empty, we return to
  139. the Event Driven Loop at line 100.
  140. Otherwise...
  141.  
  142. 1023 .sr,208
  143.  
  144. .SR,Page -- Screen Restore -- puts
  145. the screen image and color tucked
  146. away at Page by .SS,Page back on the
  147. screen. Great way to get rid of
  148. dialog boxes and menus.
  149.  
  150. 1024 .b0,"q." + w$,dv,40960
  151.  
  152. .B0,filename$,drive,location --
  153. Bloads the file, as places a zero
  154. byte at the end. The zero byte is
  155. required by the Rack command (in
  156. order to find the end of the data).
  157.  
  158. 1026 .rk,40960
  159.  
  160.     And here we Rack the file.
  161. Remember, this is the quiz file you
  162. created with Edstar or Mr. Edstar.
  163.  
  164. 1028 o=1
  165.  
  166.     We will use the variable O as
  167. OFFSET, pointing to the beginning of
  168. each question in the quiz.
  169.  
  170. 1029 .ri,o
  171.  
  172. 1030 .tx,7+128
  173.  
  174. 1032 .p@,22,0,w$
  175.  
  176.     So, we find the first line (0=1)
  177. of the data.
  178.  
  179. .TX,color+rev -- This sets the color
  180. of the text to be printed (like
  181. POKE646,color). Add 128 to turn on
  182. Reverse.
  183.  
  184. .P@,X,Y,string -- Print At screen
  185. coordinates X,Y. Only prints strings.
  186.  
  187.     Now we have the quiz loaded and
  188. the title (the first line of the data)
  189. displayed. We are ready to put the
  190. quiz data on the screen.
  191.  
  192. 1100 .ri,o+1
  193.  
  194. 1101 ifleft$(w$,8)="end quiz"then7100
  195.  
  196.     The first thing we need to do is
  197. check to see if this is the end of the
  198. quiz, marked in the data with the
  199. words "end quiz". If it is, we will go
  200. to line 7100. (We will get to 7100
  201. later. If you like, add
  202.  
  203. 7100 .of:stop
  204.  
  205. to your program for now.)
  206.  
  207.  
  208. 1102 .tx,3+128
  209.  
  210.     Set the text color to Cyan
  211. Reversed
  212.  
  213. 1104 for x=1to17
  214.  
  215. 1106 .ri,x+o
  216.  
  217. 1108 a$(x)=w$
  218.  
  219. 1110 next
  220.  
  221.     Each quiz question has 17 lines.
  222. For simplicity, we put them in an
  223. array. Of course, if we are going to
  224. use an array with more than 10 or 11
  225. items, we have to DIM it:
  226.  
  227. 13 dim a$(20)
  228.  
  229. 1120 for x=1to5
  230.  
  231. 1121 .bx,0,38,x+1,x+1,160,3
  232.  
  233. 1122 .pc,x+1,a$(x)
  234.  
  235. 1124 next
  236.  
  237.     With this, we put the question
  238. portion on the screen.
  239.  
  240. .BX,X1,X2,Y1,Y2,SC,CO -- draws a box
  241. on the screen between coordinates
  242. X1,X2,Y1,Y2, using Screen Code SC, in
  243. color CO. Here we use Screen Code 160
  244. (reversed space) and color 3 (Cyan).
  245.  
  246. 1125 z=1
  247.  
  248. 1126 for x=6to15 step3
  249.  
  250.     We are dividing up the rest of the
  251. data into the four answers
  252.  
  253. 1128 a(z)=val(a$(x))
  254.  
  255.     A(Z) holds the score value for
  256. each answer.
  257.  
  258. 1129 z=z+1
  259.  
  260.     Z is the answer number.
  261.  
  262. 1130 for y=1to2
  263.  
  264. 1131 .bx,0,38,x+y+z+1,x+y+z+1,160,3
  265.  
  266. 1132 .pc,x+y+z+1,a$(x+y)
  267.  
  268. 1134 next
  269.  
  270.     Then we draw a box over each line
  271. of each answer (a good way to wipe out
  272. what was there before).
  273.  
  274. .PC,Y,string -- Print Center on row Y.
  275.  
  276.     Finding out how to get the answers
  277. to print into the boxes on the screen
  278. was a matter of trial and error.
  279. Hence, the X+Y+Z+1. Not elegant, but
  280. it works (at least for the way I
  281. created my main screen with VDOT.)
  282.  
  283. 1135 next
  284.  
  285.     And then the next Answer.
  286.  
  287.  
  288. POINT AND CLICK
  289.  
  290.     Back when we were designing the
  291. screen, we mentioned that Event Driven
  292. code does not work so well for
  293. everything. So we will have to create
  294. our own point-and-click controls. This
  295. is not really that hard, thanks to the
  296. powerful commands of DotBASIC.
  297.  
  298. 1200 pokemv+1,35
  299.  
  300.     The first step is to set up a
  301. different place in memory for the
  302. Regions we will use in this section.
  303. POKE MV+1,35 works just fine.
  304.  
  305. 1201 .rd,1,0,38,10,11
  306.  
  307. 1202 .rd,2,0,38,14,15
  308.  
  309. 1203 .rd,3,0,38,18,19
  310.  
  311. 1204 .rd,4,0,38,22,23
  312.  
  313. 1205 .rd,5,8,13,0,0:
  314.  
  315. .RD,#,X1,X2,Y1,Y2 -- Region Define,
  316. given the region number (#) and the
  317. screen coordinates. You can check
  318. each defined region with .RA,#,160,7.
  319. This will draw a yellow box in the
  320. region so numbered (#). In this case,
  321. regions 1-4 are answer boxes, while
  322. region 5 is the Exit command.
  323.  
  324. 1206 .bx,0,38,0,0,255,7
  325.  
  326.     This BoX simply makes the menu bar
  327. at the top of the screen nice and
  328. yellow.
  329.  
  330. 1300 .do
  331.  
  332. 1310 .ma
  333.  
  334. 1340 .un cr%
  335.  
  336. 1341 ?cr%:.of:stop
  337.  
  338.     Ah hah! A DO-LOOP! These are so
  339. easy to write, you hardly need Event
  340. Driven technology at all.
  341.  
  342.     Try it out. The backgrounds do not
  343. change with rollovers, and hotkeys are
  344. not enabled yet, but if you click on
  345. an answer box, you stop the program
  346. and print the variable CR%. It will
  347. hold the Clicked Region.
  348.  
  349. .DO starts the DO-LOOP
  350.  
  351. .MA is Mouse Ask -- which brings all
  352. the information about the current
  353. mouse state into DotBASIC. Of all the
  354. stuff .MA can tell us, all we need
  355. right now is CR% -- was there a click
  356. over a region? (The regions defined in
  357. 1200-1205)
  358.  
  359. .UN CR% -- and we keep looping until
  360. there is a click over a region. When
  361. CR% is Not Zero, the program falls
  362. through -- and we will handle the
  363. click.
  364.  
  365.     But first, we want to have the
  366. colors of the regions change with
  367. roll-overs. To complicate things, we
  368. need regions 1 - 4 change from cyan to
  369. light green, and region 5 change from
  370. yellow to orange.
  371.  
  372.     This sounds like a job for an
  373. array -- and an array we set up at the
  374. beginning of the program. Lets put it
  375. at line number 4000, then we can GOSUB
  376. to it at the top of the program.
  377.  
  378. 4000 dim c(1,5)
  379.  
  380. 4001 for x=1to4
  381.  
  382. 4002 c(0,x)=3:c(1,x)=13
  383.  
  384. 4003 next
  385.  
  386. 4004 c(0,5)=7:c(1,5)=8
  387.  
  388. 4005 return
  389.  
  390.     Actually, this is VERY straight-
  391. forward BASIC. We need each region to
  392. switch between color 0 to color 1. For
  393. the first 4, we use the FOR-NEXT loop
  394. the do the assignments. C(0,x) is the
  395. unhighlighted color. C(1,x) is the
  396. hightlighted color. For region 5, line
  397. 4004 does it just fine.
  398.  
  399.     Now add
  400.  
  401. 98 gosub 4000
  402.  
  403. and you are ready to add VERY SIMPLE
  404. roll-over color changes to your point
  405. and click.
  406.  
  407. 1330 ifrg%>0andrg%<>og then:
  408.     .ra,og,255,c(0,og):
  409.     .ra,rg%,255,c(1,rg%):
  410.     og=rg%
  411.  
  412.     This is a long line that does a
  413. bunch of stuff very quickly. First, it
  414. checks if the mouse arrow is over any
  415. of the regions (RG%). The variable OG
  416. holds the Old Region, so if RG% is
  417. greater than zero and RG% does not
  418. equal the Old Region OG, THEN:
  419.  
  420. Note the colon after THEN. This is
  421. required whenever THEN is followed by
  422. a DotCommand. And in this case, the
  423. command is .RA -- Region Affect.
  424. First, we turn the Old Region color
  425. to the unhighlighted color
  426. (c(0,region)) then turn the new
  427. Region to the highlighted color
  428. (c(1,region)). Lastly, we make Old
  429. Region equal to the new Region.
  430.  
  431.     Try it again. The effect is very
  432. fluid and effective.
  433.  
  434.     Now to add hot keys. Step one is
  435. to make a string with the various hot
  436. keys. Make sure the hot key characters
  437. are in the same order as the region
  438. numbers:
  439.  
  440. 1290 kp$="1234e"+chr$(13)
  441.  
  442.     We put this before the DO-LOOP.
  443. Or, it can be put in the set-up code
  444. at 4000 -- anywhere where the
  445. concatenation is not repeated too
  446. often.
  447.  
  448.     Now, during the DO-LOOP, we add
  449.  
  450. 1320 .kp,kp$
  451.  
  452. 1321 if i%>0 then cr%=i%
  453.  
  454. .KP,string -- KeyPress. If a key has
  455. been pressed that matches one of the
  456. characters in the string, the
  457. position number of the character in
  458. the string is returned in I%.
  459.  
  460.     Now here is a trick I finally
  461. figured out that makes adding hot keys
  462. easy. if I% has a value (a key being
  463. pressed), then put that value into CR%
  464. -- as if it were a Clicked Region.
  465. Simple, huh?
  466.  
  467.     After you have checked this out,
  468. delete line 1341, and add:
  469.  
  470. 1341 if cr%=6 then cr%=og
  471.  
  472. 1342 if cr%=0then1300
  473.  
  474. 1345 if cr%<5 then a=cr%:goto7000
  475.  
  476.     Line 1341 checks to see if the
  477. <RETURN> has been pressed. If so,
  478. whatever region is in OG (Old Region)
  479. is put in CR%. If CR%=0 (no region has
  480. been selected), the program loops back
  481. to the DO-LOOP again. If CR% is less
  482. than 5, the click is one of the
  483. answers -- which are handled at line
  484. 7000. If CR%>4, then the program falls
  485. through.
  486.  
  487.     Which means Exit has been chosen.
  488.  
  489. 1349 .ss,208
  490.  
  491.     Stash the screen at 208.
  492.  
  493. 1350 .bx,11,27,9,13,160,7
  494.  
  495.     Draw a box.
  496.  
  497. 1351 .tx,7+128
  498.  
  499.     Set Text color to Yellow Reversed
  500.  
  501. 1352 .pc,10,"Exit this Quiz?"
  502.  
  503.     Print Center.
  504.  
  505. 1353 .yn,17,12,215,209
  506.  
  507.     .YN,X,Y,C1,C2 -- Yes/No Object, at
  508. screen coordinates X/Y. Color is like
  509. the Are You Sure object -- Color +
  510. 208. The result is in I%
  511.  
  512. 1354 ifi%then:.er:pokemv+2,2:return
  513.  
  514.     So, if I% >0 then the program does
  515. an Event screen Restore, POKE MV+2,2,
  516. and a RETURN to the Event Driver at
  517. line 100.
  518.  
  519.     The POKE MV+2,2 returns the number
  520. of Event Regions to 2.
  521.  
  522.     If the response is No, then we
  523. fall through 1353, and
  524.  
  525. 1356 .sr,208:goto1300
  526.  
  527.     Clear the Exit box off the screen
  528. and loop back to the top of the
  529. DO-LOOP.
  530.  
  531.     All that is left is handling the
  532. scoring...
  533.  
  534. 7000 sc=sc+a(a)
  535.  
  536. 7001 qn=qn+1
  537.  
  538. 7002 ifsgn(a(a))>0thenqt=qt+1
  539.  
  540.     SC holds the score, the sum of the
  541. scores in the quiz data. QN is
  542. Question Number, adding up how many
  543. questions have been asked. If the
  544. score for the answer is greater than
  545. 0, then QT is incremented, giving the
  546. sum of correctly answered questions.
  547.  
  548. 7003 .ra,og,255,c(0,og):og=0
  549.  
  550.     Return the clicked answer box to
  551. the unhighlighted color. Set OG (Old
  552. Region) to 0.
  553.  
  554. 7010 .bx,15,19,0,0,160,7
  555.  
  556.     Clear the place on the menu bar
  557. where the score is to be displayed.
  558.  
  559. 7015 .tx,7+128
  560.  
  561.     Set Text color to Yellow Reverse
  562.  
  563. 7020 .p@,15,0,str$(sc)
  564.  
  565.     Print At X=15, Y=0 the string of
  566. the accumulated score.
  567.  
  568. 7030 o=o+18:goto1100
  569.  
  570.     Add 18 to O (the offset value for
  571. the quiz data) and GOTO 1100 to get
  572. the next question and answers.
  573.  
  574.     If the next question is "end
  575. quiz", the program jumps to line 7100:
  576.  
  577. 7100 .bx,08,31,9,18,160,7
  578.  
  579. 7102 .pc,10,"You have just answered"
  580.  
  581. 7103 .pc,11,"all"+str$(qn)+"
  582.     questions,"
  583.  
  584. 7104 .pc,12,"with"+
  585.     str$(int((qt/qn)*100))+"% accuracy
  586.  
  587. 7106 .pc,13,"Your TOTAL SCORE is:"
  588.  
  589. 7108 .pc,14,str$(sc)
  590.  
  591. 7110 .pc,16,"Another Quiz?
  592.  
  593. 7112 .yn,17,17,215,209
  594.  
  595. 7113 if i%<>-1 then39990
  596.  
  597. 7114 pokemv+2,2:.er:return
  598.  
  599. Actually, with the exception of
  600. calculating the percentage of right
  601. answers, there is nothing really new
  602. here. Draw a box. Fill it with text
  603. and ask a Yes/No question. Use .YN to
  604. get the answer. In this case, if the
  605. answer is No, jump to line 39990.
  606. Otherwise, set Event Regions to 2, and
  607. return to the Event Driver.
  608.  
  609.  
  610. 39990 print" ":.br,0:.of
  611.  
  612.     This simply clears the screen,
  613. makes the border black (.BR,color),
  614. and turns off the Event Driver.
  615.  
  616.     Lines 40000-40999 contain the
  617. normal RETURN TO LOADSTAR code. I have
  618. it placed it in a file on this disk,
  619. "LSCONNECT". All you have to do is
  620. load it and list it. Then load
  621. QUIZ.DOT and enter each line of the
  622. code that is on the screen.
  623.  
  624.     You will also need to figure out
  625. how to make the Exit command connect
  626. to line 39990. That will be your test.
  627.  
  628.     Finally, GOTO60000 to save the
  629. program.
  630.  
  631.  
  632.     There you have it -- a complete,
  633. 4-answer Quiz program that can have
  634. almost any number of questions, and
  635. any sort of score rewards for correct
  636. or incorrect answers, with any number
  637. of quizzes on a disk.
  638.  
  639.     And you have had a chance to use
  640. some of the most powerful features of
  641. DotBASIC. By the way, there is NOT a
  642. version of this program on this disk.
  643. You [have] to write it yourself.
  644.  
  645.     Have fun.
  646.  
  647.  DMM
  648.  
  649.  
  650.