-
Notifications
You must be signed in to change notification settings - Fork 1
/
Topdown space shooter example.bb
486 lines (439 loc) · 12 KB
/
Topdown space 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
; Topdown Space Shooter Example by Pakz (Rudy van Etten)
; The map is drawn on a image.
; Every object needs to be moved with the player movement speed, this is done
; in the updatemap function.
Graphics 640,480,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Const mapwidth = 39
Const mapheight = 29
Dim map(mapwidth,mapheight)
Global mapimage = CreateImage(mapwidth*32+32,mapheight*32+32)
Global mapx# = 0
Global mapy# = 0
Global myfont = LoadFont("verdana.ttf",16)
SetFont myfont
; tx = the destination of the ship
Type ship
Field x#,y#,mx#,my#,angle,tx,ty,shootdelaytime
Field hull
End Type
Type sbullet ; ship bullets
Field x#,y#,mx#,my#,dtraveled
End Type
Type turret ; the turrets
Field x#,y#,angle,shootdelaytime,hull
End Type
Type tbullet ; turret bullets
Field x#,y#,mx#,my#
Field dtraveled ; Distance traveled by the bullet
End Type
Type player
Field x#,y#,mx#,my#,angle,thrust#
Field image[360],shootdelaytime
Field score,hull
End Type
Type pbullet
Field x#,y#,mx#,my#
Field dtraveled
End Type
Global p.player = New player
p\x = GraphicsWidth()/2-16
p\y = GraphicsHeight()/2-16
initplayer()
restart()
timer = CreateTimer(60)
While KeyDown(1) = False
WaitTimer timer
Cls
updateplayer()
updateturrets()
updateships()
updatemap()
updatepbullets()
updatetbullets()
updatesbullets()
pbulletturretcollision()
pbulletshipcollision()
tbulletplayercollision()
sbulletplayercollision()
drawmap()
drawturrets()
drawships()
drawplayer()
drawpbullets()
drawtbullets()
drawsbullets()
Color 255,255,255
Text GraphicsWidth()-100,5,"Hull:"+p\hull,1,1
Text GraphicsWidth()-100,15,"Turrets left:"+turretsleft(),1,1
Text GraphicsWidth()-100,25,"Ships left:"+shipsleft(),1,1
If turretsleft() = 0 And shipsleft() = 0 Then restart()
Flip
Wend
End
Function sbulletplayercollision()
For this.sbullet = Each sbullet
If RectsOverlap(this\x-3,this\y-3,6,6,p\x-16,p\y-16,32,32) Then
p\hull = p\hull - 1
If p\hull = 0
restart
Return
End If
Delete this
End If
Next
End Function
Function updatesbullets()
For this.sbullet = Each sbullet
this\x = this\x + this\mx
this\y = this\y + this\my
this\dtraveled = this\dtraveled + 1
If this\dtraveled > 640 Then Delete this
Next
End Function
Function drawsbullets()
For this.sbullet = Each sbullet
Color 255,0,0
Oval this\x-3,this\y-3,6,6,True
Next
End Function
Function pbulletshipcollision()
For this.ship = Each ship
For that.pbullet = Each pbullet
If RectsOverlap(this\x-16,this\y-16,32,32,that\x-3,that\y-3,6,6)
Delete that
this\hull = this\hull - 1
End If
Next
If this\hull <= 0 Then Delete this
Next
End Function
Function updateships()
For this.ship = Each ship
da = getangle(this\tx,this\ty,this\x,this\y)
v1 = 0 ; left side
v2 = 0 ; right side
ta = this\angle ; get the current angle into ta
; Count to the left to get the steps to the target angle
exitloop = False
While exitloop = False
ta = ta - 1
v1 = v1 + 1
If RectsOverlap(ta,ta,4,4,da,da,4,4) Exitloop = True
If ta < -180 Then ta = 181
Wend
ta = this\angle ;'get the current angle into ta
; Count to the right to get the steps top the target angle
exitloop = False
While exitloop = False
ta = ta + 1
v2 = v2 + 1
If RectsOverlap(ta,ta,4,4,da,da,4,4) exitloop = True
If ta >= 180 Then ta = -181
Wend
;
If v1 > v2 ; if the left side is closer to the target angle
this\angle = this\angle - 3
Else ; if the right side is closer to the target angle
this\angle = this\angle + 3
End If
; move the missile
this\x = this\x + Cos(this\angle) * 1
this\y = this\y + Sin(this\angle) * 1
If distance(this\x,this\y,p\x,p\y) < 96 Or Rand(0,200) = 1
exitloop = False
While exitloop = False
x1 = Rand(-(GraphicsWidth()*2),GraphicsWidth()*2)
y1 = Rand(-(GraphicsHeight()*2),GraphicsHeight()*2)
If distance(p\x,p\y,x1,y1) > 320
this\tx = x1
this\ty = y1
exitloop = True
End If
Wend
End If
If distance(this\x,this\y,p\x,p\y) > 300
this\tx = p\x
this\ty = p\y
End If
; Shoot at the player if he is nearby
If distance(this\x,this\y,p\x,p\y) < 150
If this\shootdelaytime < MilliSecs()
this\shootdelaytime = MilliSecs() + 1000
sb.sbullet = New sbullet
ta = getangle(this\x,this\y,p\x,p\y)
sb\x = this\x-16 + Cos(ta)*16
sb\y = this\y-16 + Sin(ta)*16
sb\mx = Cos(ta)*2
sb\my = Sin(ta)*2
End If
End If
Next
End Function
Function initships()
For i=0 To 3
this.ship = New ship
this\x = Rand(-(GraphicsWidth()*2),GraphicsWidth()*2)
this\y = Rand(-(GraphicsHeight()*2),GraphicsHeight()*2)
this\tx = p\x+Rand(-64,64)
this\ty = p\y+Rand(-64,64)
this\angle = 0
this\hull = 3
Next
End Function
Function drawships()
For this.ship = Each ship
DrawImage p\image[0],this\x-16,this\y-16
Next
End Function
Function shipsleft()
For this.ship = Each ship
cnt = cnt + 1
Next
Return cnt
End Function
Function turretsleft()
For this.turret = Each turret
cnt = cnt + 1
Next
Return cnt
End Function
Function tbulletplayercollision()
For this.tbullet = Each tbullet
If RectsOverlap(this\x-3,this\y-3,6,6,p\x-16,p\y-16,32,32)
Delete this
p\hull = p\hull - 1
If p\hull < 1
restart
Return
End If
End If
Next
End Function
Function restart()
Delete Each pbullet
Delete Each tbullet
Delete Each sbullet
Delete Each turret
Delete Each ship
p\hull = 10
p\thrust = .1
mapx = 0
mapy = 0
Cls
Text GraphicsWidth()/2,GraphicsHeight()/2,"Get Ready",1,1
Flip
Delay 1000
initmap()
initships()
End Function
Function pbulletturretcollision()
For this.turret = Each turret
For that.pbullet = Each pbullet
If RectsOverlap(this\x+4,this\y+4,32-8,32-8,that\x-3,that\y-3,6,6) Then
Delete that
this\hull = this\hull - 1
End If
Next
If this\hull < 0 Then Delete this
Next
End Function
Function updateplayer()
Local my# = 0
Local mx# = 0
If KeyDown(203)
p\angle = p\angle - 1
If p\angle < 0 Then p\angle = 360
End If
If KeyDown(205)
p\angle = p\angle + 1
If p\angle >= 360 Then p\angle = 0
End If
If KeyDown(200)
mx = Cos(p\angle) * p\thrust
my = Sin(p\angle) * p\thrust
If mx < 0
If p\mx > -1 Then p\mx = p\mx + mx
End If
If mx > 0
If p\mx < 1 Then p\mx = p\mx + mx
End If
If my < 0
If p\my > -1 Then p\my = p\my + my
End If
If my > 0
If p\my < 1 Then p\my = p\my + my
End If
End If
If KeyDown(57) And p\shootdelaytime < MilliSecs()
pb.pbullet = New pbullet
pb\x = GraphicsWidth()/2-16 + Cos(p\angle-180) * 16
pb\y = GraphicsHeight()/2-16 + Sin(p\angle-180) * 16
pb\mx = Cos(p\angle-180) * 4.5
pb\my = Sin(p\angle-180) * 4.5
p\shootdelaytime = MilliSecs() + 200
End If
End Function
Function updateturrets()
For this.turret = Each turret
If distance(this\x,this\y,p\x,p\y) < 220 Then
If this\shootdelaytime < MilliSecs()
a = getangle(this\x,this\y,p\x,p\y)
that.tbullet = New tbullet
that\x = this\x + 16 + Cos(a) * 16
that\y = this\y + 16 + Sin(a) * 16
that\mx = Cos(a)*2
that\my = Sin(a)*2
this\shootdelaytime = MilliSecs()+1000
End If
End If
Next
End Function
Function updatetbullets() ; update turret bullets
For this.tbullet = Each tbullet
this\dtraveled = this\dtraveled + 1
this\x = this\x + this\mx
this\y = this\y + this\my
If this\dtraveled > 640 Then Delete this
Next
End Function
Function drawtbullets() ; draw turret bullet
For this.tbullet = Each tbullet
Color 255,255,0
Oval this\x-3,this\y-3,6,6,True
Next
End Function
Function drawturrets()
For this.turret = Each turret
Color 0,0,255
Oval this\x,this\y,32,32,True
Next
End Function
Function updatepbullets()
For this.pbullet = Each pbullet
this\dtraveled = this\dtraveled + 1
this\x = this\x + this\mx
this\y = this\y + this\my
If this\dtraveled > 640 Then Delete this
Next
End Function
; draw player bullets
Function drawpbullets()
For this.pbullet = Each pbullet
Color 200,0,0
Oval this\x,this\y,6,6,True
Color 255,255,255
Oval this\x,this\y,6,6,False
Next
End Function
Function updatemap()
mapx = mapx + p\mx
mapy = mapy + p\my
; Move every player bullet with the movement speed of the player
For pb.pbullet = Each pbullet
pb\x = pb\x + p\mx
pb\y = pb\y + p\my
Next
; Move every turret
For t.turret = Each turret
t\x = t\x + p\mx
t\y = t\y + p\my
Next
; Move every turret bullet
For tb.tbullet = Each tbullet
tb\x = tb\x + p\mx
tb\y = tb\y + p\my
Next
; Move every ship
For s.ship = Each ship
s\x = s\x + p\mx
s\y = s\y + p\my
Next
End Function
Function drawmap()
DrawImage mapimage,mapx,mapy
End Function
Function initplayer()
Local image = CreateImage(32,32)
MidHandle image
SetBuffer ImageBuffer(image)
Color 255,255,255
Rect 0,0,32,32,True
Color 55,55,55
Oval 0,16-3,6,6,True
For i=0 To 360
p\image[i] = CreateImage(32,32)
p\image[i] = CopyImage(image)
RotateImage p\image[i],i
Next
SetBuffer BackBuffer()
End Function
Function drawplayer()
DrawImage p\image[p\angle],p\x,p\y
End Function
Function initmap()
Restore mymap
For y=0 To mapheight
For x=0 To mapwidth
Read a
If a = 1 Then map(x,y) = a
If a = 2
t.turret = New turret
t\x = x * 32
t\y = y * 32
t\hull = 3
End If
Next
Next
SetBuffer ImageBuffer(mapimage)
For y=0 To mapheight
For x=0 To mapwidth
Select map(x,y)
Case 0:Color 0,0,0
Case 1:Color 55,55,155
End Select
Rect x*32,y*32,32,32,True
Next
Next
SetBuffer BackBuffer()
End Function
.mymap
Data 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1
Data 1,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1
Data 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1
Data 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0
Data 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0
Data 0,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