Hi Guys
This is a sample of code for building puzzles.
I have a coreldraw Jigsaw Creator plugin and create the puzzle pieces
I use a"ghost" puzzle to found the puzzle places.
Please give me some ideas to make this code better
// Project: puzzlebuild_template
// Created: 2018-09-23
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "puzzlebuild_template" )
SetWindowSize( 1280, 720, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1280, 720 ) // doesn't have to match the window
SetOrientationAllowed( 0,0, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//SetDisplayAspect(1280/720.0)
debug as integer = 1
//16 piece puzzle
global puzzlepieces as integer = 16
global puzzlecolumns as integer = 4
global puzzlerows as integer = 4
global puzzle as integer[2,16]
global sprite as integer[16]
global inblock as integer
global finishpuzzle as integer[16]
global yeswin as integer
//puzzle X values start
puzzle [ 1, 1 ] = 10
puzzle [ 1, 2 ] = 145
puzzle [ 1, 3 ] = 307
puzzle [ 1, 4 ] = 470
puzzle [ 1, 5 ] = 10
puzzle [ 1, 6 ] = 145
puzzle [ 1, 7 ] = 307
puzzle [ 1, 8 ] = 469
puzzle [ 1, 9 ] = 10
puzzle [ 1, 10 ] = 145
puzzle [ 1, 11 ] = 307
puzzle [ 1, 12 ] = 469
puzzle [ 1, 13 ] = 10
puzzle [ 1, 14 ] = 145
puzzle [ 1, 15 ] = 307
puzzle [ 1, 16 ] = 469
//puzzle Y values start
puzzle [ 2, 1 ] = 10
puzzle [ 2, 2 ] = 10
puzzle [ 2, 3 ] = 10
puzzle [ 2, 4 ] = 10
puzzle [ 2, 5 ] = 103
puzzle [ 2, 6 ] = 103
puzzle [ 2, 7 ] = 103
puzzle [ 2, 8 ] = 103
puzzle [ 2, 9 ] = 223
puzzle [ 2, 10 ] = 224
puzzle [ 2, 11 ] = 223
puzzle [ 2, 12 ] = 223
puzzle [ 2, 13 ] = 344
puzzle [ 2, 14 ] = 345
puzzle [ 2, 15 ] = 344
puzzle [ 2, 16 ] = 343
//load pieces, ghost skeleton and random puzzle pieces
c=1 - puzzlecolumns
d = 0
for a = 1 to puzzlerows
c = c+puzzlecolumns
d = d+puzzlecolumns
for b = c to d
sprite[b] =b
loadimage (sprite[b] , str(b)+".png")
createsprite(sprite[b],sprite[b])
SetSpritePosition(sprite[b],random(600,1200),random(0,600))
sprite[b] =b*100
loadimage (sprite[b] , str(b)+"a.png")
createsprite(sprite[b],sprite[b])
SetSpritePosition(sprite[b],puzzle[1,b],puzzle[2,b])
next b
next a
//buttons for preview/random shuffle
AddVirtualButton(1,120,600,100)
SetVirtualButtonText(1,'Shuffle')
AddVirtualButton(2,240,600,100)
SetVirtualButtonText(2,'Preview')
do
if GetVirtualButtonPressed(1) = 1
randompuzzle()
endif
if GetVirtualButtonPressed(2) = 1
previewpuzzle()
endif
if yeswin = 0
mousex= GetPointerX()
mousey= GetPointerY()
if mousedown = 0
spritehit=GetSpriteHit( mousex, mousey )
endif
if spritehit <> 0 and spritehit < 100
if lastsprite <> spritehit
lastsprite = spritehit
endif
endif
if GetRawMouseLeftState() = 1 and lastsprite <> 0
mousedown = 1
SetSpriteDepth ( lastsprite, 0 ) // bring selected piece to the front
SetSpritePosition(lastsprite,GetPointerX()-(GetSpriteWidth(lastsprite)/2), GetPointerY()-(GetSpriteHeight(lastsprite)/2)) //set puzzle piece center of mouse cursor
if GetSpriteExists(lastsprite * 100) = 1 // check if ghost piece is there
SetSpritePosition(sprite[lastsprite],puzzle[1,lastsprite],puzzle[2,lastsprite]) // if not bring back
endif
else
mousedown=0
if spritehit <> 0
spritehit2=GetSpriteHit( mousex, mousey)
ghost_coord(lastsprite) // check x1,x2,y1,y2 coordinates (Size of piece)
if inblock=1
if GetSpriteExists(lastsprite)
setspriteposition(lastsprite,puzzle[1,lastsprite],puzzle[2,lastsprite])
finishpuzzle[lastsprite] = 1 // piece inplace counter
checkwin()
if yeswin = 1
message('you win!!!!')
endif
endif
if GetSpriteExists(spritehit2*100)
SetSpritePosition(spritehit2*100,-1000,-1000) // puzzle match move ghost puzzle away
endif
lastsprite=0
inblock=0
spritehit2=0
endif
endif
endif
endif
Sync()
loop
function ghost_coord(n) //get sqaure border from piece
if n > 99
n = n/100
endif
if n <> 0
x1 = puzzle[1,n]
x2 = getspritewidth(n*100)+puzzle[1,n]
y1 = puzzle[2,n]
y2 = getspriteheight(n*100)+puzzle[2,n]
if getpointerx() > x1 and getpointerx() < x2 and getpointery() > y1 and getpointery() < y2
inblock=1
endif
endif
endfunction
function randompuzzle() // shuffle puzzle
yeswin = 0
for i = 1 to puzzlepieces
finishpuzzle[i] = 0
next i
c=1 - puzzlecolumns
d = 0
for a = 1 to puzzlerows
c = c+puzzlecolumns
d = d+puzzlecolumns
for b = c to d
sprite[b] =b
SetSpritePosition(sprite[b],random(650,1150),random(2,620))
sprite[b] =b * 100
SetSpritePosition(sprite[b],puzzle[1,b],puzzle[2,b])
next b
next a
endfunction
function previewpuzzle()
c=1 - puzzlecolumns
d = 0
for a = 1 to puzzlerows
c = c+puzzlecolumns
d = d+puzzlecolumns
for b = c to d
sprite[b] =b
SetSpritePosition(sprite[b],puzzle[1,b],puzzle[2,b])
sprite[b] =b * 100
SetSpritePosition(sprite[b],puzzle[1,b],puzzle[2,b])
next b
next a
endfunction
function checkwin()
no = 0
print(no)
for i = 1 to puzzlepieces
if finishpuzzle[i]= 0
no = 1
endif
next i
if no = 0
yeswin = 1
endif
endfunction