November 24, 2020, 09:24:58 AM

### Author Topic: [bb] 2D bullet code for the beginner by Rob [ 1+ years ago ]  (Read 497 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] 2D bullet code for the beginner by Rob [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
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
1. ;simple 2D missile/bullet code
2. ;use cursor keys and spacebar
3.
4. ;our missile type
5. Type missiletype
6.         Field x,y
7. End Type
8. Global missile.missiletype ; missile type
9.
10.
11. Graphics 640,480,16,2
12. SetBuffer BackBuffer()
13.
14. While Not KeyHit(1)
15.         Cls
16.         ; move player ship
17.         If KeyDown(203) ; left
18.                 x=x-2
19.         ElseIf KeyDown(205) ; right
20.                 x=x+2
21.         EndIf
22.
23.         If KeyDown(200) ; up
24.                 y=y-2
25.         ElseIf KeyDown(208) ; down
26.                 y=y+2
27.         EndIf
28.
29.         If KeyHit(57) SpawnMissile(x,y)  ; missile spawn (spacebar)
30.
31.         ;draw player
32.         Oval x,y,16,16
33.
34.         ;update and draw missiles
35.         UpdateMissiles()
36.
37.
38.         Flip
39. Wend
40. End
41.
42.
43. Function SpawnMissile(missx,missy)
44.         missile.missiletype=New missiletype
45.         missilex=missx
46.         missiley=missy
47. End Function
48.
49. Function UpdateMissiles()
50.         For missile.missiletype=Each missiletype
51.                 ;whatever suits you here!
52.                 ;ie...
53.                 If missilex>640    ; or a collision...! you decide
54.                         Delete missile
55.                 Else
56.                         missilex=missilex+10
57.                         Rect missilex,missiley,2,2
58.                 EndIf
59.         Next
60. End Function