home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 93win / data1.cab / Basic_Plus_Examples / BSQUADCR < prev    next >
Encoding:
Text File  |  2005-03-02  |  11.5 KB  |  318 lines

  1. 10    ! ********************************************************************
  2. 20    ! Example: Bomb Squad (*CREATE Version)
  3. 30    !
  4. 40    ! This program demonstrates the use of the CLOCK widget in TIMER mode
  5. 50    ! by playing a game in which the user has to disarm a bomb by cutting
  6. 60    ! wires.  There are 10 wires - you must cut the correct four to disarm
  7. 70    ! the bomb.  Of the six wires left, four are don't-cares and two wires
  8. 80    ! cause the bomb go off immediately.
  9. 90    !
  10. 100   ! The wires are represented by 10 TOGGLEBUTTONs. This program uses the
  11. 110   ! SYSTEM widget to create the TOGGLEBUTTONs. This is convenient since
  12. 120   ! otherwise you would have to have some degree of separate code for
  13. 130   ! each TOGGLEBUTTON.  The fact that you only get one event for all
  14. 140   ! 10 TOGGLEBUTTONs is all right, too, since each time a TOGGLEBUTTON
  15. 150   ! event happens, the program scans through all the TOGGLEBUTTONs to
  16. 160   ! check their VALUEs.
  17. 170   !
  18. 180   ! ********************************************************************
  19. 190   !
  20. 200       RANDOMIZE INT(10^7*FRACT(TIMEDATE))! Set random seed
  21. 210   !
  22. 220   ! Miscellaneous general-purpose variables
  23. 230   !
  24. 240       INTEGER N,V
  25. 250       DIM S$[256],B$(1:1)[64],Eol$[2]
  26. 260       Eol$=CHR$(13)
  27. 270   !
  28. 280   ! Variables for display and widget handling
  29. 290   !
  30. 300       INTEGER Nlines,D(1:4)    ! Used for display setup
  31. 310       REAL Dw,Dh               ! Display dimensions
  32. 320       REAL Px,Py,Pw,Ph,Iw,Ih   ! Main PANEL parameters
  33. 330       REAL Gap,Bh,Bw,Ch,Cw,Cx,Cy! Button/Clock dimensions
  34. 340       REAL Prh,Prw,Prx,Pry     ! Printer widget dimensions
  35. 350   !
  36. 360   ! Various variables for playing the game
  37. 370   !
  38. 380       INTEGER Playgame         ! Indicates game in progress
  39. 390       INTEGER Wires(1:10)      ! Designates wire settings
  40. 400       INTEGER Live,Kill,Dontcare,Cut! Wire values
  41. 410       DATA 1,2,3,4
  42. 420       READ Live,Deadly,Dontcare,Cut
  43. 430       INTEGER Livewires,Lethal ! Number of live/deadly wires
  44. 440   !
  45. 450   ! Set up display
  46. 460   !
  47. 470       CLEAR SCREEN
  48. 480       STATUS CRT,13;Nlines        ! Get number of display lines
  49. 490       GESCAPE CRT,3;D(*)          ! Get BASIC display size
  50. 500       Dw=D(3)-D(1)                ! Display width
  51. 510       Dh=(D(4)-D(2))*((Nlines-7)/Nlines)! Display height above softkeys
  52. 520   !
  53. 530   ! Set up PANEL coordinates
  54. 540   !
  55. 550       Pw=Dw*.7
  56. 560       Ph=Dh*.9
  57. 570       Px=(Dw-Pw)/2
  58. 580       Py=(Dh-Ph)/2
  59. 590   !
  60. 600   ! Create a SYSTEM widget
  61. 610   !
  62. 620       COM @Sys
  63. 630       ASSIGN @Sys TO WIDGET "SYSTEM"
  64. 640   !
  65. 650   ! Create a PANEL on the SYSTEM widget with SYSTEM MENU attribute
  66. 660   !
  67. 670       CONTROL @Sys;SET ("*NAME":"Main","*CREATE":"PANEL","VISIBLE":0)
  68. 680       CONTROL @Sys;SET ("RESIZABLE":0,"MAXIMIZABLE":0)
  69. 690       CONTROL @Sys;SET ("X":Px,"Y":Py,"WIDTH":Pw,"HEIGHT":Ph)
  70. 700       CONTROL @Sys;SET ("TITLE":" Example: Bomb Squad (*CREATE)")
  71. 710       CONTROL @Sys;SET ("SYSTEM MENU":"Quit")
  72. 720   !
  73. 730   ! Get interior dimensions of PANEL
  74. 740   !
  75. 750       STATUS @Sys;RETURN ("INSIDE WIDTH":Iw,"INSIDE HEIGHT":Ih)
  76. 760   !
  77. 770   ! Set up widget coordinates and dimensions
  78. 780   !
  79. 790       Gap=Ih*.03      ! Vertical gap
  80. 800       Bh=Ih*.1        ! Button height
  81. 810       Bw=Iw*.2        ! Button width
  82. 820   !
  83. 830       Ch=Iw*.20       ! Clock height
  84. 840       Cw=Ch           ! Clock Width
  85. 850       Cx=Bw+((Iw-Bw)-Cw)/2! Clock X location
  86. 860       Cy=Gap          ! Clock Y coordinate
  87. 870   !
  88. 880       Prh=Ih-(Ch+3*Gap)! Printer height
  89. 890       Prw=((Iw-Bw)-2*Gap)! Printer width
  90. 900       Prx=Bw+Gap      ! Printer X coordinate
  91. 910       Pry=Ch+2*Gap    ! Printer Y coordinate
  92. 920   !
  93. 930   ! Set up buttons in PANEL. The SYSTEM widget is used
  94. 940   ! to good advantage here, since the same code can
  95. 950   ! create all ten TOGGLEBUTTONS.
  96. 960   !
  97. 970       FOR N=1 TO 10
  98. 980           S$=VAL$(N)
  99. 990           CONTROL @Sys;SET ("*NAME":"Main/T"&S$,"*CREATE":"TOGGLEBUTTON")
  100. 1000           CONTROL @Sys;SET ("X":0,"Y":(N-1)*Bh,"WIDTH":Bw,"HEIGHT":Bh)
  101. 1010           CONTROL @Sys;SET ("LABEL":"Wire "&S$)
  102. 1020       NEXT N
  103. 1030  !
  104. 1040  ! Create CLOCK, set up as down-counting TIMER. The TIMER
  105. 1050  ! limit does not have to be set - it is 0 by default.
  106. 1060  !
  107. 1070       CONTROL @Sys;SET ("*NAME":"Main/Clock","*CREATE":"CLOCK")
  108. 1080       CONTROL @Sys;SET ("X":Cx,"Y":Cy,"WIDTH":Cw,"HEIGHT":Ch)
  109. 1090       CONTROL @Sys;SET ("TYPE":"TIMER","TIMER DIRECTION":"DOWN")
  110. 1100  !
  111. 1110  ! Create PRINTER
  112. 1120  !
  113. 1130       CONTROL @Sys;SET ("*NAME":"Main/Printer","*CREATE":"PRINTER")
  114. 1140       CONTROL @Sys;SET ("X":Prx,"Y":Pry,"WIDTH":Prw,"HEIGHT":Prh)
  115. 1150  !
  116. 1160  ! Set up events for SYSTEM MENU, TOGGLEBUTTONS, and CLOCK
  117. 1170  !
  118. 1180       ON EVENT @Sys,"SYSTEM MENU",15 GOTO Finis
  119. 1190       ON EVENT @Sys,"CHANGED" GOSUB Cutwire
  120. 1200       ON EVENT @Sys,"TIMER" GOSUB Boomboom
  121. 1210  !
  122. 1220  ! Turn on panel
  123. 1230  !
  124. 1240       CONTROL @Sys;SET ("*NAME":"Main","VISIBLE":1)
  125. 1250  !
  126. 1260  ! Display instructions using a DIALOG
  127. 1270  !
  128. 1280       S$="Disarm the bomb before the clock times out"&Eol$
  129. 1290       S$=S$&Eol$
  130. 1300       S$=S$&"The bomb has ten wires:"&Eol$
  131. 1310       S$=S$&Eol$
  132. 1320       S$=S$&"  - 4 wires are inert."&Eol$
  133. 1330       S$=S$&"  - 4 wires must be cut to disarm the bomb."&Eol$
  134. 1340       S$=S$&"  - 2 wires are triggers: cut both, "&Eol$
  135. 1350       S$=S$&"      and the bomb goes off."&Eol$
  136. 1360       S$=S$&Eol$
  137. 1370       S$=S$&"GOOD LUCK!"&Eol$
  138. 1380  !
  139. 1390       B$(1)="Click Here To Begin Game"
  140. 1400  !
  141. 1410       DIALOG "INFORMATION",S$;SET ("TITLE":" Bomb Squad Instructions","JUSTIFICATION":"LEFT","BACKGROUND":9,"PEN":0,"DIALOG BUTTONS":B$(*))
  142. 1420  !
  143. 1430  ! Main game loop
  144. 1440  !
  145. 1450       LOOP
  146. 1460  !
  147. 1470  ! Clear PRINTER widget, set up all the "wires"
  148. 1480  !
  149. 1490           DISABLE
  150. 1491           CONTROL @Sys;SET ("STACKING ORDER":0)
  151. 1500           CONTROL @Sys;SET ("*NAME":"Main/Printer","TEXT":"")
  152. 1510           CALL Pr(" Welcome to BOMB SQUAD.")
  153. 1520           CALL Pr("")
  154. 1530  !
  155. 1540           FOR N=1 TO 10
  156. 1550               Wires(N)=Dontcare
  157. 1560               S$=VAL$(N)
  158. 1570               CONTROL @Sys;SET ("*NAME":"Main/T"&S$)
  159. 1580               CONTROL @Sys;SET ("SENSITIVE":1,"VALUE":0)
  160. 1590           NEXT N
  161. 1600  !
  162. 1610  ! Set up the deadly wires
  163. 1620  !
  164. 1630           Lethal=0
  165. 1640           REPEAT
  166. 1650               N=1+INT(10*RND)
  167. 1660               IF Wires(N)=Dontcare THEN
  168. 1670                   Wires(N)=Deadly
  169. 1680                   Lethal=Lethal+1
  170. 1690               END IF
  171. 1700           UNTIL (Lethal=2)
  172. 1710  !
  173. 1720  ! Set up the live wires
  174. 1730  !
  175. 1740           Livewires=0
  176. 1750           REPEAT
  177. 1760               N=1+INT(10*RND)
  178. 1770               IF Wires(N)=Dontcare THEN
  179. 1780                   Wires(N)=Live
  180. 1790                   Livewires=Livewires+1
  181. 1800               END IF
  182. 1810           UNTIL (Livewires=4)
  183. 1820  !
  184. 1830  ! Set the timer to 30 seconds, and start it running
  185. 1840  !
  186. 1850           CONTROL @Sys;SET ("*NAME":"Main/Clock")
  187. 1860           CONTROL @Sys;SET ("TIMER VALUE":30000,"TIMER STATE":"RUNNING")
  188. 1870  !
  189. 1880  ! Loop until game over. Note how the "Playgame" variable is
  190. 1890  ! set to 1 by EVENT-driven routines to tell the main routine
  191. 1900  ! that the game is over and that a new one should be started
  192. 1910  ! (by returning to the top of the loop).
  193. 1920  !
  194. 1930           ENABLE
  195. 1940           Playgame=0
  196. 1950           REPEAT
  197. 1960           UNTIL (Playgame=1)
  198. 1970  !
  199. 1980       END LOOP
  200. 1990       STOP
  201. 2000  !
  202. 2010  ! This routine checks the status of the "wire" togglebuttons.
  203. 2020  ! It relies on the "Wires" array to track the condition
  204. 2030  ! of the wire set at any time.
  205. 2040  !
  206. 2050  ! The following comments may help explain the routine.
  207. 2060  !
  208. 2070  Cutwire:!
  209. 2080  !
  210. 2090  ! Check status of all ten wires
  211. 2100  !
  212. 2110       FOR N=1 TO 10
  213. 2120  !
  214. 2130  ! Ignore the wire if it has been cut
  215. 2140  !
  216. 2150           IF Wires(N)<>Cut THEN
  217. 2160  !
  218. 2170  ! Otherwise, query the togglebutton value
  219. 2180  !
  220. 2190               S$=VAL$(N)
  221. 2200               CONTROL @Sys;SET ("*NAME":"Main/T"&S$)
  222. 2210               STATUS @Sys;RETURN ("VALUE":V)
  223. 2220  !
  224. 2230  ! Ignore the button if it is not set, otherwise ...
  225. 2240  !
  226. 2250               IF V=1 THEN
  227. 2260  !
  228. 2270  ! ... disable the button ...
  229. 2280  !
  230. 2290                   CONTROL @Sys;SET ("SENSITIVE":0)
  231. 2300  !
  232. 2310  ! ... and take the appropriate measures for the wire value
  233. 2320  !
  234. 2330                   SELECT Wires(N)
  235. 2340  !
  236. 2350  ! Don't care, just say so
  237. 2360  !
  238. 2370                   CASE Dontcare
  239. 2380                       CALL Pr("Inert wire.")
  240. 2390  !
  241. 2400  ! Live wire: decrement the live-wire count - if it reaches 0,
  242. 2410  ! you win. Use a DIALOG to indicate the matter and query to see
  243. 2420  ! if the user wants to play another game. If it is not 0,
  244. 2430  ! announce the wire has been cut and list the number of wires
  245. 2440  ! remaining.
  246. 2450  !
  247. 2460                   CASE Live
  248. 2470                       Livewires=Livewires-1
  249. 2480                       IF Livewires=0 THEN
  250. 2490                           CONTROL @Sys;SET ("*NAME":"Main/Clock")
  251. 2500                           CONTROL @Sys;SET ("TIMER STATE":"STOPPED")
  252. 2510                           S$="Play another game?"
  253. 2520                           DIALOG "QUESTION",S$,Btn;SET ("TITLE":" Bomb Disarmed !!")
  254. 2530                           SELECT Btn
  255. 2540                           CASE 0
  256. 2550                               Playgame=1! Start new game
  257. 2560                               RETURN
  258. 2570                           CASE 1
  259. 2580                               GOTO Finis! Quit program
  260. 2590                           END SELECT
  261. 2600                       ELSE
  262. 2610                           S$=VAL$(Livewires)
  263. 2620                           CALL Pr("LIVE WIRE -- "&S$&" wires left.")
  264. 2630                       END IF
  265. 2640  !
  266. 2650  ! Is deadly -- count down deadly wires, if zero, you are dead
  267. 2660  !
  268. 2670                   CASE Deadly
  269. 2680                       Lethal=Lethal-1
  270. 2690                       IF Lethal=0 THEN
  271. 2700                           GOSUB Boomboom
  272. 2710                           RETURN
  273. 2720                       ELSE
  274. 2730                           CALL Pr("DANGER -- trigger wire, one left!")
  275. 2740                       END IF
  276. 2750                   END SELECT
  277. 2760  !
  278. 2770  ! If you have not either won or been killed, mark this wire as
  279. 2780  ! being "cut".
  280. 2790  !
  281. 2800                   Wires(N)=Cut
  282. 2810  !
  283. 2820               END IF
  284. 2830           END IF
  285. 2840       NEXT N
  286. 2850       RETURN
  287. 2860  !
  288. 2870  ! This routine tells you that you are dead
  289. 2880  !
  290. 2890  Boomboom:!
  291. 2900       CONTROL @Sys;SET ("*NAME":"Main/Clock","TIMER STATE":"STOPPED")
  292. 2910       S$="YOU'RE DEAD!"
  293. 2920       S$=S$&Eol$&Eol$
  294. 2930       S$=S$&"Play another game?"
  295. 2940       DIALOG "QUESTION",S$,Btn;SET ("TITLE":" Bomb Exploded !!")
  296. 2950       IF Btn=0 THEN
  297. 2960           Playgame=1
  298. 2970           RETURN
  299. 2980       ELSE
  300. 2990           GOTO Finis
  301. 3000       END IF
  302. 3010       RETURN
  303. 3020  !
  304. 3030  ! Go here when done
  305. 3040  !
  306. 3050  Finis:!
  307. 3060       ASSIGN @Sys TO *! Delete SYSTEM widget
  308. 3070       END
  309. 3080  !
  310. 3090  ! *************** End of Main Program ***********************
  311. 3100  !
  312. 3110  ! Routine to print string in PRINTER widget
  313. 3120  !
  314. 3130       SUB Pr(S$)
  315. 3140           COM @Sys
  316. 3150           CONTROL @Sys;SET ("*NAME":"Main/Printer","APPEND TEXT":S$)
  317. 3160       SUBEND
  318.