Code archives/Miscellaneous/2D bullet code for the beginner

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

Download source code

2D bullet code for the beginner by Rob2001
try to pick apart the useful bits... you can modify UpdateMissiles() function to turn them into homing missiles, bombs and more.
;simple 2D missile/bullet code
;use cursor keys and spacebar

;our missile type
Type missiletype
	Field x,y
End Type
Global missile.missiletype ; missile type 


Graphics 640,480,16,2
SetBuffer BackBuffer()

While Not KeyHit(1)
	Cls
	; move player ship
	If KeyDown(203) ; left
		x=x-2
	ElseIf KeyDown(205) ; right
		x=x+2
	EndIf
	
	If KeyDown(200) ; up
		y=y-2
	ElseIf KeyDown(208) ; down
		y=y+2
	EndIf

	If KeyHit(57) SpawnMissile(x,y)  ; missile spawn (spacebar)
	
	;draw player
	Oval x,y,16,16
	
	;update and draw missiles
	UpdateMissiles()
	
	
	Flip
Wend
End


Function SpawnMissile(missx,missy)
	missile.missiletype=New missiletype
	missile\x=missx
	missile\y=missy
End Function

Function UpdateMissiles()
	For missile.missiletype=Each missiletype
		;whatever suits you here!
		;ie...
		If missile\x>640    ; or a collision...! you decide
			Delete missile
		Else
			missile\x=missile\x+10
			Rect missile\x,missile\y,2,2
		EndIf
	Next
End Function

Comments

Trimer342010
Am new to BLITZ PLUS and this is the type of examples am looking for.. Thank you so much for sharing this code..


Code Archives Forum