Lingo Game: Jigsaw Puzzle

File "arkas.dir" contains the basic pieces (taken from a drawing by Greek cartoonist Arkas) for making a 4x4 puzzle:

The four pieces of the puzzle (cut in Photoshop) and the complete image for reference, might be useful to align the pieces to their final positions):


The cast members have a simple script that will help us determine their final (correct) destination (to more completely understand testing functions in the Message window):

on mouseUp
   put sprite(_mouse.clickOn).locH
   put sprite(_mouse.clickOn).locV

That way, after we assemble them in their final location, we can click on them and find their coordinates.
Note that each sprite is moveable. You can set that either by clicking on the appropriate button on the properties window or by setting the property moveableSprite to TRUE in the script.

File "arkas1.dir" has the programming of the puzzle complete.
Each puzzle piece checks to see if it is within epsilon = 20 pixels to its final destination, and if so,
it is placed ("magnetized") in its correct destination.

on mouseUp
   finH = 267 -- inserted manually
   finV = 147 -- inserted manually
   currH = sprite(_mouse.clickOn).locH
   currV = sprite(_mouse.clickOn).locV
   if (abs(currH - finH) < 20) and (abs(currV - finV) < 20) then 
      sprite(_mouse.clickOn).locH = finH
      sprite(_mouse.clickOn).locV = finV
   end if

Note that a "beep" gives the positive feedback of success.
Also note that the four cast members share a large part of the script.

File "arkas2.dir" is a little prettier in terms of interface and less cumbersome to replicate using the moveAndCheck() function:

on mouseUp
   finH = 267 -- needs to be inserted manually
   finV = 147 -- needs to be inserted manually
   moveAndCheck(finH, finV)

which contains the pieces of the code that do not change.
There is also a field that is used for feedback.
A variable "success" is used to count the correctly placed pieces.
When all 4 pieced have been placed correctly, it declares success:

on startMovie
   global success
   success = 0 -- counts correctly placed pieces
   member("banner").text = "Assemble the puzzle"

on checkIfDone
   global success
   if success = 4 then 
      member("banner").text = "Success!"
      -- here you should play a sound and/or go to another frame
      -- to celebrate success!
   end if

Also, if you have a bunch of puzzles of the same size, you can program it so that it shows a different group of sprites every time.

File "arkas3.dir" Note that there are a couple problems with the game presentation: First, if you accidentally move a piece out of its position, the counter of successes will not decrease. So, you can "cheat" by moving a single piece in and out of position 4 times. It would be better to have a piece "stick" to its final place once it has moved there. You can do that by setting the moveableSprite property of a sprite to false.

Next, when a piece moves on top of another, you cannot see the one below. That makes it difficult to move sprites on top of a congested stage. To avoid that you can move the sprite you are currently clicking on at the top channel available to your application. You can do that by setting the locZ property of a sprite to the highest channel available. locZ works like locH and locV, but it has only a temporary effect. After the end of the event handler it is in, it will go back to its original position.

on mouseDown
  -- move sprite to top channel temporarily
  global gHighestSprite
  sprite(_mouse.clickOn).locZ = gHighestSprite + 1
  gHighestSprite = gHighestSprite + 1

The file at the header of this section implements both of these improvements.

File "arkasSwap.dir. Finally, in this version, you are moving and swapping sprites based on whether they overlap or not. This is useful when you are doing a puzzle with in-situ pieces.



Maintained By: Takis Metaxas
Last Modified: November 3, 2014