Spaceship Movement routine (no OOP)

BlitzMax Forums/BlitzMax Tutorials/Spaceship Movement routine (no OOP)

Some folks expressed their desire to see the routine without the Object Oriented Programming elements\

'        1/5/2005 Bill Radford
'	Spaceship Movement tutorial--  "sans OOP"

Strict					' 'strict' = must declare variables
' VARIABLES
Global GW = 640 			' Graphics Height
Global GH = 480			' Graphics Width
Global GD = 0				' Graphics Depth "0" means Windowed Mode
Global MID_GH = GH/2		' Middle of the screen Height (Y)
Global MID_GW = GW/2		' Middle of the screen width  (X)
Global MX,MY				' Used for Mousex() and Mousey()

Graphics(GW,GH,GD)		' set graphics mode

' VARIABLES
	Global x# = GW/2		' SCREEN X and Y POSITION (initially the middle)
	Global y# = GH/2
	Global xint:Int			' x,y integers fot neater screen printing
	Global yint:Int
	Global name:String			' string contains the text name of player
	Global life = 100			' life force (defaults to 100)
	Global entity				' a placeholder for the image of the actor
	Global angle#				' angle# = 0 to 360 degrees
	Global velocity#			' speed of the actor (forward momentum?) 		
	Global angular_velocity#	' rotational speed, i.e. how fast it spins
	Global angular#,thrust#
	Global friction# = 0.99		' Friction slows down the actor slightly each frame
	Global rotfriction# = 0.97	' slows down the rate of spin slightly each frame
		
' Create the ship image
entity = CreateImage(64,64,DYNAMICIMAGE)
	SetColor 255,255,0
	SetLineWidth 3
	DrawLine 0,64,32,0
	DrawLine 32,0,64,64
	DrawOval 24,16,16,16
	GrabImage entity,0,0
	SetImageHandle(entity,32,48)
	
' MAIN LOOP
Repeat
	Cls				' Clear the screen
		' check controls
		angular# = 0 ; thrust# = 0 ' RESET to 0 for this frame
		
		' CHECK KEY INPUTS and 
		If KeyDown(KEY_LEFT) Then angular# = -0.1
		If KeyDown(KEY_RIGHT) Then angular# = 0.1
		If KeyDown(KEY_UP) Then thrust# = .05
		
		' ADD the key input values to the existing velocities
		angular_velocity# = angular_velocity# + angular#
		velocity# = velocity# + thrust#
		' SUBTRACT the effect of FRICTION from the velocities
		angular_velocity# = angular_velocity# * rotfriction#
		velocity# = velocity# * friction
		
		' Calculate the direction of travel (angle is 0-360 degrees, the SIN and COS will return a value
		' between -1.0 and +1.0
		x=x+(velocity*Cos(angle-90))
		y=y+(velocity*Sin(angle-90))
		
		   ' Turn the ship
		
		angle#:+angular_velocity
			If angle#<0.0 Then angle#=360.0
			If angle#>360.0 Then angle#= 0.00
						
			' If the actor goes off screen, WRAP to other side
			If x>GW Then x = 0 
			If x<0 Then x = GW
			If y<0 Then y = GH 
			If y>GH Then y=0

		' show
		xint = x ; yint = y
		
		SetRotation(angle)			' turn spaceship to current angle
		DrawImage(entity,x,y)
		' show text attributes
		SetTransform(0,1.1,1.1) 
		SetColor 0,0,255
		DrawRect x-44,y+30,96,15
		SetColor 255,255,0
			DrawText "x="+xint+" y="+yint,x-40,y+32
			DrawText "Velocity = "+velocity,x-40,y+48

	Flip				' display the backbuffer (flip to front buffer)
Until KeyHit(KEY_ESCAPE)