-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example - Platformer jetpack.bb
365 lines (323 loc) · 6.84 KB
/
Example - Platformer jetpack.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
;
; Hero like platformer (jetpack)
;
; The map is 4 directional scrolling
; the player can plant explosives 'space' and blow up doors
; use the cursors to thrust and move
;
;
Const mapwidth = 128
Const mapheight = 128
Dim map(mapwidth,mapheight)
Type explosives
Field x,y
Field timeout
End Type
Type explosion
Field x,y,velx#,vely#,timeout
End Type
Type gfx
Field tile[32]
Field player
Field mapbuffer
End Type
Global gfx.gfx = New gfx
Type mymap
Field x,y,cx,cy
Field offx,offy
End Type
Global mymap.mymap = New mymap
Type player
Field state
Field x#,y#
Field w,h
Field incx#,incy#
Field thrust
End Type
Global player.player = New player
Graphics 640,480,32,2
SetBuffer BackBuffer()
inigfx
For x=0 To 5
For y=0 To 5
insertprefab(Rand(2),x*12+3,y*12+3)
Next:Next
ClsColor 0,0,0
player\x = 128
player\y = 128-32
player\w = 10
player\h = 16
mymap\cy = 7
mymap\offy = -16
While KeyDown(1) = False
Cls
drawmap
If KeyDown(42) = False Then updatemap
updateplayer
updateexplosives
updateexplosion
playercontrols
;
If KeyHit(57) = True Then playerdropexplosive
;
If MouseDown(1) = True Then player\x = MouseX() : player\y = MouseY()
Text 0,0,playermapcollision(0,0)
If MouseX() > 630 Then mymap\x = mymap\x + 1
Flip
Wend
End
Function iniexplosion(x,y)
For i=0 To 10
this.explosion = New explosion
this\x = x
this\y = y
this\velx = Rand(-3,3)
this\vely = Rand(-3,3)
this\timeout = MilliSecs() + 1000 + Rand(1000)
Next
End Function
Function updateexplosion()
For this.explosion = Each explosion
this\x = this\x + this\velx
this\y = this\y + this\vely
If this\timeout < MilliSecs() Then Delete this
Next
End Function
Function drawexplosion()
For this.explosion = Each explosion
Color 255,255,0
Oval this\x,this\y,4,4
Next
End Function
Function updateexplosives()
For this.explosives = Each explosives
If this\timeout < MilliSecs() Then
iniexplosion(this\x,this\y)
destroydoors(this\x,this\y)
Delete this
End If
Next
End Function
Function drawexplosives()
For this.explosives = Each explosives
Color 150,0,0
Rect this\x,this\y,5,7
Next
End Function
Function playerdropexplosive()
this.explosives = New explosives
this\x = player\x
this\y = player\y+10
this\timeout = MilliSecs() + 3000
End Function
Function handleworld(x,y)
For this.explosives = Each explosives
this\x = this\x + x
this\y = this\y + y
Next
End Function
Function destroydoors(x,y)
x = x / 32 + 1
y = y / 32
For x1=-2 To 2
For y1=-1 To 1
If map(x+mymap\cx+x1,y+mymap\cy+y1) = 2 Then
map(x+mymap\cx+x1,y+mymap\cy+y1) = 0
End If
Next:Next
End Function
Function updatemap()
If player\y > 480-200
scrolldown()
handleworld(0,-1)
End If
While player\x > 640-200
scrollright()
handleworld(-1,0)
Wend
If mymap\cx > 0
While player\x < 200
scrollleft()
handleworld(1,0)
Wend
End If
If mymap\cy > 0 Then
While player\y < 200
scrollup
handleworld(0,1)
Wend
End If
End Function
Function scrollup()
mymap\offy = mymap\offy + 1
player\y = player\y + 1
If mymap\offy > 31 Then
mymap\cy = mymap\cy - 1
mymap\offy = 0
End If
End Function
Function scrollleft()
mymap\offx = mymap\offx + 1
player\x = player\x + 1
If mymap\offx > 31 Then
mymap\cx = mymap\cx - 1
mymap\offx = 0
End If
End Function
Function scrollright()
mymap\offx = mymap\offx - 1
player\x = player\x - 1
If mymap\offx < -31 Then
mymap\cx = mymap\cx + 1
mymap\offx = 0
End If
End Function
Function scrolldown()
mymap\offy = mymap\offy - 1
player\y = player\y - 1
If mymap\offy < -31 Then
mymap\cy = mymap\cy + 1
mymap\offy = 0
End If
End Function
Function updateplayer()
; Rocket thruster
If player\thrust = False
If playermapcollision(0,12) = False
player\thrust=True : player\incy=1
End If
End If
If player\thrust = True Then
If playermapcollision(0,-5) = True Then
player\incy = 1
End If
If player\incy < 0 Then player\incy = player\incy + .1
If player\incy > 0 Then player\incy = player\incy + .01
player\y = player\y + player\incy
For i=0 To player\incy
If playermapcollision(0,i) = True Then
player\thrust = False
player\y = player\y/32*32-1
End If
Next
End If
End Function
Function playercontrols()
If KeyDown(200) = True Then ; up
player\incy = -1.5
player\thrust = True
End If
If KeyDown(203) = True Then ; left
If playermapcollision(-1,0) = False Then
player\x = player\x - 1
End If
End If
If KeyDown(205) = True Then ; right
If playermapcollision(1,0) = False Then
player\x = player\x + 1
End If
End If
End Function
Function playermapcollision(x,y)
px = player\x / 32
py = player\y / 32
For x1=-1 To 3
For y1=-1 To 3
If RectsOverlap(px+x1,py+y1,1,1,0,0,mapwidth,mapheight) = True
If map(px+x1+mymap\cx,py+y1+mymap\cy) > 0 Then
;Rect px*32+mymap\offx+x1*32-32,py*32+mymap\offy+y1*32-32,32,32,False
If RectsOverlap(player\x+x,player\y+y,player\w,player\h,(px*32+x1*32)-32+mymap\offx,(py*32+y1*32)-32+mymap\offy,32,32) = True Then
;Rect px*32+mymap\offx+x1*32-32,py*32+mymap\offy+y1*32-32,32,32,False
Return True
End If
End If
End If
Next:Next
End Function
Function drawplayer()
DrawImage gfx\player,player\x,player\y
End Function
Function drawmap()
SetBuffer ImageBuffer(gfx\mapbuffer)
Cls
For y1 = 0 To 480+32 Step 32
For x1 = 0 To 640+32 Step 32
If RectsOverlap(1/32+mymap\cx,y1/32+mymap\cy,1,1,0,0,mapwidth,mapheight) = True Then
If map(x1/32+mymap\cx,y1/32+mymap\cy) > 0 Then
DrawImage gfx\tile[map(x1/32+mymap\cx,y1/32+mymap\cy)],x1+mymap\offx-32,y1+mymap\offy-32
End If
EndIf
Next:Next
drawexplosion
drawplayer
drawexplosives
SetBuffer BackBuffer()
DrawBlock gfx\mapbuffer,0,0
End Function
Function inigfx()
gfx\tile[1] = makeblocktile()
gfx\tile[2] = makedoortile()
gfx\player = makeplayerimage()
gfx\mapbuffer = CreateImage(800,600)
End Function
Function makeplayerimage()
Local tmpim = CreateImage(10,16)
SetBuffer ImageBuffer(tmpim)
ClsColor 0,0,0
Cls
Color 200,200,0
Rect 0,0,10,16,True
SetBuffer BackBuffer()
Return tmpim
End Function
Function makedoortile()
Local tmpim = CreateImage(32,32)
SetBuffer ImageBuffer(tmpim)
ClsColor 0,70,70
Cls
Color 100,100,100
Rect 0,0,32,32,False
SetBuffer BackBuffer()
Return tmpim
End Function
Function makeblocktile()
Local tmpim = CreateImage(32,32)
SetBuffer ImageBuffer(tmpim)
ClsColor 150,150,150
Cls
Color 200,200,200
Rect 0,0,32,32,False
SetBuffer BackBuffer()
Return tmpim
End Function
Function insertprefab(prefab,x,y)
Select prefab
Case 1
Restore prefab1
Case 2
Restore prefab2
End Select
For y1=0 To 7
For x1=0 To 7
Read a
map(x1+x,y1+y) = a
Next:Next
End Function
.prefab1
Data 1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,0,0
Data 1,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,1,1
.prefab2
Data 1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1
Data 0,0,2,0,0,0,0,1
Data 1,1,1,1,0,0,0,1
Data 1,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1
Data 1,1,1,1,1,1,1,1