-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sideview Shooter example.bb
246 lines (229 loc) · 5.1 KB
/
Sideview Shooter example.bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
; Sideview shooter example by Pakz
Graphics 640,480,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Type ast
Field x,y
End Type
Type bullet
Field t,x#,y#,mx#,my#
End Type
Type player
Field x,y,w,h,e ;e=energy
Field s ; s = score
End Type
Type asteroid
Field x#,y#,mx#,my#,f,ftime,p ; f = flash , flash time,p = power
Field im,fim ; image and flash image
End Type
Global p.player = New player
p\x = 32
p\y = GraphicsHeight()/2
p\w = 32
p\h = 32
; make 10 asteroids
For i=0 To 10
newasteroid
Next
timer = CreateTimer(60)
While KeyDown(1) = False
WaitTimer(timer)
Cls
updateplayer()
updatebullets()
updateasteroids()
drawbullets()
drawasteroids()
Color 255,0,0
Oval p\x-p\w/2,p\y-p\h/2,p\w,p\h,True
Color 255,255,255
Text 0,0,"score:"+p\s
Flip
Wend
End
Function newasteroid() ; create a new asteroid
this.asteroid = New asteroid
n = MilliSecs()
; create the images
this\im = CreateImage(64,64)
this\fim = CreateImage(64,64)
; draw the asteroids into the containers
this\im = makeasteroid(0,n)
this\fim = makeasteroid(1,n)
; choose top right or bottom position for asteroid to begin
a = Rand(1,3)
Select a
Case 1 ; top
this\x = Rand(100,GraphicsWidth())
this\y = -48
this\mx = Rand(-1,-2)
this\my = Rand(1,2)
Case 2 ; bottom
this\x = Rand(100,GraphicsWidth())
this\y = GraphicsHeight()+48
this\mx = Rand(-1,-2)
this\my = Rand(-1,-2)
Case 3 ; right
this\x = GraphicsWidth()+48
this\y = Rand(-48,GraphicsHeight()+92)
this\mx = Rand(-1,-2)
this\my = Rand(-1,2)
End Select
this\p = Rand(2,3)
End Function
Function updateasteroids()
For this.asteroid = Each asteroid
; move the asteroids
this\x = this\x + this\mx
this\y = this\y + this\my
; stop the flashing
If this\ftime < MilliSecs() Then this\f = False
; if the asteroid is to far away out of the screen then delete it and remake it elsewhere
If RectsOverlap(this\x-24,this\y-24,48,48,-64,-64,GraphicsWidth()+128,GraphicsHeight()+128) = False
FreeImage this\im
FreeImage this\fim
Delete this
newasteroid()
End If
Next
End Function
Function drawasteroids()
For this.asteroid = Each asteroid
Select this\f
Case False : DrawImage this\im,this\x-32,this\y-32
Case True : DrawImage this\fim,this\x-32,this\y-32
End Select
Next
End Function
Function updatebullets()
For this.bullet = Each bullet
rembullet = False
this\x = this\x + this\mx
this\y = this\y + this\my
For that.asteroid = Each asteroid
Select this\t
Case 1
If RectsOverlap(this\x-3,this\y-16,3,32,that\x-32,that\y-32,64,64)
that\p = that\p - 1
that\f = True
that\ftime = MilliSecs()+120
If that\p < 0
p\s = p\s + 10
FreeImage that\im
FreeImage that\fim
Delete that
newasteroid
End If
rembullet = True
End If
Case 2
If RectsOverlap(this\x-16,this\y-3,32,3,that\x-32,that\y-32,64,64)
that\p = that\p - 1
that\f = True
that\ftime = MilliSecs()+120
If that\p < 0
p\s = p\s + 10
FreeImage that\im
FreeImage that\fim
Delete that
newasteroid
End If
rembullet = True
End If
End Select
Next
If RectsOverlap(this\x,this\y,3,32,-32,-32,GraphicsWidth()+64,GraphicsHeight()+64) = False
Delete this
End If
If rembullet = True Then Delete this
Next
End Function
;
; Move the player towards the mouse position
; Fire if pressed
; If collision with player then reset to start position
Function updateplayer()
a = getangle(p\x,p\y,MouseX(),MouseY())
p\x = p\x + Cos(a) * 5
p\y = p\y + Sin(a) * 5
If MouseHit(1)
initshot()
End If
For this.asteroid = Each asteroid
If RectsOverlap(p\x-16,p\y-16,32,32,this\x-32,this\y-32,64,64)
p\x = 64
p\y = GraphicsHeight()/2-16
p\s = 0
End If
Next
End Function
Function initshot()
this.bullet = New bullet
this\x = p\x
this\y = p\y
this\mx = 12 ; bullet shooting speed
this\my = 0
this\t = 1
this.bullet = New bullet
this\x = p\x
this\y = p\y
this\mx = 0
this\my = -12
this\t = 2
this.bullet = New bullet
this\x = p\x
this\y = p\y
this\mx = 0
this\my = 12
this\t = 2
End Function
Function drawbullets()
For this.bullet = Each bullet
If this\t = 1
Color 0,0,255
Oval this\x,this\y-16,4,32,True
End If
If this\t = 2
Color 0,0,255
Oval this\x-16,this\y,32,4,True
End If
Next
End Function
Function getangle(x1,y1,x2,y2)
Local dx = x2 - x1
Local dy = y2 - y1
Return ATan2(dy,dx)+360 Mod 360
End Function
;
; This function makes a random asteroid
;
; It goes in a circle and uses a random distance to place points
; then it draws lines from the last point to the last point
;
Function makeasteroid(c=0,seed)
Delete Each ast
SeedRnd seed
Local im = CreateImage(64,64)
SetBuffer ImageBuffer(im)
Select c
Case 0:Color 150,150,150
Case 1:Color 255,255,255
End Select
a=-180
For i=0 To 20
a = a + 360/20
this.ast = New ast
this\x = Cos(a)*Rand(20,32)
this\y = Sin(a)*Rand(20,32)
Next
this.ast = Last ast
x1 = this\x
y1 = this\y
For this.ast = Each ast
Line x1+32,y1+32,this\x+32,this\y+32
x1 = this\x
y1 = this\y
Next
SetBuffer BackBuffer()
Return im
End Function