simple jump code anyone?

BlitzPlus Forums/BlitzPlus Beginners Area/simple jump code anyone?

I need some simple basic jumping code or stratagies to go on, I am a big fan of the mario bros. side-veiw games and really want to make a simple one but I cant make them jump!

thanx

Gravity# = .7
YSpeed# = 0
Jump# = -3.0

While NOT KeyHit(1)
YSpeed# = YSpeed# + Gravity#

Ypos# = Ypos# + YSpeed#

if keyhit(57) then YSpeed# = Jump#
Wend


where did you learn to code like that and how long did it take to learn it?

I've been programming in blitz for 4 years or so.

I've also mailed you a copy of a Dizzy thingy... Should show how to do a few things!

Dabz

Iv been programming for about 2 years but just this last year I have really gotten into it, and Dabz, I tryed
to download the program but said its not a "valid archive"...?

mmmm, weird, I zipped it using windows own zippy thingy!?!

Try this:-

http://myweb.tiscali.co.uk/mdplasteringservices/Dizzy.zip

Dabz

Dabz's code shows how a character jumps x frames up and then falls x frames, but the one i provided jumps by velocity so it's more realistic and smoother.

That example was based obviously on the Dizzy character, which needs to spin and land pretty much within the jump frames!

But, he has two sets of code to look at now, which use two different ways... Much better in my opinion! :D

Dabz

sorry, I dont now much about winzip exept to click on the Icons, could ya help me out? and I'v always wondered whats all that weird writing done on notepad, like yours thats labeled "thumbs"

heres the way i learned to make people jump. This is psedo-code, so you'll haveta apply it to ur siduation
if(keyhit(jumpkey)and y=abletojump)
 yv=32
endif

if(y <> abletojump)
 playery=playery-y/3
 yv=yv-1

 ;see if the player has hit the top of a block
 if(player_hit_bottom_of_block)
  yv=0
  while(player_hit_bottom_of_block)
   playery=playery+2
  wend
 endif
 
 ;see if the player has hit the bottom
 if(player_hit_top_of_block)
  yv=abletojump
  while(player_hit_top_of_block
   playery=playery-2
  wend
 endif
endif
 

i just typed this up in a few minutes from what i can remeber, so if somthing is unclear to you, tell me and i'll try to fix it.

on second thought, I dont know how to use Andres or your code in blitz+ with my characters, I mean I can understand it for the most part but how do I USE it?...hmm

idk. o well. maybe if you tell us where ur stuck, we could help.

To unzip, right click the Dizzy.zip icon, select 'Extract All' then follow the wizard, when the wizard finishes, make sure the 'show extracted files' checkbox is ticked and click 'finish'

"thumbs" is a file created by windows, it uses this file to organise the contents of the folder! When unzipped, you shouldnt see it!

Erm, if you get the zip open, below is a version of the source included, I've commented a little more in there, but you are going to have to do what the majority of us have done... study the source code a little more, unless you want to post some code and we could fill in the gaps, while explaining what we've done! :)

 ;DizzyPC
;Written by Michael Denathorn 2005

;Event Message Flags
Const EVENT_None			= $0		
Const EVENT_KeyDown		= $101	
Const EVENT_KeyUp			= $102		
Const EVENT_ASCII			= $103		
Const EVENT_MouseDown		= $201		
Const EVENT_MouseUp		= $202		
Const EVENT_MouseMove		= $203		
Const EVENT_Gadget		= $401		
Const EVENT_Move			= $801		
Const EVENT_Size			= $802		
Const EVENT_Close			= $803		
Const EVENT_Front			= $804		
Const EVENT_Menu			= $1001	
Const EVENT_LostFocus		= $2001		
Const EVENT_GotFocus		= $2002		
Const EVENT_Timer			= $4001		

;Create Window, and set 'Notify' window title
AppTitle "DizzyPC"
hWin = CenterWindow("DizzyPC - Peacock's Coins",346,300,Desktop(),13)

;Windows menu
mainmenu = CreateMenu ("&Game", 0, WindowMenu (hWin))
CreateMenu "&About", 1, mainmenu
CreateMenu "", 2, mainmenu
CreateMenu "E&xit", 3, mainmenu
UpdateWindowMenu hWin

;Create DC
canvas = CreateCanvas(10,10,320,202,hWin)
SetBuffer CanvasBuffer (canvas)
HidePointer canvas
;Frames Per Second
fps = CreateTimer (60)

;Load Graphics
;Border
border = LoadImage("Border.bmp")
;Dizzy
dizzyStand = LoadAnimImage("DizzyStand.bmp",24,24,0,2)
MaskImage dizzyStand,255,0,255
MidHandle dizzyStand
dizzyLeft = LoadAnimImage("DizzyLeft.bmp",23,24,0,8)
If dizzyLeft = 0 Then RuntimeError("Error")
MaskImage dizzyLeft,255,0,255
MidHandle dizzyLeft
dizzyRight = LoadAnimImage("DizzyRight.bmp",23,24,0,8)
MaskImage dizzyRight,255,0,255
MidHandle dizzyRight

dizzyJumpUp = LoadAnimImage("DizzyJumpStraight.bmp",24,24,0,8)
If dizzyJumpUp = False Then RuntimeError"Error"
MaskImage dizzyJumpUp,255,0,255
MidHandle dizzyJumpUp
dizzyJumpLeft = LoadAnimImage("DizzyJumpLeft.bmp",24,24,0,8)
If dizzyJumpLeft = False Then RuntimeError"Error"
MaskImage dizzyJumpLeft,255,0,255
MidHandle dizzyJumpLeft

dizzyJumpRight = LoadAnimImage("DizzyJumpRight.bmp",24,24,0,8)
If dizzyJumpRight = False Then RuntimeError"Error"
MaskImage dizzyJumpRight,255,0,255
MidHandle dizzyJumpRight

;Setup Direction Variables
Const dizzyStill = 0
Const dizzyDirLeft = 1
Const dizzyDirRight = 2
Global dizzyDir	;Holds the value stating which way dizzy is facing
Global dizzyX# ;Dizzy's X location on the screen
Global dizzyY# ;Dizzy's Y location on the screen

;Game area variables and setup
backgroundTiles = LoadAnimImage("GroundTiles.bmp",16,16,0,7)
MaskImage backgroundTiles,255,0,255
solid = $FF800000 ;Solid color used for checking collisions (check the ReadPixelFast Command)

;Game area variables
Const gameAreaOffsetX = 40
Const gameAreaOffsetY = 50
Const areaWidth = 15
Const areaHeight = 9

;Create an array for a tilemap
Dim map(areaWidth-1,areaHeight-1)
;Load the data from the bottom of this source code into the tilemap
Restore screen
For yloop = 0 To areaHeight-1
	For xloop = 0 To areaWidth-1
		Read something
		map(xloop,yloop) = something
	Next
Next

;Setup some variables
dizzyX = 159
dizzyY = 100
jump = False
jumpYDir = -1
;Status bar text
SetStatusText(hWin,"Presented by www.dabzware.co.uk")

;Main loop
Repeat
	;System code - to let us handle system events
	Repeat
	
		e = WaitEvent (1) 
		;Process system events
		Select e		
			Case EVENT_Close
				End
			
			Case EVENT_Menu
				item = EventData ()
				
				Select item
					Case 1
						Notify "DizzyPC - Peacock's Coins (ALPHA)"+Chr$(13)+"Written by Michael Denathorn 2005"+Chr$(13)+Chr$(13)+"www.dabzware.co.uk"
					Case 3
						If Confirm ("Are you sure you want to quit?")
							End
						EndIf
				End Select
				
		End Select
		
	Until e = EVENT_Timer
	
	;Game code
	;Clear the screen
	Cls
	;Draw the border
	DrawImage border,0,0	
	
	;Draw the two dizzys outside the border
	DrawImage DizzyStand,48,18,0
	DrawImage DizzyStand,GadgetWidth(canvas)-48,18,0
	
	;Draw Game Area Background (Or tilemap)
	For yloop = 0 To areaHeight-1
		For xloop = 0 To areaWidth-1
			DrawImage backgroundTiles,(xloop Shl 4) + gameAreaOffsetX ,(yloop Shl 4) + gameAreaOffsetY,map(xloop,yloop)
		Next
	Next
	
	;If the jump button hasnt been pressed
	If jump = False
	  ;If no Left/Right Buttons have been pressed
	If KeyDown(203) = False
		If KeyDown(205) = False
			;Draw the animated standing dizzy
			If MilliSecs() > tmrAnimateStand + 100 Then
				tmrAnimateStand=MilliSecs() 
				frameStand = ( frameStand + 1 ) Mod 2 
			End If
			DrawImage DizzyStand,dizzyX,dizzyY,frameStand
			dizzyDir = dizzyStill
			jumpDir = 0
		End If
	End If
	
	;Move dizzy Left
	;Check if right button is not pressed
	If KeyDown(205) = False
	;Check if left button is pressed (if so)
	If KeyDown(203)
		;move dizzy back one and animage him
		dizzyX = dizzyX - 1
		If MilliSecs() > tmrAnimateLeft + 100 Then
				tmrAnimateLeft=MilliSecs() 
				frameLeft = ( frameLeft + 1 ) Mod 8 
		End If
		;Draw dizzy moving left
		DrawImage DizzyLeft,dizzyX,dizzyY,frameLeft
		dizzyDir = dizzyDirLeft
		jumpDir = -1
	End If
	End If
	
	;As above, but check for right key press
	If KeyDown(205)
		dizzyX = dizzyX + 1
		If MilliSecs() > tmrAnimateRight + 100 Then
			tmrAnimateRight=MilliSecs() 
			frameRight = ( frameRight + 1 ) Mod 8 
		End If
		DrawImage DizzyRight,dizzyX,dizzyY,frameRight
		dizzyDir = dizzyDirRight
		jumpDir = 1
	End If
	
	;Check if the jump flag is false
	If jump = False
		;Check if space (jump button) is pressed
		If KeyHit(57)
			jump = True	;Set jump flag to true
			jumpheight = dizzyY - 40 ;set jump height
			jumpYDir = -1	;setup jump dir
			frameUp = 0
		End If
	End If
	End If
	
	If dizzyX = 280
		dizzyX = 41
	End If
	
	If dizzyX = 40
		dizzyX = 279
	End If
	
	;Jump code
	If jump = True
		;Move dizzy in the required 'jump' direction
		dizzyX = dizzyX + jumpDir
		dizzyY = dizzyY + jumpYDir
		If dizzyY = jumpHeight
			jumpYDir =-jumpYDir
		End If
		
		;check which way he's jumping, and animate accordingly
		If MilliSecs() > tmrAnimateJumpUp + 83 Then
			tmrAnimateJumpUp=MilliSecs() 
			frameUp = ( frameUp + 1 ) Mod 8 
		End If
			
		If jumpDir = 0
			DrawImage dizzyJumpUp,dizzyX,dizzyY,frameUp
		End If
		
		If jumpDir < 0 
			DrawImage dizzyJumpLeft,dizzyX,dizzyY,frameUp
		End If	
		If jumpDir > 0 
			DrawImage dizzyJumpRight,dizzyX,dizzyY,frameUp
		End If	
	End If
	
	;make dizzy fall
	If jump = False
	dizzyY = dizzyY + 1
	End If
	
	;Check for collision
	LockBuffer CanvasBuffer(canvas)
	pixelBottomLeft = ReadPixelFast(dizzyX-6,dizzyY+12)
	pixelBottomRight = ReadPixelFast(dizzyX+6,dizzyY+12)
	pixelBankLeft = ReadPixelFast(dizzyX-11,dizzyY+8)
	pixelBankRight = ReadPixelFast(dizzyX+11,dizzyY+8)
	UnlockBuffer CanvasBuffer(canvas)
	
	
	If pixelBottomLeft = solid Or pixelBottomRight = solid
		dizzyY = dizzyY - 1
		If frameUp = 7
			jump = False
		End If
	End If
	If dizzyDir <> dizzyStill
		If pixelBankLeft = solid Or pixelBankRight = solid
			dizzyY = dizzyY - 2
			jump = False	
		End If
	End If
	
	If pixelmid = solid
		dizzyY = dizzyY - 1
			jump = False
	End If
	
	SetStatusText(hWin,"X:"+(dizzyX Shr 4)+" - Y:"+(dizzyY Shr 4))
	FlipCanvas canvas
Forever

;Center window function
Function CenterWindow (title$, width, height, group = 0, style = 15)
	Return CreateWindow (title$, (ClientWidth (Desktop ()) / 2) - (width / 2), (ClientHeight (Desktop ()) / 2) - (height / 2), width, height, group, style)
End Function

;Screen area data (Tilemap)
.screen
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,2,1,1,1
Data 0,0,0,0,0,0,0,0,0,0,2,4,6,6,6
Data 0,0,0,0,0,0,0,0,0,2,4,6,6,6,6
Data 1,1,1,1,1,1,1,1,1,4,6,6,6,6,6


Right, gotta go to work, late already! :)

Dabz

Hey Dabz, is it really a good idea to use a seperate bitmap for a character's movements/jumps/directions? I mean, sure it looks the same, but if you want the game to run lean, the fewer seperate files the better... After all, individual bitmaps have some seemingly erroneous data in their headers...


Hey Dabz, is it really a good idea to use a seperate bitmap for a character's movements/jumps/directions? I mean, sure it looks the same, but if you want the game to run lean, the fewer seperate files the better... After all, individual bitmaps have some seemingly erroneous data in their headers...



Your right Adam, but Ice T seems new to Blitz, so giving him source that is too overly complicated may just put him off, the Dizzy one above, uses different images for different actions, which may provided more understanding about what one section of the source is doing!

After all, he was after jumping code, not how to orangize images from an image strip! ;)

Dabz

the part with the jump code that I'm having trouble with is that nothings jumping.
how do I put in my image and assign it to the code so that my image is jumping, oh and yeah Dabz Im still working on putting my images all into one strip so thanx

Your right Adam, but Ice T seems new to Blitz, so giving him source that is too overly complicated may just put him off, the Dizzy one above, uses different images for different actions, which may provided more understanding about what one section of the source is doing!

After all, he was after jumping code, not how to orangize images from an image strip! ;)


Of course, the "phsychology factor" I always fail to take into account. Bravo, Dabz, I stand corrected!

Hey! looking back at the first jumping code I just really cant figure out how to apply it in my code! can anyone help?!!!!

p.s I'v been using a cruddy jumping code for a while and I would really like to learn more advanced jumping code techneques but I cant understand it. I want to actually UNDERSTAND it so if I needed to alter the code to fit the situation.

If you want to make a smooth curve, you could use:
speed# = 2.0
y# = y# - speed#
instead of:
y = y - 2

Then, you can decrease the speed by using:
speed# = speed# / 1.02

To make a jump frame, use a variable 'jump' in a Select..Case block:
Select jump

case 0
  if keyhit(space) then jump = 1

case 1
  y = y - 1
  if y < 10 then jump = 2

case 2
  y = y + 1
  if y > 100 then jump = 0

end select


And draw sprites with Ceil#() for more smoth ;)

( instead of Blitz will convert variables in Int() )

but how do I apply that to somthing like




Graphics 800,600

SetBuffer BackBuffer()

AutoMidHandle True
;31x34
metroid = LoadAnimImage("metroid_animation.bmp",25,34,0,4)



metroid_x=400
metroid_y=300
metroid_frame=0

If metroid_frame>3
metroid_frame=0
ElseIf metroid<0
metroid_frame=3
EndIf

While Not KeyDown(1)

Cls

If KeyDown(203)
metroid_x=metroid_x-4
metroid_frame=metroid_frame-1

EndIf

If KeyDown(205)
metroid_x=metroid_x+4
metroid_frame=metroid_frame+1
EndIf

If metroid_frame>3
metroid_frame=0

ElseIf metroid_frame<0
metroid_frame=3

EndIf

DrawImage metroid,metroid_x,metroid_y,metroid_frame
Delay 50
Flip
Wend

^
^
^
^
can any one help with putting jump code in this code?

Hi,

I can possibley shed some light on the subject here for you. There are several things that have to happen in order for the player to appear to "jump". In fact there is no jumping what so ever. The jump code that was shown to you simply pulls the "player image" to the bottom of the screen. There is also no floor defined??

If you already have the walking code down then the "jump code" will be no problem. You have to set up a "flag" to tell the program that you are jumping. for example

; Don't be afraid to comment the code :)
Graphics 800,600

SetBuffer BackBuffer()

AutoMidHandle True
Gravity# = .7 ; this defines what the gravity strength is. A positive number at the y location pulls the character down
Jump# = -3.0 ; this tells how high the player will go before the gravity will affect it. change the jump# value to lower negative number for a higher jump
;31x34
metroid = LoadAnimImage("metroid_animation.bmp",25,34,0,4)

metroid_x=400
metroid_y=300
metroid_frame=0

If metroid_frame>3
metroid_frame=0
ElseIf metroid<0
metroid_frame=3
EndIf

While Not KeyDown(1)

Cls

If KeyDown(57) and jumping=0 ; space bar and check to see if we have already pressed it, no double jump
jumping=1 ; setting this to a 1 value tells the program to execute the jump code calculation.
YSpeed# = Jump# ; this will now affect the location of our player since the yspeed# isn't 0 anymore
EndIf


If KeyDown(203) ; left arrow key
metroid_x=metroid_x-4
metroid_frame=metroid_frame-1

EndIf

If KeyDown(205) ; right arrow key
metroid_x=metroid_x+4
metroid_frame=metroid_frame+1
EndIf

If metroid_frame>3
metroid_frame=0

ElseIf metroid_frame<0
metroid_frame=3

EndIf

If jumping=1 ; check that we have pushed the jump key
YSpeed# = YSpeed# + Gravity# ; this will calculate the y location in pixels
metroid_y = metroid_y + YSpeed# ; this will add our calculated number to our current y position
EndIf
If metroid_y>300 ; check to see if our player has fallen past our floor
metroid_y=300 ; set our y location on 300 so that the player doesn't appear to have its feet in the ground
jumping = 0 ; this is set to 0 so that gravity will not affect the player when he has landed on the floor
EndIf
;Oval metroid_x-3,metroid_y-3,6,6

DrawImage metroid,metroid_x,metroid_y,metroid_frame
Delay 50
Flip
Wend


hope it is commented enought to let you undertand the trickery that what you see on the screen is just that illusion of something happening when it doesn't really do anything but change numbers on ya!!! happy programming

yes!!! thank you, I now understand it!