Does anyone have any tips for coding platformers?
Graphics 800,600
Global dude = CreateImage(20,20)
SetBuffer ImageBuffer(dude)
Color 255,0,0
Oval 0,0,20,20,1
Global dline = CreateImage(20,3)
SetBuffer ImageBuffer(dline)
Color 155,50,0
Rect 0,0,100,3,1
Global pltf = CreateImage(200,30)
SetBuffer ImageBuffer(pltf)
Color 155,250,0
Rect 0,0,200,30,1
Global pltfl = CreateImage(200,3)
SetBuffer ImageBuffer(pltfl)
Color 255,255,255
Rect 0,0,200,3,1
SetBuffer BackBuffer()
Global jumpheight# = 10
Global canjump = 0
Global jump = 0
Const gravity# = 0.4
Type player
Field x#
Field y#
Field h#
End Type
Type platform
Field x
Field y
Field g
End Type
createplatform()
createplayer()
While Not KeyHit ( 1 )
Cls
Text 50,50,"Jumpheight = " +Int(jumpheight)
drawlines()
renderplatform()
renderplayer()
moveplayer()
playerjump()
collisionchecks()
Flip
Wend
End
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Draw Lines ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function drawlines()
For p.player = Each player
DrawImage dline,p\x + 3,p\y + 50
Next
For plat.platform = Each platform
DrawImage pltfl,plat\x,plat\y - 1
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Create Platform ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function createplatform()
plat.platform = New platform
plat\x = 400
plat\y = 535
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Create Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function createplayer()
p.player = New player
p\x = 300
p\y = 540
p\h = 10
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Render Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function renderplayer()
For p.player = Each player
DrawImage dude,p\x,p\y,0
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function :Render Platform ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function renderplatform()
For plat.platform = Each platform
DrawImage pltf,plat\x,plat\y
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Move Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function moveplayer()
If KeyDown ( 203 )
For p.player = Each player
p\x = p\x - 3
Next
ElseIf KeyDown ( 205 )
For p.player = Each player
p\x = p\x + 3
Next
EndIf
For p.player = Each player
If p\y > 540 Then
p\y = 540
canjump = 0
jump = 0
jumpheight = 10
EndIf
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Player Jump ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function playerjump()
If KeyHit ( 57 ) And (jumpheight = 10 Or Int(jumpheight) = -1);(Int(jumpheight = -1)))
canjump = 1
jump = 1
jumpheight = 10
EndIf
If canjump = 1 And jump = 1 Then
If jumpheight <=0 Then
For p.player = Each player
For i = 0 To 1 + Abs(jumpheight)
If collisionchecks() = True Then Exit
p\y = p\y + 1
Next
Next
Else
For p.player = Each player
For i = 0 To jumpheight
If collisionchecks = True Then Exit
p\y = p\y - 1
Next
Next
EndIf
jumpheight = jumpheight - gravity
Else
jump = 0
canjump = 0
jumpheight = 10
EndIf
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Collision checks ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function collisionchecks()
For p.player = Each player
For plat.platform = Each platform
If ImagesCollide(dline,p\x + 3,p\y + 50,0,pltfl,plat\x,plat\y - 1,0) And jumpheight <0 Then
canjump = 1
jump = 0
Return True
ElseIf p\y <540 And jumpheight = 10 Then
canjump = 1
jump = 1
jumpheight = -1
Return True
EndIf
Next
Next
Return False
End Function
CREDIT to ORIGINAL POSTER
dunno who they were, but this snippet was posted in these forums a while back...
hope it helps...