|
|
|
Puzzler II - Slide Puzzle
Added on 10/25/1999
|
This behavior can be dropped on a grid of images to create a sliding puzzle game.
Be sure to name your pieces conncurrently and name the "empty" piece so that it will fall at the end of the list when sorted alphabetically.
A working example with full source code can be found in the learning arcade at www.mediamacros.com
--Copyright 1999 Chuck Neal
--chuck@mediamacros.com
--If you find this code helpful, send me an e-mail and let me know. :-)
property spriteNum, piece
global pieceList, pickList
on beginSprite me
--name the pieces by number
piece = sprite(spriteNum).member.name
if pieceList = void then
pieceList = []
end if
if pieceList.getOne(piece) <> 0 then
--old list, reinitialize
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
--check for lastOne
sort(pieceList)
if sprite(spriteNum).member = member(pieceList[pieceList.count]) and spriteNum <> whatSprite then
--is the "last one" (blank space)
overlap = intersect(sprite(spriteNum).rect, whatRect)
if (overLap.width > 0 or overlap.height > 0) then
--do they overLap?
if overLap.width > 1 or overlap.height > 1 then
--make sure they don;t overlap diagonally
--if they intersect, swap
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
--win
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 & "A working example with full source code can be found in the learning arcade at www.mediamacros.com"
return describe
end
|
|