Main Menu

Jigsaw Puzzler

Started by stef, October 28, 2006, 01:21:58 PM

Previous topic - Next topic

stef

Hi

This is a simple Jigsaw-puzzle-generator
(used in my 'Halloween'-Production 'Ghostwriters'

It is for loaded images or created animations
(It's both in this program; that's just for demostration)

You can change number of tiles,image- and tilesize

Click on a tile - click on an other tile to exchange
use function-keys F1-F4

made with PB 1.54

PlayBASIC Code: [Select]
; PROJECT : Jigsaw-Puzzler
; AUTHOR : stef
; CREATED : 26.10.2006
; EDITED : 28.10.2006

; ---------------------------------------------------------------------

; click one tile , click another tile to exchange with first one
; F1 toggles info
; F2 toggles red grid
; F3 shows tiles in correct position
; F4 change images (just to demostrate you can use a loaded image or an animation)

;---------------------------------------------------------------------
;to change manually

Global screenw=800
Global screenh=600

; imagesize/blocksize should be without remainder!
Global imagesizex=500;width of displayed image
Global imagesizey=400;height of displayed image

Global tilesizex=100;width of tile
Global tilesizey=100;height of tile

Global tilesx=imagesizex/tilesizex; number of tiles x-direction
Global tilesy=imagesizey/tilesizey; number of tiles y-direction
Global tiles=tilesx*tilesy;total number of tiles

Global offsetx=(screenw-imagesizex)/2;display image/tiles centered on screen
Global offsety=(screenh-imagesizey)/2; if offsetx=0 : left screenborder
;--------------------------------------------------------------------

OpenScreen screenw,screenh,16,2
Global fpsset=60
SetFPS fpsset

; change ---------------------------------------------------
animal_im=GetFreeImage()

If FileExist("animal.jpg")=1
LoadImage "animal.jpg",animal_im
Else
LoadImage "..\..\projects\gfx\animal.jpg",animal_im ; maybe more or less "..\"
EndIf

ScaleImage animal_im,imagesizex,imagesizey,1
;------------------------------------------------------------



fs=50;framebreadth
col=RGB(100,100,100)
colorframe_im=NewImage(screenw,screenh)
RenderToImage colorframe_im
;boxc 0,0,screenw,screenh,1,rgb(0,0,100)
BoxC offsetx-fs,offsety-fs,offsetx+imagesizex+fs,offsety+imagesizey+fs,1,col
BoxC offsetx,offsety,offsetx+imagesizex,offsety+imagesizey,1,RGB(0,0,0)

Global display=1
Global bluebox=0
Global grid=-1
Global choice=1
Global info=1
Global tim

Global xmouse
Global ymouse


Global display_im,initimage

Type tparts
image
xnumber
ynumber
xactualpos
yactualpos
xcorrectpos
ycorrectpos
xfalsepos
yfalsepos

correctposexist
falseposexist
EndType

;creating and defining of tiles
Dim parts(tiles) As tparts

For a = 1 To tiles
parts(a).image=NewImage(tilesizex,tilesizey)
parts(a).xnumber=xval
parts(a).ynumber=yval
parts(a).xcorrectpos=parts(a).xnumber*tilesizex
parts(a).ycorrectpos=parts(a).ynumber*tilesizey

xval=xval+1
If xval=tilesx
xval=0
yval=yval+1
EndIf
If yval=tilesy Then yval=0
Next

;setting tiles on randompositions
For a = 1 To tiles
Repeat
anew = RndRange(1,tiles)
If parts(a).correctposexist=0 And parts(anew).falseposexist=0
parts(anew).falseposexist=1
parts(a).correctposexist=1
parts(a).xfalsepos=parts(anew).xcorrectpos
parts(a).yfalsepos=parts(anew).ycorrectpos
Inc newexistcounter
If newexistcounter=tiles Then ExitRepeat
EndIf
Until parts(anew).falseposexist=0
Next

Global display_im=NewImage(imagesizex,imagesizey)

Do

;switches images-------------------------------------------------
If choice=1
generateimage();image to display as puzzle
Else
RenderToImage animal_im;loaded image
EndIf
;----------------------------------------------------------

;grabbing of tiles-----------------------------------------
For a= 1 To tiles
GetImage parts(a).image,parts(a).xcorrectpos,parts(a).ycorrectpos,parts(a).xcorrectpos+tilesizex,parts(a).ycorrectpos+tilesizey
Next
;---------------------------------------------------------

RenderToScreen
Cls 0

DrawImage colorframe_im,0,0,1



For a= 1 To tiles
If bluebox=1
Login required to view complete source code