cutting irregular pieces from image for puzzle game

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: cutting irregular pieces from image for puzzle game

Post by bn » Fri Jul 11, 2025 9:49 am

I think it is due to the fact that the folder does not exist. It is not automatically created.

Here is what worked for me on a Mac.

Code: Select all

on mouseUp
   put the number of images of this card into nImages
   put specialFolderPath("documents") & "/app data/testing/tempImages" into tTempPath
   
   
   set the itemDelimiter to slash
   put itemOffset("Documents",tTempPath) into tOffset
   
   put item 1 to tOffset of tTempPath into tRootPath
   
   repeat with i = tOffset +1 to the number of items of tTempPath
      if there is not a folder (item 1 to i of tTempPath) then
         create folder (item 1 to i of tTempPath)
         put the result into tResult
         if tResult is not empty then breakpoint
      end if
   end repeat
   
   set the itemDelimiter to comma
   
   repeat with i = 1 to 1  -- nImages ## changed for testing
      put the short name of image i into tImageFileName
      put tTempPath & "/" & the short name of image i & ".png" into tFilePath
      export image tImageFileName to file tFilePath as PNG
      put the result into tResult
   end repeat
end mouseUp
It first checks for the existence of a folder, if it not exists then it creates the folder(s) and only then exports the images to that folder.

Checking for the result after creating a folder makes sure that it worked. If successful the result is empty.

Kind regards
Bernd

rcmills
Posts: 68
Joined: Wed Nov 21, 2018 8:27 pm

Re: cutting irregular pieces from image for puzzle game

Post by rcmills » Fri Jul 11, 2025 10:05 pm

Thanks again! Even though I "thought" I had created the proper folders, going through the process of checking on them somehow fixed the problem!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: cutting irregular pieces from image for puzzle game

Post by bn » Wed Jul 16, 2025 8:55 pm

I made a script that arranges the Puzzle Pieces in correct order and position.
Consider it a "cheat" script or a final arrangement script since manual placement of puzzle pieces is difficult.

Code: Select all

on mouseUp
   local tMaxColNum, tMaxRowNum, tNumPieces
   local tCollect, tShortImgName, tExpectedNumPieces, tStartTL, tStartLeft, tStartTop
   local tPieceSize = 60, tPuzzleWidth, tPuzzleHeight
   local tImgColumn, tImgRow, tFudge, tTempLeft, tTempTop
   
   repeat with i = 1 to the number of images
      put the short name of image i into tShortImgName
      if tShortImgName begins with "PP" then
         put tShortImgName & return after tCollect
         put max(char 3 to -1 of item 1 of tShortImgName, tMaxColNum) into tMaxColNum
         put max(char 1 to -1 of item 2 of tShortImgName, tMaxRowNum) into tMaxRowNum
      end if
   end repeat
   delete char -1 of tCollect
   
   if tCollect is empty then 
      answer "No Puzzle Pieces found"
      exit mouseUp
   end if
   
   put tMaxColNum * tMaxRowNum into tExpectedNumPieces
   
   ## check for number of Puzzle Pieces and bail out if there are too few or too many
   if the number of lines of tCollect <> tExpectedNumPieces then
      answer "Not all or too many Puzzle Pieces found"
      exit mouseUp
   end if
   
   ## format of name is "PP" & column & comma & row
   ## this sorts the images by column and then by row
   ## PP1,1; PP2,1 etc
   ## PP1,2; PP2,2 etc
   sort tCollect numeric by char 3 to -1 of item 1 of each
   sort tCollect numeric by char 1 to -1 of item 2 of each
   
   put the topLeft of image line 1 of tCollect into tStartTL
   put item 1 of tStartTL into tStartLeft
   put item 2 of tStartTL into tStartTop
   
   ## check for space to arrange Puzzle Pieces; otherwise they could be placed beyond the card boundaries
   if tStartLeft + (tMaxColNum * tPieceSize) > the width of this card or \
         tStartTop + (tMaxRowNum * tPieceSize) > the height of this card then
      answer "Place top-left Puzzle Piece near the top left of the card"
      exit mouseUp
   end if
   
   repeat for each line anImage in tCollect
      put char 3 to -1 of item 1 of anImage into tImgColumn
      put char 1 to -1 of item 2 of anImage into tImgRow
      put the uMarginFudge of image anImage into tFudge
      put tStartLeft + ((tImgColumn - 1) * tPieceSize) into tTempLeft
      put tStartTop + ((tImgRow -1) * tPieceSize) into tTempTop
      put tTempLeft - item 1 of tFudge into tTempLeft
      put tTempTop - item 2 of tFudge into tTempTop
      set the topLeft of image anImage to tTempLeft, tTempTop
   end repeat
   
end mouseUp
use this script in a button on a card that contains all the pieces of one puzzle. Not more not less.

Kind regards
Bernd

PS if you are still using the outerglow for the script of the puzzle pieces when they are move by the mouse then I changed "outerglow" to "innerglow" because the outerglow overlaps the neighboring pieces whereas "innerglow" does not but still marks the moving piece.

Post Reply