Code archives/Algorithms/Simple Gravity

This code has been declared by its author to be Public Domain code.

Download source code

Simple Gravity by Valorden
(Posted 21 years ago)
Simple gravity effect for a platformer
Graphics 800, 600, 16, 2
SetBuffer BackBuffer()

Global X# = 400
Global Y# = 525

Global GRAVITY# = 4

Global JUMP = False

While Not KeyHit(1)

	Cls
	
		UPDATE_MOVEMENT()
	
	Flip
	
Wend
End

Function UPDATE_MOVEMENT()

	If KeyDown(57) = True				; INITIATE THE JUMP
		JUMP = True
	EndIf
	If KeyDown(203) = True
		X# = X# - 2
	EndIf
	If KeyDown(205) = True
		X# = X# + 2
	EndIf
	
	If JUMP = True 
		Y# = Y# - GRAVITY#
		GRAVITY# = GRAVITY# - .08		; ADJUST THIS TO CHANGE THE HEIGHT OF THE JUMP
		If Y# >= 524
			JUMP = False
			GRAVITY# = 4
		EndIf
	EndIf

	Oval X#, Y#, 25, 25, 0 				; BALL
	Rect 0, 550, 800, 50, 1				; FLOOR
	
	Text 0,0,"Press L and R arrow keys to move left and right"
	Text 0,10,"Press space bar to jump"

End Function

Comments

Rob Farley
(Posted 21 years ago)
No trying to hijack just showing another approach, I wrote this ages ago. If you want me to delete it I will.

; Thrust type example by Rob Farley (http://www.mentalillusion.co.uk)
; Use the cursor keys to move it block around the screen

Graphics 640,480,16 ; set graphics mode
SetBuffer BackBuffer() ; set back buffer to enable screen flipping
Cls


; set up some varibles all of the ones below have a #
; this means they are floating point numbers ie they have a decimal place
x#=320 ;x position
y#=200 ;y position
xspeed#=0 ; xspeed
yspeed#=0 ; yspeed

; these are the keys for control Const sets them up as constant varibles
; this means they can't be changed but it makes everything a bit faster
Const k_left=203, k_right=205, k_up=200 , k_down=208


; start the main loop
Repeat ; note the loop is indented for easy to read code


	Cls ;clear the screen

	Color 0,255,0
	Line 0,470,639,470 ;draw a green line at the bottom of the screen

	If KeyDown(k_left) Then xspeed#=xspeed#-.1 ;this adds acceleration in the correct direction
	If KeyDown(k_right) Then xspeed#=xspeed#+.1
	If KeyDown(k_up) Then yspeed#=yspeed#-.2

	x#=x#+xspeed# ;this adds the acceleration to the current x,y position
	y#=y#+yspeed#

	xspeed#=xspeed#*.99 ;this makes the xspeed 99% of what is was ie adds friction and slows you down
	yspeed#=yspeed#+.05 ; here we are adding gravity acceleration to the y direction so you're always being pulled down


	If y#>460 Then y#=460:yspeed#=-yspeed#/2 ; this is hitting the bottom of the screen and bouncing you up at half the speed you hit it
	If y#<0 Then y#=0:yspeed#=0 ; stopping you at the top of the screen
	If x#>650 Then x#=-10 ; wrapping you from right to left
	If x#<-10 Then x#=650 ; wrapping you from left to right

	Color 255,255,0
	Rect x#-10,y#-10,20,20 ; draw a yellow rectangle at your position

	Flip ; flip the back buffer to the front so you can see what's been drawn

Until KeyHit(1) ; back to the beginning of the main loop until ESC is hit

End ; End the program


Valorden
(Posted 21 years ago)
I don't mind.

Regular K
(Posted 21 years ago)
Graphics 800, 600, 0, 60

Global X# = 400
Global Y# = 525

Global GRAVITY# = 4

Global JUMP = False

While Not KeyHit(KEY_ESCAPE)

	Cls
	
		UPDATE_MOVEMENT()
	
	Flip
	
Wend
End

Function UPDATE_MOVEMENT()

	If KeyDown(KEY_SPACE) = True				' INITIATE THE JUMP
		JUMP = True
	EndIf
	If KeyDown(KEY_LEFT) = True
		X# = X# - 2
	EndIf
	If KeyDown(KEY_RIGHT) = True
		X# = X# + 2
	EndIf
	
	If JUMP = True 
		Y# = Y# - GRAVITY#
		GRAVITY# = GRAVITY# - .08		'; ADJUST THIS To CHANGE THE HEIGHT OF THE JUMP
		If Y# >= 524
			JUMP = False
			GRAVITY# = 4
		EndIf
	EndIf

	DrawOval X#, Y#, 25, 25 				'; BALL
	DrawRect 0, 550, 800, 50				'; Floor
	
	DrawText "Press L and R arrow keys to move left and right",0,0
	DrawText "Press space bar to jump",0,10

End Function


Converted your code to Bmax for anyone interested (I was).

mm
(Posted 21 years ago)
From New User MattMars
Thanks for demo + thanks v much for conversion,
its too time consuming to learn an old version of a language just so u can convert examples to the new one.
so much appreciated
mm

Code Archives Forum