Help with jump platform code please!!

Blitz3D Forums/Blitz3D Beginners Area/Help with jump platform code please!!

Spec(Posted 2004) [#1]
; My first Steps into Solo programming
; using Blitz 3D !!
;
; By Paul Harthen (aka Spectre)
; 14 - 3 - 2004 (C)

Const screenx=800 
Const screeny=600

Graphics screenx,screeny,16,2

SetBuffer BackBuffer()
player=LoadImage("alien.bmp")

Type aliens
	Field char
	Field x
	Field y
	Field speed
	Field jump_height
	Field jump_width
	Field jump_angle
End Type

alien.aliens=New aliens
alien\char=player
alien\x=screenx/2
alien\y=screeny/2
alien\speed=2
alien\jump_height=100
alien\jump_width=90
alien\jump_angle=0

char=alien\char
charx=alien\x
chary=alien\y
charspeed=alien\speed
height=alien\jump_height
width=alien\jump_width
charangle=alien\jump_angle


direction=0

While Not KeyHit(1)
	Cls
	
	If KeyDown(203)  
		charx=charx-charspeed
		direction=-1
	EndIf
	
	If KeyDown(205)  
		charx=charx+charspeed
		direction=1
	EndIf
	
	If KeyDown(57)
		Gosub jump
	EndIf
	
	If charx<0 Then charx=0
	If charx>screenx-ImageWidth(char) Then charx=screenx-ImageWidth(char)
	
	DrawImage char,charx,chary
	Flip
Wend

For alien.aliens=Each aliens
	Delete alien
Next

End

.Jump
If charangle < 181

   If direction=-1
      x=Cos(charangle)*width/2
      charx=xoffset+x-width/2
   EndIf
   If direction=1
      x=-Cos(charangle)*width/2
      charx=xoffset+x+width/2
   EndIf

   y=Sin(charangle)*height/2

   chary=yoffset+y
   charangle=charangle+charspeed

Else

   chary=yoffset
   charangle=0

EndIf
Return


Can anyone help with this code I am working on please!!???

What I am trying to do is get this sprite to move left and right and jump when you press the space bar!!

I have really messed up somewhere!!

Also if you could explain or change the jump routine to a function, that would be great as I couldn't do it!! :)

Another thing....... Am I doing the Type thing correct as it is hard to understand!!! :)))

Thanks if you can help.


electronin(Posted 2004) [#2]
Hey Spec, I'll look at your code. Also, you might want to check this out.


Spec(Posted 2004) [#3]
Thanks a lot m8 :) Goin to check it now!!


electronin(Posted 2004) [#4]
Inside the Jump label, there's a variable called xoffset that isn't declared anywhere else. That could be your problem.


Spec(Posted 2004) [#5]
had another bash at it, see code below, but still not great looks a bit false and if you keep hitting space quick whilst moving it goes higher up the screen!!! :) lol

 ; My first Steps into Solo programming
; using Blitz 3D !!
;
; By Paul Harthen (aka Spectre)
; 14 - 3 - 2004 (C)

Const screenx=800 
Const screeny=600

Graphics screenx,screeny,16,2

SetBuffer BackBuffer()
player=LoadImage("alien.bmp")

Type aliens
	Field char
	Field x
	Field y
	Field speed
	Field jump_height
	Field jump_width
	Field jump_angle
	Field jump_speed
End Type

alien.aliens=New aliens
alien\char=player
alien\x=screenx/2
alien\y=screeny/2
alien\speed=2
alien\jump_height=100
alien\jump_width=90
alien\jump_angle=0
alien\jump_speed=4

char=alien\char
charx=alien\x
chary=alien\y
charspeed=alien\speed
height=alien\jump_height
width=alien\jump_width
charangle=alien\jump_angle
jumpspeed=alien\jump_speed

direction=0 : jumping=0

While Not KeyHit(1)
	Cls
	
	If KeyDown(203)  
		charx=charx-charspeed
		direction=-1
	EndIf
	
	If KeyDown(205)  
		charx=charx+charspeed
		direction=1
	EndIf
	
	If KeyHit(57)
		xoffset=charx
		yoffset=chary
		jumping=1
	EndIf
	
	If jumping=1 Then Gosub jump
	
	If charx<0 Then charx=0
	If charx>screenx-ImageWidth(char) Then charx=screenx-ImageWidth(char)
	
	DrawImage char,charx,chary
	Flip
Wend

For alien.aliens=Each aliens
	Delete alien
Next

End

.Jump
If charangle < 181

   If direction=-1
      x=Cos(charangle)*width/2
      charx=xoffset+x-width/2
	  direction=0
   EndIf
   If direction=1
      x=-Cos(charangle)*width/2
      charx=xoffset+x+width/2
	  direction=0
   EndIf

   y=-Sin(charangle)*height/2

   chary=yoffset+y
   charangle=charangle+charspeed*jumpspeed

Else	

   jumping=0
   chary=yoffset
   charangle=0

EndIf
Return



electronin(Posted 2004) [#6]
Maybe make a variable called "AlreadyJumping" or something, and make it so that if the player is already jumping, the player can't jump again. Try this:

 ; My first Steps into Solo programming
; using Blitz 3D !!
;
; By Paul Harthen (aka Spectre)
; 14 - 3 - 2004 (C)

Const screenx=800 
Const screeny=600

Graphics screenx,screeny,16,2

SetBuffer BackBuffer()
player=LoadImage("alien.bmp")

Type aliens
	Field char
	Field x
	Field y
	Field speed
	Field jump_height
	Field jump_width
	Field jump_angle
	Field jump_speed
End Type

alien.aliens=New aliens
alien\char=player
alien\x=screenx/2
alien\y=screeny/2
alien\speed=2
alien\jump_height=100
alien\jump_width=90
alien\jump_angle=0
alien\jump_speed=4

char=alien\char
charx=alien\x
chary=alien\y
charspeed=alien\speed
height=alien\jump_height
width=alien\jump_width
charangle=alien\jump_angle
jumpspeed=alien\jump_speed

direction=0 : jumping=0

While Not KeyHit(1)
	Cls
	
	If KeyDown(203)  
		charx=charx-charspeed
		direction=-1
	EndIf
	
	If KeyDown(205)  
		charx=charx+charspeed
		direction=1
	EndIf
	
	If KeyHit(57) And alreadyjumping=0
		xoffset=charx
		yoffset=chary
		jumping=1
		alreadyjumping=1
	EndIf
	
	If jumping=1 Then Gosub jump
	
	If charx<0 Then charx=0
	If charx>screenx-ImageWidth(char) Then charx=screenx-ImageWidth(char)
	
	DrawImage char,charx,chary
	Flip
Wend

For alien.aliens=Each aliens
	Delete alien
Next

End

.Jump
If charangle < 181

   If direction=-1
      x=Cos(charangle)*width/2
      charx=xoffset+x-width/2
	  direction=0
   EndIf
   If direction=1
      x=-Cos(charangle)*width/2
      charx=xoffset+x+width/2
	  direction=0
   EndIf

   y=-Sin(charangle)*height/2

   chary=yoffset+y
   charangle=charangle+charspeed*jumpspeed

Else	

   jumping=0
   chary=yoffset
   charangle=0
   alreadyjumping=0

EndIf
Return




LarsG(Posted 2004) [#7]
why use another value "alreadyjumping"? you can just use "jumping":
If KeyHit(57) And jumping=0



Spec(Posted 2004) [#8]
Oooops didn't spot that, that was when I was trying to make it into a function and forgot to delete it!! :))

Heres the new code:

; My first Steps into Solo programming
; using Blitz 3D !!
;
; By Paul Harthen (aka Spectre)
; 14 - 3 - 2004 (C)

Const screenx=800 
Const screeny=600

Graphics screenx,screeny,16,2

SetBuffer BackBuffer()
player=LoadImage("alien.bmp")

Type aliens
	Field char
	Field x
	Field y
	Field speed
	Field jump_height
	Field jump_width
	Field jump_angle
	Field jump_speed
End Type

alien.aliens=New aliens
alien\char=player
alien\x=screenx/2
alien\y=screeny/2
alien\speed=2
alien\jump_height=100
alien\jump_width=60
alien\jump_angle=0
alien\jump_speed=4

char=alien\char
charx=alien\x
chary=alien\y
charspeed=alien\speed
height=alien\jump_height
width=alien\jump_width
charangle=alien\jump_angle
jumpspeed=alien\jump_speed

direction=0 : jumping=0

While Not KeyHit(1)
	Cls
	
	; detect left key
	If KeyDown(203)
		charx=charx-charspeed
		direction=-1
	EndIf
	
	; detect right key
	If KeyDown(205) 
		charx=charx+charspeed
		direction=1
	EndIf
	
	; detect jump space key
	If KeyDown(57) And jumping=0
		xoffset=charx
		yoffset=chary
		jumping=1
	EndIf
	
	If jumping=1 Then Gosub jump
		
	If charx<0 Then charx=0
	If charx>screenx-ImageWidth(char) Then charx=screenx-ImageWidth(char)
	
	DrawImage char,charx,chary
	Flip
Wend

For alien.aliens=Each aliens
	Delete alien
Next

End

; jump routine
.Jump
If charangle < 181

   If direction=-1
      x=Cos(charangle)*width/2
      charx=xoffset+x-width/2
	  direction=0
   EndIf
   If direction=1
      x=-Cos(charangle)*width/2
      charx=xoffset+x+width/2
	  direction=0 
   EndIf

   y=-Sin(charangle)*height/2

   chary=yoffset+y
   charangle=charangle+charspeed*jumpspeed

Else	

   jumping=0
   chary=yoffset
   charangle=0

EndIf
Return


Any ideas on how to make it better??


Agamer(Posted 2004) [#9]
Get rid off thouse gosubs sorry


electronin(Posted 2004) [#10]
LarsG - thanks for spotting that ;)