-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scrolling Platformer.bb
408 lines (375 loc) · 9.9 KB
/
Scrolling Platformer.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
; Scrolling platformer with enemies and shooting (c key) by Rudy van Etten
;
Graphics 640,480,32,2
SetBuffer BackBuffer()
Const tilewidth = 32
Const tileheight = 32
Const mapwidth = 39
Const mapheight = 29
Dim map(mapwidth,mapheight)
Global mapx = 0
Global mapy = 0
Global mapsx = 0
Global mapsy = 0
Global firedelay
Global playerx = 4*32
Global playery = 2*32
Global playerdirection
Const playerwidth = 48
Const playerheight = 32
Global pfalling = False
Global pjumping = False
Global pjs# = 0.0
Global pfs# = 0.0
Const enemywidth = 32
Const enemyheight = 32
Type enemy
Field x#,y#
Field mx#
Field enemydirection
Field deleteme
End Type
Const bulletwidth = 8
Const bulletheight = 8
Type bullet
Field x#,y#,mx#
Field bulletdirection
Field deleteme
Field timeout
End Type
readlevel()
While KeyDown(1) = False
Cls
For i=0 To 2
moveplayer()
movemap()
playergravity()
Next
updateenemies()
updatebullets()
playerbulletcollision()
drawlevel()
drawenemies()
drawbullets()
drawplayer()
Flip
Wend
End
Function playerbulletcollision()
For this.bullet = Each bullet
For that.enemy = Each enemy
If RectsOverlap(this\x,this\y,bulletwidth,bulletheight,that\x,that\y,enemywidth,enemyheight) = True Then
this\deleteme = True
that\deleteme = True
End If
Next
Next
End Function
Function updatebullets()
Local tx#,ty#
For this.bullet = Each bullet
tx = this\x
ty = this\y
tx=tx+this\mx
this\timeout = this\timeout + 1
If this\timeout > 300
this\deleteme = True
End If
If bmapcollision(tx-32,ty-32) = True Then
this\deleteme = True
End If
this\x = tx
this\y = ty
Next
For that.bullet = Each bullet
If that\deleteme = True Then
Delete that
End If
Next
End Function
Function drawbullets()
For this.bullet = Each bullet
Color 0,0,255
Oval this\x-(mapx*tilewidth)+mapsx-tilewidth,this\y-(mapy*tileheight)+mapsy-tileheight,bulletwidth,bulletheight,True
Next
End Function
Function updateenemies()
Local tx,ty,goahead
For this.enemy = Each enemy
tx = this\x
ty = this\y
If this\enemydirection = -1 Then
tx=tx - 1
Else
tx=tx + 1
End If
goahead = True
Select this\enemydirection
Case -1
If emapcollision(tx-32,ty-32) = True Or emapcollision(tx-64,ty) = False Then
this\enemydirection = 1
goahead = False
End If
Case 1
If emapcollision(tx-32,ty-32) = True Or emapcollision(tx,ty) = False Then
this\enemydirection = -1
goahead = False
End If
End Select
If goahead = True Then
this\x = tx
this\y = ty
End If
Next
For that.enemy = Each enemy
If that\deleteme = True Then
Delete that
End If
Next
End Function
Function drawenemies()
For this.enemy = Each enemy
Color 255,255,0
Rect this\x-(mapx*tilewidth)+mapsx-tilewidth,this\y-(mapy*tileheight)+mapsy-tileheight,enemywidth,enemyheight,True
Next
End Function
Function playergravity()
If pmapcollision(playerx,playery+1) = False
If pfalling = False And pjumping = False
pfalling = True
pfs = 0.0
End If
End If
If pfalling = True Then
If pfs < 2
pfs = pfs + 0.1
End If
For i=0 To pfs
If pmapcollision(playerx,playery+1) = True
pfalling = False
pfs = 0.0
Else
playery = playery + 1
End If
Next
End If
If pjumping = True Then
pjs = pjs - .1
If pjs < 0 Then
pjumping = False
pfalling = True
pfs = 0.0
End If
For i=0 To pjs
If pmapcollision(playerx,playery-1) = True
pjumping = False
pfalling = True
pfs = 0.0
Exit
Else
playery = playery - 1
End If
Next
End If
End Function
Function bmapcollision(px,py)
Local pcx = px / tilewidth
Local pcy = py / tileheight
For y=-2 To 3
For x=-2 To 3
If pcx + x > 0 And pcx+x <mapwidth And pcy + y > 0 And pcy+y<mapheight
If map(pcx+x,pcy+y) = 1
xx = ((pcx)+x)*tilewidth-tilewidth
yy = ((pcy)+y)*tileheight-tileheight
If RectsOverlap(px,py,bulletwidth,bulletheight,xx,yy,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
End Function
Function emapcollision(px,py)
Local pcx = px / tilewidth
Local pcy = py / tileheight
For y=-2 To 3
For x=-2 To 3
If pcx + x > 0 And pcx+x <mapwidth And pcy + y > 0 And pcy+y<mapheight
If map(pcx+x,pcy+y) = 1
xx = ((pcx)+x)*tilewidth-tilewidth
yy = ((pcy)+y)*tileheight-tileheight
If RectsOverlap(px,py,enemywidth,enemyheight,xx,yy,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
End Function
Function pmapcollision(px,py)
Local pcx = mapx + (px / tilewidth)
Local pcy = mapy + (py / tileheight)
For y=-2 To 3
For x=-2 To 3
If pcx + x > 0 And pcx+x <mapwidth And pcy + y > 0 And pcy+y<mapheight
If map(pcx+x,pcy+y) = 1
xx = ((pcx-mapx)+x)*tilewidth+mapsx-tilewidth
yy = ((pcy-mapy)+y)*tileheight+mapsy-tileheight
If RectsOverlap(px,py,playerwidth,playerheight,xx,yy,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
End Function
Function movemap()
If playerx > GraphicsWidth()/2
If mapx < (mapwidth - GraphicsWidth() / tilewidth)
mapsx = mapsx - 1
playerx= playerx - 1
If mapsx < 0 Then
mapsx = tilewidth
mapx = mapx + 1
End If
End If
End If
If playerx < GraphicsWidth()/2
If mapx > 0
mapsx = mapsx + 1
playerx = playerx + 1
If mapsx > tilewidth
mapsx = 0
mapx = mapx - 1
End If
End If
End If
If playery > GraphicsHeight() / 2
If mapy < (mapheight - GraphicsHeight() / tileheight)
If pmapcollision(playerx,playery-1) = False
mapsy = mapsy - 1
playery = playery - 1
If mapsy < 0
mapsy = tileheight
mapy = mapy + 1
End If
End If
End If
End If
If playery < GraphicsHeight() / 2
If mapy > 0
If pmapcollision(playerx,playery+1) = False
mapsy = mapsy + 1
playery = playery + 1
If mapsy > tileheight
mapsy = 0
mapy = mapy - 1
End If
End If
End If
End If
End Function
Function moveplayer()
Local px = playerx
Local py = playery
If KeyDown(203) ; left
If pmapcollision(px-1,py) = False
px = px - 1
End If
playerdirection = -1
End If
If KeyDown(205) ; right
If pmapcollision(px+1,py) = False
px = px + 1
End If
playerdirection = 1
End If
If KeyDown(57) ; space
If pjumping = False And pfalling = False
pjumping = True
pjs = 4.0
End If
End If
If KeyDown(46) ; c fire key
If firedelay < MilliSecs()
this.bullet = New bullet
Select playerdirection
Case -1
this\x = playerx+tilewidth-8+(mapx*tilewidth)
this\y = playery+(mapy*tileheight)-mapsy+tileheight
this\mx = -1
this\bulletdirection = -1
Case 1
this\x = playerx + playerwidth+tilewidth+(mapx*tilewidth)
this\y = playery+(mapy*tileheight)-mapsy+tileheight
this\mx = 1
this\bulletdirection = 1
End Select
firedelay = MilliSecs()+500
End If
End If
playerx = px
playery = py
End Function
Function drawplayer()
Color 255,0,0
Rect playerx,playery,playerwidth,playerheight,True
End Function
Function drawlevel()
For y=0 To GraphicsHeight() / tileheight
For x=0 To GraphicsWidth() / tilewidth
If map(x+mapx,y+mapy) = 1 Then
Color 255,255,255
Rect x*tilewidth+mapsx-tilewidth,y*tileheight+mapsy-tileheight,32,32,True
End If
Next
Next
End Function
Function readlevel()
For y=0 To mapheight
For x=0 To mapwidth
Read a
Select a
Case 1
map(x,y) = 1
Case 2
this.enemy = New enemy
this\x = x*tilewidth+tilewidth
this\y = y*tileheight
this\enemydirection = -1
this\mx = 0.1
End Select
Next
Next
End Function
.leveldata
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 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,0,0,1,1
Data 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,0,0,1,1
Data 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,0,0,1,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1,1,1,1,1,1,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,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