explain sliding game lingo

This code is for a picture sliding game where one block is used to move the images. There are 16 individual images including the empty block. Can any one explain the code below?


property spriteNum, piece

global pieceList, pickList

on beginSprite me

  piece = sprite(spriteNum).member.name
  if pieceList = void then
    pieceList = []
  end if
  if pieceList.getOne(piece) <> 0 then

    pieceList = []
  end if  
  pieceList.add(piece)
end

on shuffle me
  if pickList = void then
    pickList = duplicate(pieceList)
  else if pickList.count = 0 then
    pickList = duplicate(pieceList)
  end if
  thisOne = random(pickList.count)
  sprite(spriteNum).member = member(pickList[thisOne])
  pickList.deleteAt(thisOne)  
end

on swap me, whatRect, whatSprite

  sort(pieceList)  
  if sprite(spriteNum).member = member(pieceList[pieceList.count]) and spriteNum <> whatSprite then

    overlap = intersect(sprite(spriteNum).rect, whatRect)
    if (overLap.width > 0 or overlap.height > 0) then

      if overLap.width > 1 or overlap.height > 1 then

        whatMember = sprite(spriteNum).member
        sprite(spriteNum).member = sprite(whatSprite).member
        sprite(whatSprite).member = whatMember
      end if
    end if
  end if
end

on checkWin me, winList
  if sprite(spriteNum).member.name = piece then
    winList.add(1)
  else
    winList.add(0)
  end if
end

on mouseUp me
  sendAllSprites(#swap, (sprite(spriteNum).rect + rect(-1,-1,1,1), spriteNum)
  winList = []
  sendAllSprites(#checkWin, winList)
  if winList.getOne(0) = 0 then

    go "win"
  end if
end

on getBehaviorDescription me
  describe = "This behavior can be dropped on a grid of images to create a sliding puzzle game."
  describe = describe & return & "Be sure to name your pieces conncurrently and name the " & quote & "empty" & quote & " piece so that it will fall at the end of the list when sorted alphabetically."
  describe = describe & return & 
  return describe
end
lukegriffiths110Asked:
Who is Participating?
 
LingoMasterCommented:
It sure wold help if you were a bit more specific about what you understand and what you don't, do you have a working copy of the game? or do you need basic member score configuration tips for getting the code to work right?

A couple of observations

The behavior is dropped on all 16 tile sprites

property spriteNum, piece
-- are private variables different for each tile, sprieNum is a property of the scrip instance me passed to the handlers, a more common way to use it would be.

put "my sprite is " & sprite(me.spriteNum)


global pieceList, pickList
 -- are variables shared by all tiles, the lowest sprite number is the first script to run, it detects if the lists are not defined and creates the initial empty one as needed with:

if pieceList = void then
    pieceList = []
  end if

Each tile sprite, in turn adds it's member name to the  pieceList global list variable.  There mus be 16 unique member names assigned to the tile bitmaps, I suggest:  Tile1, Tile2 ...tile15, zEmptyOne The name "zEmptyOne" starts with z to insure the empty tile (a part of the background cut out in a tile shape) name ends at the end of a sorted list.

it appears that a separate Shuffle button has to call the Shuffle handler in each of the  tiles in order to get the game reset.

-- this reset button script should do
on mouseUp
  sendAllSprites #Shuffle
end

sendAllSprites is the function used by the clicked tile to  pass some information about it'self while communicating to all the others,

Clicking a tile makes it send swap to all the tiles, passing a slightly expanded (by one pixel) rectangle to all the other sprites, only the sprite currently assigned the member takes action, it determines by the expanded rects pased to it, which tile is touching it, the empty tile sprit swaps image members with the clicked tile which is the one touching it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.