Sorry but your code is a mess - I've tidied it up a bit.
p.s. It's very inefficient to check collisions against all the blocks. Really, you only need to check those around you but I'll leave that for you to figure out.
Graphics 1280,1024,0,1
SetBuffer BackBuffer()
Type block
Field x#,y#
Field img
End Type
Type player
Field x#,y#
Field xv#,yv#
Field frame
Field img
End Type
Global player.player,block.block
bimg = CreateImage(128,128)
SetBuffer ImageBuffer(bimg)
Rect 0,0,127,127
SetBuffer BackBuffer()
CreatePlayer(0,100,1,1)
CreateBlock(100,100,bimg)
While Not KeyHit(1)
Cls
DrawBlocks()
UpdatePlayer()
Flip
Wend
;======================================================================================
;======================================================================================
;======================================================================================
Function CreatePlayer(x#,y#,xv#,yv#)
p.player = New player
p\img = CreateImage(64,64)
SetBuffer ImageBuffer(p\img)
Rect 0,0,63,63
SetBuffer BackBuffer()
p\x# = x#
p\y# = y#
p\xv# = xv#
p\yv# = yv#
p\frame = 0
End Function
;======================================================================================
;======================================================================================
;======================================================================================
Function UpdatePlayer()
For p.player = Each player
Mx# = ( KeyDown(205) - KeyDown(203) ) * p\xv
My# = ( KeyDown(208) - KeyDown(200) ) * p\yv
If Mx <> 0 Or My <> 0
For b.block = Each block
If ImagesOverlap( p\img, p\x + Mx, p\y, b\Img, b\x, b\y ) Mx = 0
If ImagesOverlap( p\img, p\x, p\y + My, b\Img, b\x, b\y ) My = 0
Next
p\x = LIMIT( p\x + Mx , 0 , GraphicsWidth()-ImageWidth( p\img ) )
p\y = LIMIT( p\y + My , 0 , GraphicsHeight() - ImageHeight( p\img ) )
EndIf
DrawImage p\img,p\x,p\y,p\frame
Next
End Function
;======================================================================================
;======================================================================================
;======================================================================================
Function LIMIT#( v#, low#, high# )
If v < low v = low
If v > high v = high
Return v
End Function
;======================================================================================
;======================================================================================
;======================================================================================
Function CreateBlock(x#,y#,img)
b.block = New block
b\x# = x#
b\y# = y#
b\img = img
End Function
;======================================================================================
;======================================================================================
;======================================================================================
Function DrawBlocks()
For b.block = Each block
DrawImage b\img, b\x, b\y
Next
End Function
|