As penance for not spotting my newbie style syntax error I'll post the entire code of what I was trying to achieve. I've not coded anything in 4 years so pretty my all my base are yours atm. Still, it works and it stands alone needing only a random image to be supplied.
Naturally it could (and probably should) be prettied up with button graphics, some better event handling and a more sophisticated interface to choose images and puzzle dimensioins (rows colums)
Global ROWS =4
Global COLS =4
SeedRnd MilliSecs()
Dim board(ROWS,COLS)
;Include "puzzlefuncs.bb"
Graphics 800,600,32,3
mainImage=LoadImage("default.jpg")
If Not (mainimage) Then RuntimeError("No image to load, default.jpg needed in the same directory as this executable")
ResizeImage mainImage,640,480
cellx=640/COLS
celly=480/ROWS
ox=(800-640)/2
oy=(600-480)/2
boardImage = CreateImage (cellx,celly,ROWS*COLS)
;set the current buffer to the image I want to grab frames from
SetBuffer ImageBuffer(mainImage)
For row=0 To ROWS-1
For col= 0 To COLS-1
idx=COLS*row + col
;grab a portion of the mainImage into a frame of boardImage
GrabImage (boardImage,col*cellx,row*celly,idx)
;set up the game board matrix while we're at it
board(row,col)=idx
Next
Next
SetBuffer FrontBuffer()
drawbuttons(ox,oy,cellx,celly)
drawboard(ox,oy,boardImage,cellx,celly)
While Not KeyHit(1)
WaitMouse()
mx=MouseX()
my=MouseY()
If my<oy And my>(oy-30) Then
mx=mx-ox
If mx>0 And mx< COLS*(cellx+1) Then
column = Floor(mx/cellx)
columnShiftUp(column)
drawboard(ox,oy,boardImage,cellx,celly)
End If
End If
If (my>oy+(ROWS*(celly+1))) And my<(oy+(rows*(celly+1)+20)) Then
mx=mx-ox
If mx>0 And mx< COLS*(cellx+1) Then
column = Floor(mx/cellx)
columnShiftDown(column)
drawboard(ox,oy,boardImage,cellx,celly)
End If
End If
If mx<ox And mx>(ox-30) Then
my=my-oy
If my>0 And my < ROWS*(celly+1) Then
row=Floor (my/celly)
rowShiftLeft(row)
drawboard(ox,oy,boardImage,cellx,celly)
End If
End If
If (mx>ox+(ROWS*(cellx+1))) And (mx<(ox+(ROWS*(cellx+1))+20)) Then
my=my-oy
If my>0 And my < ROWS*(celly+1) Then
row=Floor (my/celly)
rowShiftRight(row)
drawboard(ox,oy,boardImage,cellx,celly)
End If
End If
If my<30 And my>10 Then
If mx>170 And mx< 230 Then
mixIt(ox,oy,boardImage,cellx,celly)
End If
If mx>570 And mx<630 Then
End
End If
If mx>370 And mx<430 Then
fixIt()
drawboard(ox,oy,boardImage,cellx,celly)
End If
End If
Wend
Function drawbuttons(ox,oy,cellx,celly)
Color 150,100,100
;draw buttons along the top for each column of cells
For col=0 To COLS-1
Rect ox+2+(cellx+1)*col,oy-20,cellx-4,10,1
Rect ox+2+(cellx+1)*col,rows*(celly+1)+oy+10,cellx-4,10,1
Next
For row=0 To rows-1
Rect ox-20,oy+2+(celly+1)*row,10,celly-4,1
Rect ox+10+(cellx+1)*cols,oy+2+(celly+1)*row,10,celly-4,1
Next
;Draw mix it, fix it and quit it
Rect 170,10,60,16,1
Rect 370,10,60,16,1
Rect 570,10,60,16,1
Color 100,50,50
Text 200,18,"Mix It",1,1
Text 400,18,"Fix It",1,1
Text 600,18,"Quit It",1,1
End Function
Function drawboard(ox,oy,boardImage,cellx,celly)
For row=0 To ROWS-1
For col= 0 To COLS-1
idx=board(row,col)
DrawImage boardImage,col*(cellx+1)+ox,row*(celly+1)+oy,idx
Next
Next
End Function
Function drawboardimage(ox,oy,bI,cellx,celly)
For row=0 To ROWS-1
For col= 0 To COLS-1
idx=4*row+col
DrawImage bI,col*(cellx+1)+ox,row*(celly+1)+oy,idx
Next
Next
End Function
Function mixIt(ox,oy,boardImage,cellx,celly)
For f = 1 To 100
rc=Rnd(1,2)
If rc=1 Then
c=Rnd(0,cols-1)
ud=Rnd(1,2)
If ud=1 Then
columnShiftUp(c)
Else
columnShiftDown(c)
End If
Else If rc= 2 Then
r=Rnd(0,rows-1)
lr=Rnd(1,2)
If lr=1 Then
rowShiftRight(r)
Else
rowShiftleft(r)
End If
End If
drawboard(ox,oy,boardImage,cellx,celly)
Delay 50
Next
End Function
Function fixIt()
For row=0 To ROWS-1
For col=0 To COLS-1
board(row,col)=COLS*row + col
Next
Next
End Function
Function RowShiftRight(row)
board(row,COLS)=board(row,COLS-1)
For f=(COLS-1) To 1 Step -1
board(row,f)=board(row,f-1)
Next
board(row,0)=board(row,COLS)
board(row,COLS)=0
End Function
Function RowShiftLeft(row)
board(row,COLS)=board(row,0)
For f=0To COLS-2
board(row,f)=board(row,f+1)
Next
board(row,COLS-1)=board(row,COLS)
board(row,COLS)=0
End Function
Function ColumnShiftUp(col)
board(ROWS,col)=board(0,col)
For f=0 To ROWS-2
board(f,col)=board(f+1,col)
Next
board(ROWS-1,col)=board(ROWS,col)
board(ROWS,col)=0
End Function
Function ColumnShiftDown(col)
board(ROWS,col)=board(ROWS-1,col)
For f=(ROWS-1) To 1 Step -1
board(f,col)=board(f-1,col)
Next
board(0,col)=board(ROWS,col)
board(ROWS,col)=0
End Function
|