[bb] 2D bullet code for the beginner by Rob [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : 2D bullet code for the beginner
Author : Rob
Posted : 1+ years ago

Description : try to pick apart the useful bits... you can modify UpdateMissiles() function to turn them into homing missiles, bombs and more.

Code :
Code (blitzbasic) Select
;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
missilex=missx
missiley=missy
End Function

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


Comments :


Trimer34(Posted 1+ years ago)

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