home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 November / PCO_1198.ISO / filesbbs / os2 / tspg202s.arj / TSPG202S.ZIP / Scripts / FLIPPY.CWX < prev    next >
Encoding:
Text File  |  1997-05-04  |  5.3 KB  |  235 lines

  1. /*
  2.   flippy.cwx
  3.  
  4.   This script plays the simple "flippy" game.  Displays the rules on startup.
  5.   You make a move by selecting the appropriate object.
  6.  
  7.  Copyright 1997 by TrueSpectra Inc.                                  
  8.                                                                      
  9.  This code is provided purely for demonstration purposes and is not  
  10.  supported or under warranty.  Feel free to modify and examine this  
  11.  example script for your own purposes.                               
  12.  
  13. */
  14.  
  15.  
  16.  
  17. /* Load utility functions. */
  18. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  19. call SysLoadFuncs
  20.  
  21. /* Return character for dialogs.*/
  22. cr = x2c('0A');
  23.  
  24.  
  25. /* First, make sure we aren't stomping on anybody's project.*/
  26. if CwGetObjectCount(CwGetCurrentView()) > 0 then do
  27.     call CwMsg "You currently have a project.  Flippy needs" ,
  28.         "an empty project."
  29.     exit
  30.     end
  31.  
  32.  
  33. call CwMsg "Flippy is a simple puzzle." cr "The object of the" ,
  34.     "game is to make the center square red and all others" ,
  35.         "green.  Clicking on a square flips its colour and" ,
  36.     "the colour of some other squares."||cr||cr||"Note:" ,
  37.     "Photo>Graphics MUST be in Object Edit Mode for this" ,
  38.     "game to work."
  39.  
  40. call setupGame
  41. do until \play()
  42.     call scramble
  43.     end
  44.  
  45. exit
  46.  
  47.  
  48. /* Game main-loop. */
  49. play:procedure
  50. moves = 0
  51.  
  52. /* First, clear the initial selection. */
  53. selected = CwGetSelectedObject()
  54. if CwIsHandleValid(selected) then
  55.     call CwDeselectObject(selected)
  56.  
  57. /* Now, loop. */
  58. do while \isDone()
  59.     
  60.     /* Periodically poll until the user selects
  61.        one of the buttons. */
  62.     do forever
  63.         call SysSleep 1
  64.         selected = CwGetSelectedObject()
  65.         if CwIsHandleValid(selected) then
  66.             leave
  67.         end
  68.     
  69.     /* Do game things with the button.*/
  70.     call clicked selected
  71.     moves = moves + 1
  72.     call CwDeselectObject(selected)
  73.     call CwWaitOnRender(CwGetCurrentView())
  74.     end
  75.  
  76. stat = RxMessageBox("You finished in" moves "moves.  Play again?", ,
  77.     "Finished!","yesno")
  78. return (stat = 6)
  79.  
  80.          
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /* Change the board.*/
  87. clicked:procedure
  88. parse arg button
  89.  
  90. move.1 = "110110000"
  91. move.2 = "111000000"
  92. move.3 = "011011000"
  93. move.4 = "100100100"
  94. move.5 = "010111010"
  95. move.6 = "001001001"
  96. move.7 = "000110110"
  97. move.8 = "000000111"
  98. move.9 = "000011011"
  99.  
  100. bname = CwGetName(button)
  101. bnum = substr(bname, 2, 1)
  102.  
  103. say bnum move.bnum
  104. do i=1 to 9
  105.     if substr(move.bnum, i, 1) then
  106.         call buttonOp b||i, "flip"
  107.     end
  108.  
  109. return
  110.  
  111.  
  112.  
  113.  
  114. /* Determine if the game is over. */
  115. isDone:procedure
  116.  
  117. state = ''
  118. do i=1 to 9
  119.     state = state || buttonOp(b||i)
  120.     end
  121. return ("000010000" = state)
  122.  
  123.  
  124.  
  125.  
  126.  
  127. /* Set/Get a button state.*/
  128. buttonOp:procedure 
  129. parse arg name, op, state
  130.  
  131. red="(210,50,3)"
  132. green="(11,183,9)"
  133.  
  134. button = CwGetHandleFromObjectName(CwGetCurrentView(), name)
  135. emboss = CwGetTool(CwGetHandleFromObjectName(button, "Color me"))
  136. color = CwGetProperty(emboss, "Color:HSV Color")
  137.  
  138. /* Explicitly set the state.*/
  139. if op = "set" then do
  140.     if state then
  141.         color = red
  142.     else 
  143.         color = green
  144.     
  145.     call CwSetProperty emboss, "Color:HSV Color", color
  146.     end
  147.  
  148. /* Or invert the state. */
  149. else if op="flip" then do
  150.     if color = red then
  151.         color = green
  152.     else
  153.         color = red
  154.  
  155.     call CwSetProperty emboss, "Color:HSV Color", color
  156.     end
  157.  
  158. /* Return the state.*/
  159. return (color=red)
  160.  
  161.  
  162.  
  163.  
  164.  
  165. /* Randomize the board. */
  166. scramble:procedure
  167.  
  168. do i=1 to 9
  169.     call buttonOp 'b'||i, "set", random(0,1)
  170.     end
  171. return
  172.  
  173.  
  174. /* Create the initial flippy setup.*/
  175. setupGame:procedure
  176.  
  177. ah = CwGetAppHandle("Output Settings")
  178. maxwidth=CwGetProperty(ah, 'output size:width')
  179. maxheight=CwGetProperty(ah, 'output size:height')
  180.  
  181. bdim = min(maxwidth, maxheight) / 3
  182. centerx = maxwidth / 2
  183. centery = maxheight / 2
  184.  
  185. call createButton 'b1', centerx-bdim, centery-bdim, bdim, bdim
  186. call createButton 'b2', centerx, centery-bdim, bdim, bdim
  187. call createButton 'b3', centerx+bdim, centery-bdim, bdim, bdim
  188. call createButton 'b4', centerx-bdim, centery, bdim, bdim
  189. call createButton 'b5', centerx, centery, bdim, bdim
  190. call createButton 'b6', centerx+bdim, centery, bdim, bdim
  191. call createButton 'b7', centerx-bdim, centery+bdim, bdim, bdim
  192. call createButton 'b8', centerx, centery+bdim, bdim, bdim
  193. call createButton 'b9', centerx+bdim, centery+bdim, bdim, bdim
  194.  
  195. return
  196.  
  197.  
  198.  
  199.  
  200. /* Create a button.*/
  201. createButton:procedure
  202. parse arg name, x, y, width, height
  203.  
  204. bwidth = .1
  205.  
  206. /* Create the text object:*/
  207. text = CwCreateEffect("Headline Text", "Solid Color")
  208. call CwSetProperty CwGetRegion(text), "Caption", " Flippy!!! "
  209. call CwSetPosition text, x, y, width - width * bwidth * 2, ,
  210.     height - height * bwidth * 2, 0, 0
  211.  
  212. /* Create the emboss object on top:*/
  213. emboss = CwCreateEffect("Rectangle", "emboss")
  214. call CwSetName emboss, "Color me"
  215. call CwSetPosition emboss, x, y, width, height, 0, 0
  216.  
  217. /* Create the button:*/
  218. button = CwCreateEffect("Rectangle", "Button")
  219. buttonTool = CwGetTool(button)
  220. call CwSetProperty buttonTool, "Border Width", bwidth
  221. call CwSetProperty buttonTool, "Border Height", bwidth
  222. call CwSetPosition button, x, y, width, height, 0, 0
  223.  
  224. /* Group the objects: */
  225. group = CwAddObjectToGroup(button, 0)
  226. call CwAddObjectToGroup emboss, group
  227. call CwAddObjectToGroup text, group
  228. call CwSetName group, name
  229.  
  230. /* Initialize the button to green.*/
  231. call buttonOp name, "set", 0
  232.  
  233. return
  234.  
  235.