-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example - Platformer ladders.bb
350 lines (307 loc) · 7.18 KB
/
Example - Platformer ladders.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
;
; Platformer with ladders
;
; Type : single screen
;
;
; The collision used is done by scanning the 9 tiles ontop and around the
; player. In this example the outside of the map is checked and the block
; tiles. The collision was therefore done with rectsoverlap.
;
;
; A system checks flags whenever a player is ontop of a ladder and changes
; the player onladder states. Also it sets the nearest top and nearest
; bottom coordinates which we can use to exit the ladder climbing state.
; When the player presses up or down he climbs until the ladderbound is
; reached and then sets the usingladder state to false.
;
;
; Graphics used :
; Created in the program
; Level size :
; 9*9 squares
; Special :
; Mouse left places player anywhere on the map
Graphics 500,400,32,2
SetBuffer BackBuffer()
Dim map(9,9)
Type fps
Field fps,counter,timer
End Type
Global fps.fps = New fps
Type player
Field x#,y#
Field jump,fall,grav#
Field onladder,ladderbottom,laddertop,usingladder
End Type
Global player.player = New player
Type mymap
Field nothing
Field block
End Type
Type gfx
Field tiles[5]
Field player
End Type
Global gfx.gfx = New gfx
player\grav = 0.1
; Initiate the system
inigfx
readlevel
player\fall = True
Color 255,255,255
While KeyDown(1) = False
Cls
drawlevel
drawplayer
playercontrol
levelactive
gravity
If MouseDown(1) = True Then player\x = MouseX(): player\y = MouseY()
updatefps
Text 0,0,fps\fps
Flip True
Wend
End
Function playeronladder(x,y)
px = ((player\x+x) / 32) - 1
py = ((player\y+y) / 32) - 1
If RectsOverlap(px,py,1,1,0,0,10,10) = False Then Return
If map(px,py) = 2 Then Return True
If map(px,py) = 3 Then Return True
End Function
Function levelactive()
px = (player\x / 32) - 1
py = (player\y / 32) - 1
player\onladder = False
If RectsOverlap(px,py,1,1,0,0,10,10) = False Then Return
If map(px,py) = 2 Then player\onladder = True
If map(px,py) = 3 Then player\onladder = True
If map(px,py) = 2 Then player\laddertop = py*32 : Line 0,py*32,320,py*32
If py-1 > 0
If map(px,py-1) = 3 Then player\laddertop = py*32
EndIf
If py < 9 Then
If map(px,py+1) = 3 Then player\ladderbottom = py*32+32 : Line 0,py*32+96,320,py*32+96
If map(px,py+1) = 2 Then player\ladderbottom = py*32+32 : Line 0,py*32+96,320,py*32+96
End If
If player\onladder = False Then player\usingladder = False
End Function
Function gravity()
If player\fall = True Then
For i=0 To player\grav
player\y = player\y + .1;player\grav
If playertilecollision(0,2) = True Or playeronladder(0,2) = True
player\fall = False
player\grav = False
player\y = player\y/32*32
player\jump = False
End If
Next
player\grav = player\grav + .1
End If
If playertilecollision(0,2) = False And playeronladder(0,2) = False
If player\usingladder = False
player\fall = True
End If
End If
If player\jump = True Then
player\y=player\y + player\grav
player\grav = player\grav + .1
If player\grav > 0 Then player\jump = False : player\fall = True
End If
End Function
Function playercontrol()
If KeyDown(200) = True Then ; cursor up jump
If player\jump = False And player\fall = False
If player\onladder = False Then
If playertilecollision(0,0) = False Then
player\jump = True
player\grav = -1.55
End If
End If
If player\onladder = True Then
If playertilecollision(0,-4) = False Then
player\y = player\y - 1
player\usingladder = True
If player\y+16 < player\laddertop Then
player\onladder = False
player\usingladder = False
End If
End If
End If
End If
End If
If KeyDown(205) = True Then ; cursor right ; right
If playertilecollision(1,0) = False Then
player\x = player\x + 1
End If
End If
If KeyDown(203) = True Then ; cursor left ; left
If playertilecollision(-1,0) = False Then
player\x = player\x - 1
End If
End If
If KeyDown(208) = True Then
If playeronladder(0,2) = True Then
If playertilecollision(0,0) = False
player\onladder = True
player\usingladder = True
player\y = player\y + 1
If player\y > player\ladderbottom Then
player\onladder = False
player\usingladder = False
End If
End If
End If
End If
End Function
Function playertilecollision(x1,y1)
;
; This collision checks a segment of the map against the player
;
; collide with walls
px = (player\x-32)/32
py = (player\y-32)/32
For y=-1 To 1
For x=-1 To 1
If RectsOverlap(px+x,py+y,1,1,0,0,10,10) Then
;Rect (px*32)+(x*32),(py*32)+(y*32),32,32,False
If map(px+x,py+y) = 1 Then
If RectsOverlap((player\x)+x1,(player\y)+y1,16,18,(px*32)+(x*32)+32,(py*32)+(y*32)+32,32,32) = True Then
Return True
End If
End If
End If
Next:Next
; out of level
If RectsOverlap(player\x+x1-16,player\y+16+y1,16,16,32,32,9*32,10*32) = False Then Return True
End Function
Function drawplayer()
DrawImage gfx\player,player\x,player\y
End Function
Function drawlevel()
For x=0 To 11
DrawImage gfx\tiles[1],x*32,y*32
DrawImage gfx\tiles[1],x*32,y*32+11*32
Next
For y=0 To 11
DrawImage gfx\tiles[1],0*32,y*32
DrawImage gfx\tiles[1],11*32,y*32
Next
For y=0 To 9
For x=0 To 9
tmp = map(x,y)
DrawImage gfx\tiles[map(x,y)],x*32+32,y*32+32
Next:Next
End Function
Function readlevel()
Restore level1
For y=0 To 9
For x=0 To 9
Read a
If a = 8 Then player\x = 32*x+32+1 : player\y = 32*y+32+1
Select a
Case 0
map(x,y) = a
Case 1
map(x,y) = a
Case 2
map(x,y) = a
Case 3
map(x,y) = a
End Select
Next:Next
End Function
.level1
Data 9,0,0,0,0,0,0,0,0,0
Data 1,1,3,1,1,1,1,1,1,1
Data 0,0,2,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,1,3,1,1
Data 0,0,0,0,0,0,0,2,0,0
Data 1,3,1,1,1,1,1,1,1,1
Data 0,2,0,0,0,0,0,0,0,0
Data 0,2,0,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,1,1,3,1
Data 8,0,0,0,0,0,0,0,2,0
Function inigfx()
gfx\tiles[0] = makeblacktile()
gfx\tiles[1] = makeblocktile()
gfx\tiles[2] = makeladdertile()
gfx\tiles[3] = makeladdertile2()
gfx\player = makeplayer()
End Function
Function makeplayer()
Local tmpim = CreateImage(16,16)
SetBuffer ImageBuffer(tmpim)
;
Color 0,255,0
Oval 0,0,16,16
Color 255,0,0
Rect 0,0,16,16,False
;
SetBuffer BackBuffer()
Return tmpim
End Function
Function makeladdertile2() ; with block behind it
Local tmpim = CreateImage(32,32)
SetBuffer ImageBuffer(tmpim)
;
For y=0 To 32 Step 9
Color 200,200,0
Rect 2,2+y,30,5
Color 255,255,255
For x1=0 To 1
For y1=0 To 1
Rect 4+x1*20,2+y,4,4
Next:Next
Next
;
SetBuffer BackBuffer()
Return tmpim
End Function
Function makeladdertile()
Local tmpim = CreateImage(32,32)
SetBuffer ImageBuffer(tmpim)
;
ClsColor 0,0,0
Cls
For y=0 To 32 Step 9
Color 200,200,0
Rect 2,2+y,30,5
Color 255,255,255
For x1=0 To 1
For y1=0 To 1
Rect 4+x1*20,2+y,4,4
Next:Next
Next
;
SetBuffer BackBuffer()
Return tmpim
End Function
Function makeblocktile()
Local tmpim = CreateImage(32,32)
SetBuffer ImageBuffer(tmpim)
ClsColor 100,100,100
Cls
Color 150,150,150
Rect 0,0,32,32,False
SetBuffer BackBuffer()
Return tmpim
End Function
Function makeblacktile()
Local tmpim = CreateImage(32,32)
SetBuffer ImageBuffer(tmpim)
ClsColor 5,5,5
Cls
SetBuffer BackBuffer()
Return tmpim
End Function
Function updatefps()
fps\counter = fps\counter + 1
If fps\timer < MilliSecs() Then
fps\fps = fps\counter
fps\counter = 0
fps\timer = MilliSecs() + 1000
End If
End Function