Hello forumknights! Yesterday I was catching up reading the forum when I came across a new-to-agk programmer asking about building a scramble picture game. So I thought it would be cool to build a killer puzzle game with the lot of you.
I currently have a working picture scrambler / puzzle game. Right now it's able to take pretty much any square image, divide by however many pieces you choose and randomly scramble them up! The player can move pieces and try to put it back together. No scoring or fancy movement or graphics currently.
I've laid the ground work, now it's your turn! Anyone can contribute anything! Here's a basic list of things the game will need;
- Menu Graphics / Programmed UI
- Better photos ( current ones are my personal photos, so no worries on copyright )
- Fancy puzzle piece movment
- scoring / leaderboard
- Sound Effects
- Music
- Time Trial? Various game modes.
- Game Icon
- Testers
- Code cleaners, I tried to keep the code as clean as possible lol.
- ETC!
Here's some screenshots:
Here is the current code:
// PicPuzzle Basic Gameplay Test
// Community Open Source Project
// Contributors:
// - Josh Mooney
` Setup Game Window
SetWindowTitle( "PicPuzzle" )
SetWindowSize( 1024 , 1024 , 0 )
SetDisplayAspect( 1 )
` Game Variables
GameState$ as String : GameState$ = "load" `current game state
` Puzzle Variables
PuzzleSize as Integer : PuzzleSize = 4 `number of puzzle pieces squared
Difficulty as Integer : Difficulty = 200 `how many pieces the computer moves when mixing image
PuzzleName$ as String : PuzzleName$ = "kayaks" `name of puzzle image to load
PuzzleImage as Integer : PuzzleImage = 0 `puzzle image number
PuzzleSprite as Integer : PuzzleSprite = 0 `puzzle sprite number
Scramble as Integer : Scramble = 1 `piece to move from blank piece when scrambling
OldFrame as Integer : OldFrame = 1 `old piece frame - for blank switch
BlankFrame as Integer : BlankFrame = 1 `blank piece frame - for blank switch
Type Puzzle
Sprite as Integer `puzzle piece sprite number
Solve as Integer `puzzle pieces solved frame number
Frame as Integer `frame of puzzle piece
Blank as Integer `puzzle piece blank?
EndType
Dim ImageScramble[ PuzzleSize , PuzzleSize ] as Puzzle
Frame as Integer : Frame = 1 `current frame - for assigning sprite frame
For y = 1 to PuzzleSize
For x = 1 to PuzzleSize
ImageScramble[ x , y ].Solve = Frame
ImageScramble[ x , y ].Frame = Frame
ImageScramble[ x , y ].Blank = 0
Frame = Frame + 1
Next x
Next y
LoadPuzzle( PuzzleName$ , PuzzleSize , Difficulty )
` Main Game Loop
Do
`player - puzzle interaction
PuzzlePlay( PuzzleSize )
`update puzzle pieces
UpdatePuzzle( PuzzleSize )
Sync()
Loop
` Program Functions
Function FreeImage()
For i = 1 to 100
If GetImageExists( i ) = 0
image = i
Exit
Endif
Next i
EndFunction image
Function FreeSprite()
For s = 1 to 100
If GetSpriteExists( s ) = 0
sprite = s
Exit
Endif
Next s
EndFunction sprite
` Input Functions
Function button( sprite ) `checking for button press using sprite
returnValue = 0 `reset value for check
If GetPointerX() > GetSpriteXByOffset( sprite ) - ( GetSpriteWidth( sprite ) / 2 )
If GetPointerX() < GetSpriteXByOffset( sprite ) + ( GetSpriteWidth( sprite ) / 2 )
If GetPointerY() > GetSpriteYByOffset( sprite ) - ( GetSpriteHeight( sprite ) / 2 )
If GetPointerY() < GetSpriteYByOffset( sprite ) + ( GetSpriteHeight( sprite ) / 2 )
If GetPointerPressed() = 1
returnValue = 1
Endif
Endif
Endif
Endif
Endif
EndFunction returnValue
` Puzzle Functions
Function LoadPuzzle( name$ , size , difficulty )
` Create Image Puzzle
PuzzleImage = FreeImage()
LoadImage( PuzzleImage , "images/" + name$ + ".png" )
SetImageMagFilter( PuzzleImage , 0 )
For y = 1 to size
For x = 1 to size
ImageScramble[ x , y ].Sprite = FreeSprite()
CreateSprite( ImageScramble[ x , y ].Sprite , PuzzleImage )
SetSpriteAnimation( ImageScramble[ x , y ].Sprite , GetImageWidth( PuzzleImage ) / size , GetImageHeight( PuzzleImage ) / size , ( size * size ) )
SetSpriteSize( ImageScramble[ x , y ].Sprite , 100 / size , -1 )
SetSpriteOffset( ImageScramble[ x , y ].Sprite , GetSpriteWidth( ImageScramble[ x , y ].Sprite ) / 2 , GetSpriteHeight( ImageScramble[ x , y ].Sprite ) / 2 )
SetSpritePositionByOffset( ImageScramble[ x , y ].Sprite , ( x * GetSpriteWidth( ImageScramble[ x , y ].Sprite ) ) - ( GetSpriteWidth( ImageScramble[ x , y ].Sprite ) / 2 ) , ( y * GetSpriteHeight( ImageScramble[ x , y ].Sprite ) ) - ( GetSpriteHeight( ImageScramble[ x , y ].Sprite ) / 2 ) )
SetSpriteDepth( ImageScramble[ x , y ].Sprite , 10 )
SetSpriteFrame( ImageScramble[ x , y ].Sprite , ImageScramble[ x , y ].Frame )
Next x
Next y
` Random Blank Piece
bx = random( 1 , size )
by = random( 1 , size )
ImageScramble[ bx , by ].Blank = 1
SetSpriteColorAlpha( ImageScramble[ bx , by ].Sprite , 0 )
` Scramble Image Puzzle
For d = 1 to difficulty
`check for blank piece
For sy = 1 to size
For sx = 1 to size
If ImageScramble[ sx , sy ].Blank = 1
blankx = sx
blanky = sy
Endif
Next sx
Next sy
`randomly select piece to move from blank piece
Scramble = Random( 1 , 4 )
If Scramble = 1 `scramble top
If blanky-1 > 0
OldFrame = ImageScramble[ blankx , blanky-1 ].Frame
BlankFrame = ImageScramble[ blankx , blanky ].Frame
ImageScramble[ blankx , blanky ].Frame = OldFrame
ImageScramble[ blankx , blanky ].Blank = 0
SetSpriteFrame( ImageScramble[ blankx , blanky ].Sprite , ImageScramble[ blankx , blanky ].Frame )
ImageScramble[ blankx , blanky-1 ].Frame = BlankFrame
ImageScramble[ blankx , blanky-1 ].Blank = 1
SetSpriteFrame( ImageScramble[ blankx , blanky-1 ].Sprite , ImageScramble[ blankx , blanky-1 ].Frame )
Endif
Endif
If Scramble = 2 `scramble bottom
If blanky+1 < size+1
OldFrame = ImageScramble[ blankx , blanky+1 ].Frame
BlankFrame = ImageScramble[ blankx , blanky ].Frame
ImageScramble[ blankx , blanky ].Frame = OldFrame
ImageScramble[ blankx , blanky ].Blank = 0
SetSpriteFrame( ImageScramble[ blankx , blanky ].Sprite , ImageScramble[ blankx , blanky ].Frame )
ImageScramble[ blankx , blanky+1 ].Frame = BlankFrame
ImageScramble[ blankx , blanky+1 ].Blank = 1
SetSpriteFrame( ImageScramble[ blankx , blanky+1 ].Sprite , ImageScramble[ blankx , blanky+1 ].Frame )
Endif
Endif
If Scramble = 3
If blankx-1 > 0
OldFrame = ImageScramble[ blankx-1 , blanky ].Frame
BlankFrame = ImageScramble[ blankx , blanky ].Frame
ImageScramble[ blankx , blanky ].Frame = OldFrame
ImageScramble[ blankx , blanky ].Blank = 0
SetSpriteFrame( ImageScramble[ blankx , blanky ].Sprite , ImageScramble[ blankx , blanky ].Frame )
ImageScramble[ blankx-1 , blanky ].Frame = BlankFrame
ImageScramble[ blankx-1 , blanky ].Blank = 1
SetSpriteFrame( ImageScramble[ blankx-1 , blanky ].Sprite , ImageScramble[ blankx-1 , blanky ].Frame )
Endif
Endif
If Scramble = 4
If blankx+1 < size+1
OldFrame = ImageScramble[ blankx+1 , blanky ].Frame
BlankFrame = ImageScramble[ blankx , blanky ].Frame
ImageScramble[ blankx , blanky ].Frame = OldFrame
ImageScramble[ blankx , blanky ].Blank = 0
SetSpriteFrame( ImageScramble[ blankx , blanky ].Sprite , ImageScramble[ blankx , blanky ].Frame )
ImageScramble[ blankx+1 , blanky ].Frame = BlankFrame
ImageScramble[ blankx+1 , blanky ].Blank = 1
SetSpriteFrame( ImageScramble[ blankx+1 , blanky ].Sprite , ImageScramble[ blankx+1 , blanky ].Frame )
Endif
Endif
Next d
EndFunction
Function PuzzlePlay( size )
check = 0
For ppy = 1 to size
For ppx = 1 to size
If check = 0
`check for player click on puzzle piece
If Button( ImageScramble[ ppx , ppy ].Sprite ) = 1
If ImageScramble[ ppx , ppy ].Blank = 0
`check for top blank piece
If ppy-1 > 0
If ImageScramble[ ppx , ppy-1 ].Blank = 1
OldFrame = ImageScramble[ ppx , ppy ].Frame
BlankFrame = ImageScramble[ ppx , ppy-1 ].Frame
BlankX = ppx
BlankY = ppy-1
check = 1
Endif
Endif
`check for bottom blank piece
If ppy+1 < size+1
If ImageScramble[ ppx , ppy+1 ].Blank = 1
OldFrame = ImageScramble[ ppx , ppy ].Frame
BlankFrame = ImageScramble[ ppx , ppy+1 ].Frame
BlankX = ppx
BlankY = ppy+1
check = 1
Endif
Endif
`check for left blank piece
If ppx-1 > 0
If ImageScramble[ ppx-1 , ppy ].Blank = 1
OldFrame = ImageScramble[ ppx , ppy ].Frame
BlankFrame = ImageScramble[ ppx-1 , ppy ].Frame
BlankX = ppx-1
BlankY = ppy
check = 1
Endif
Endif
`check for right blank piece
If ppx+1 < size+1
If ImageScramble[ ppx+1 , ppy ].Blank = 1
OldFrame = ImageScramble[ ppx , ppy ].Frame
BlankFrame = ImageScramble[ ppx+1 , ppy ].Frame
BlankX = ppx+1
BlankY = ppy
check = 1
Endif
Endif
If check = 1
`switch puzzle piece and blank piece
ImageScramble[ ppx , ppy ].Frame = BlankFrame
ImageScramble[ ppx , ppy ].Blank = 1
SetSpriteFrame( ImageScramble[ ppx , ppy ].Sprite , ImageScramble[ ppx , ppy ].Frame )
ImageScramble[ Blankx , Blanky ].Frame = OldFrame
ImageScramble[ Blankx , Blanky ].Blank = 0
SetSpriteFrame( ImageScramble[ Blankx , Blanky ].Sprite , ImageScramble[ Blankx , Blanky ].Frame )
Endif
Endif
Endif
Endif
Next ppx
Next ppy
EndFunction
Function UpdatePuzzle( size )
For py = 1 to size
For px = 1 to size
If ImageScramble[ px , py ].Blank = 1
SetSpriteColorAlpha( ImageScramble[ px , py ].Sprite , 0 )
Else
SetSpriteColorAlpha( ImageScramble[ px , py ].Sprite , 255 )
Endif
Next px
Next py
EndFunction
Last, Here's a download link for the Project Folder.
If you would like to contribute media or music/sound effects, than post them here in the thread! I'll add them when I can.
If you want to contribute to the codez, post your revised or updated code in the thread and I will do my best to implement or insert your update into a new master version. All contributors will receive credit for their work and it'll be heck of some fun learning experience.
Later my buds!