home *** CD-ROM | disk | FTP | other *** search
- /*
- flippy.cwx
-
- This script plays the simple "flippy" game. Displays the rules on startup.
- You make a move by selecting the appropriate object.
-
- Copyright 1997 by TrueSpectra Inc.
-
- This code is provided purely for demonstration purposes and is not
- supported or under warranty. Feel free to modify and examine this
- example script for your own purposes.
-
- */
-
-
-
- /* Load utility functions. */
- call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
- call SysLoadFuncs
-
- /* Return character for dialogs.*/
- cr = x2c('0A');
-
-
- /* First, make sure we aren't stomping on anybody's project.*/
- if CwGetObjectCount(CwGetCurrentView()) > 0 then do
- call CwMsg "You currently have a project. Flippy needs" ,
- "an empty project."
- exit
- end
-
-
- call CwMsg "Flippy is a simple puzzle." cr "The object of the" ,
- "game is to make the center square red and all others" ,
- "green. Clicking on a square flips its colour and" ,
- "the colour of some other squares."||cr||cr||"Note:" ,
- "Photo>Graphics MUST be in Object Edit Mode for this" ,
- "game to work."
-
- call setupGame
- do until \play()
- call scramble
- end
-
- exit
-
-
- /* Game main-loop. */
- play:procedure
- moves = 0
-
- /* First, clear the initial selection. */
- selected = CwGetSelectedObject()
- if CwIsHandleValid(selected) then
- call CwDeselectObject(selected)
-
- /* Now, loop. */
- do while \isDone()
-
- /* Periodically poll until the user selects
- one of the buttons. */
- do forever
- call SysSleep 1
- selected = CwGetSelectedObject()
- if CwIsHandleValid(selected) then
- leave
- end
-
- /* Do game things with the button.*/
- call clicked selected
- moves = moves + 1
- call CwDeselectObject(selected)
- call CwWaitOnRender(CwGetCurrentView())
- end
-
- stat = RxMessageBox("You finished in" moves "moves. Play again?", ,
- "Finished!","yesno")
- return (stat = 6)
-
-
-
-
-
-
-
- /* Change the board.*/
- clicked:procedure
- parse arg button
-
- move.1 = "110110000"
- move.2 = "111000000"
- move.3 = "011011000"
- move.4 = "100100100"
- move.5 = "010111010"
- move.6 = "001001001"
- move.7 = "000110110"
- move.8 = "000000111"
- move.9 = "000011011"
-
- bname = CwGetName(button)
- bnum = substr(bname, 2, 1)
-
- say bnum move.bnum
- do i=1 to 9
- if substr(move.bnum, i, 1) then
- call buttonOp b||i, "flip"
- end
-
- return
-
-
-
-
- /* Determine if the game is over. */
- isDone:procedure
-
- state = ''
- do i=1 to 9
- state = state || buttonOp(b||i)
- end
- return ("000010000" = state)
-
-
-
-
-
- /* Set/Get a button state.*/
- buttonOp:procedure
- parse arg name, op, state
-
- red="(210,50,3)"
- green="(11,183,9)"
-
- button = CwGetHandleFromObjectName(CwGetCurrentView(), name)
- emboss = CwGetTool(CwGetHandleFromObjectName(button, "Color me"))
- color = CwGetProperty(emboss, "Color:HSV Color")
-
- /* Explicitly set the state.*/
- if op = "set" then do
- if state then
- color = red
- else
- color = green
-
- call CwSetProperty emboss, "Color:HSV Color", color
- end
-
- /* Or invert the state. */
- else if op="flip" then do
- if color = red then
- color = green
- else
- color = red
-
- call CwSetProperty emboss, "Color:HSV Color", color
- end
-
- /* Return the state.*/
- return (color=red)
-
-
-
-
-
- /* Randomize the board. */
- scramble:procedure
-
- do i=1 to 9
- call buttonOp 'b'||i, "set", random(0,1)
- end
- return
-
-
- /* Create the initial flippy setup.*/
- setupGame:procedure
-
- ah = CwGetAppHandle("Output Settings")
- maxwidth=CwGetProperty(ah, 'output size:width')
- maxheight=CwGetProperty(ah, 'output size:height')
-
- bdim = min(maxwidth, maxheight) / 3
- centerx = maxwidth / 2
- centery = maxheight / 2
-
- call createButton 'b1', centerx-bdim, centery-bdim, bdim, bdim
- call createButton 'b2', centerx, centery-bdim, bdim, bdim
- call createButton 'b3', centerx+bdim, centery-bdim, bdim, bdim
- call createButton 'b4', centerx-bdim, centery, bdim, bdim
- call createButton 'b5', centerx, centery, bdim, bdim
- call createButton 'b6', centerx+bdim, centery, bdim, bdim
- call createButton 'b7', centerx-bdim, centery+bdim, bdim, bdim
- call createButton 'b8', centerx, centery+bdim, bdim, bdim
- call createButton 'b9', centerx+bdim, centery+bdim, bdim, bdim
-
- return
-
-
-
-
- /* Create a button.*/
- createButton:procedure
- parse arg name, x, y, width, height
-
- bwidth = .1
-
- /* Create the text object:*/
- text = CwCreateEffect("Headline Text", "Solid Color")
- call CwSetProperty CwGetRegion(text), "Caption", " Flippy!!! "
- call CwSetPosition text, x, y, width - width * bwidth * 2, ,
- height - height * bwidth * 2, 0, 0
-
- /* Create the emboss object on top:*/
- emboss = CwCreateEffect("Rectangle", "emboss")
- call CwSetName emboss, "Color me"
- call CwSetPosition emboss, x, y, width, height, 0, 0
-
- /* Create the button:*/
- button = CwCreateEffect("Rectangle", "Button")
- buttonTool = CwGetTool(button)
- call CwSetProperty buttonTool, "Border Width", bwidth
- call CwSetProperty buttonTool, "Border Height", bwidth
- call CwSetPosition button, x, y, width, height, 0, 0
-
- /* Group the objects: */
- group = CwAddObjectToGroup(button, 0)
- call CwAddObjectToGroup emboss, group
- call CwAddObjectToGroup text, group
- call CwSetName group, name
-
- /* Initialize the button to green.*/
- call buttonOp name, "set", 0
-
- return
-
-