-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakout.p8
410 lines (378 loc) · 15.7 KB
/
breakout.p8
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
pico-8 cartridge // http://www.pico-8.com
version 42
__lua__
function _init()
init_p_ups()
init_paddle()
mode_upd = {
start = upd_start,
game = upd_game,
over = upd_over
}
mode_draw = {
start = draw_start,
game = draw_game,
over = draw_over
}
init_game()
end
function _update60()
mode_upd[mode]()
end
function _draw()
mode_draw[mode]()
end
-->8
--core
function update(o)
o.x += o.dx
o.y += o.dy
end
function draw(o)
sspr(
o.sx,
o.sy,
o.w,
o.h,
o.x,
o.y
)
end
-- checks if a will collide
-- with b after movement
function collide(a, b, dx, dy)
return a.x + dx < b.x + b.w
and b.x < a.x + dx + a.w
and a.y + dy < b.y + b.h
and b.y < a.y + dy + a.h
end
function center_text(x, t, y, c)
print(t, x - #t * 2, y, c)
end
-->8
--ball
function spawn_ball(sticky)
add(
balls, {
sx = 24,
sy = 0,
w = 4,
h = 4,
x = paddle.x + paddle.w / 2 - 2,
y = paddle.y - 4,
dx = 1,
dy = -1,
sox=0,
sticky = sticky
}
)
end
function update_ball(ball)
if ball.sticky then
ball.y = paddle.y - ball.h
ball.x = paddle.x + paddle.w / 2 - ball.w / 2 + ball.sox
if paddle.dx > 0 then
ball.dx = 1
elseif paddle.dx < 0 then
ball.dx = -1
end
if btnp() > 8 then
ball.sticky = false
end
return
end
update(ball)
-- wall boundaries
if ball.x <= 1 and ball.dx < 0
or ball.x >= 81 - ball.w and ball.dx > 0 then
ball.dx *= -1
sfx(0)
end
if ball.y <= 1 and ball.dy < 0 then
ball.dy *= -1
sfx(0)
end
-- paddle
if ball.dy > 0 then
if collide(
ball,
paddle,
0,
ball.dy
) then
if paddle.sticky then
set_sticky(false)
ball.sticky=true
ball.sox=ball.x - (paddle.x + paddle.w / 2 - ball.w / 2)
else
ball.dy *= -1
sfx(0)
end
elseif collide(
ball,
paddle,
ball.dx,
0
) then
ball.dx *= -1
sfx(0)
end
end
--bricks
for b in all(bricks) do
if collide(ball, b, 0, ball.dy) then
ball.dy *= -1
hit_brick(b)
elseif collide(ball, b, ball.dx, 0) then
ball.dx *= -1
hit_brick(b)
end
end
if ball.y > 128-ball.h then
del(balls, ball)
if #balls == 0 then
lives -= 1
if lives <= 0 then
mode = 'over'
else
balls = {}
p_ups = {}
spawn_ball(true)
end
end
end
end
-->8
--paddle
function init_paddle()
paddle = {
sx = 8,
sy = 0,
w = 16,
h = 4,
x = 10,
y = 100,
dx = 0,
dy = 0,
sticky = false,
sticky_done=false,
sticky_t = 0
}
end
function set_sticky(sticky)
paddle.sticky = sticky
paddle.sticky_t=0
paddle.sticky_done=true
if sticky then
paddle.sticky_done=false
paddle.sx = 40
paddle.sy = 8
else
paddle.sx = 8
paddle.sy = 0
end
end
function update_paddle()
if paddle.sticky and not paddle.sticky_done then
paddle.sticky_t+=1
local frame = (flr(paddle.sticky_t/3)%6)
paddle.sx = 40+frame*16
if frame == 5 then
paddle.sx = 8
paddle.sy = 4
paddle.sticky_done=true
end
end
if btn(⬅️) then
paddle.dx -= 0.4
elseif btn(➡️) then
paddle.dx += 0.4
end
paddle.dx *= .8
update(paddle)
paddle.x = mid(1, paddle.x, 81 - paddle.w)
end
-->8
--bricks
function init_bricks()
bricks = {}
for i = 0, 9 do
for j = 0, 9 do
local t = mget(i + level * 10 - 10, 16 + j)
if t > 0 then
add(
bricks, {
sx = t % 16 * 8,
sy = flr(t / 16) * 8,
w = 8,
h = 4,
x = 1 + i * 8,
y = 18 + j * 4
}
)
end
end
end
end
function hit_brick(b)
del(bricks, b)
sfx(0)
-- 1/10 odds that a powerup spawns
-- when a ball hits
if rnd() > 0.8 then
spawn_p_up(b.x + b.w/2,b.y + b.h/2)
end
end
-->8
--modes
function init_game()
level = 1
init_level()
lives = 3
mode = 'start'
end
function init_level()
balls = {}
p_ups = {}
spawn_ball(true)
init_bricks()
end
function upd_start()
update_paddle()
if btnp() > 0 then
mode = 'game'
end
end
function draw_start()
cls(5)
center_text(64, "press any key to start", 60, 7)
end
function upd_game()
update_paddle()
foreach(balls, update_ball)
foreach(p_ups,update_p_up)
if #bricks == 0 then
level += 1
init_level()
end
end
function draw_game()
cls(5)
map()
draw(paddle)
foreach(bricks, draw)
foreach(balls, draw)
foreach(p_ups, draw)
center_text(105, 'lives', 60, 7)
for i = 1, lives do
sspr(32, 0, 8, 8, 96 - lives * 5 + i * 10, 68)
end
end
function upd_over()
if btnp() > 8 then
init_game()
end
end
function draw_over()
cls(2)
center_text(64, "game over; you died", 60, 7)
center_text(64, "press any key to retry", 67, 7)
end
-->8
--power-ups
function init_p_ups()
m_ball=0
s_paddle=1
l_pwrup=2
end
function spawn_p_up(x,y)
add(
p_ups, {
sx = 40,
sy = 0,
w = 4,
h = 4,
x = x - 2,
y = y - 2,
dx = 0,
dy = .5,
p_up=flr(rnd(2))
}
)
end
function update_p_up(p_up)
update(p_up)
if collide(p_up, paddle, 0, p_up.dy) then
if p_up.p_up==m_ball then
spawn_ball(false)
elseif p_up.p_up==s_paddle then
set_sticky(true)
end
del(p_ups, p_up)
end
if p_up.y > 128-p_up.h then
del(p_ups, p_up)
end
end
__gfx__
00000000666666666666666606600000088008800330000007700000000000000000000000000000000000000000000000000000000000000000000000000000
000000006dddddddddddddd66dd600008788888837b300007b370000000000000000000000000000000000000000000000000000000000000000000000000000
0070070006dddddddddddd606dd60000888888883bb3000073370000000000000000000000000000000000000000000000000000000000000000000000000000
00077000006666666666660006600000888888880330000007700000000000000000000000000000000000000000000000000000000000000000000000000000
00077000bbbbbbbbbbbbbbbb00000000288888820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700b33333333333333b00000000028888200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000b333333333333b000000000002882000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000bbbbbbbbbbbb0000000000000220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
4444444422222222111111113333333399999999b766666666666666bb76666666666666bbbb766666666666bbbbbbb766666666bbbbbbbbbb76666600000000
49999994288888821cccccc13bbbbbb39aaaaaa9b7ddddddddddddd6b37dddddddddddd6b3337dddddddddd6b3333337ddddddd6b3333333337dddd600000000
49999994288888821cccccc13bbbbbb39aaaaaa907dddddddddddd600b7ddddddddddd600b337ddddddddd600b333337dddddd600b333333337ddd6000000000
44444444222222221111111133333333999999990066666666666600007666666666660000bb76666666660000bbbbb76666660000bbbbbbbb76660000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
66666666666666666666666600000006666666661111111166666666666666666666666611111116888888880000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000000000006061111111111111161111111111111161111111111111116222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
60000000000000060000000060000000061111110611111161111111111111161111111161111111222222220000000000000000000000000000000000000000
66666666666666666666666660000000666666660611111166666666666666666666666661111111222222220000000000000000000000000000000000000000
__map__
2022222222222222222224282828282700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3300000000000000000035252525252900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3032323232323232323234383838383700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0010101010101010100000001100000000110000000000000000000000001212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212120000000000000000
0010111111111111100000000011000011000000000000000000000000001213131313131313131212131313131313131312121313131313131313121213131313131313131212131313131313131312121313131313131313121213131313131313131212131313131313131312121313131313131313120000000000000000
0010111313131311100000001010101010100000121311111111111113121213111111111111131212131111111111111312121311111111111113121213111111111111131212131111111111111312121311111111111113121213111111111111131212131111111111111312121311111111111113120000000000000000
0010111312121311100000101010101010101000000011101010101100001213111010101011131212131110101010111312121311101010101113121213111010101011131212131110101010111312121311101010101113121213111010101011131212131110101010111312121311101010101113120000000000000000
0000000000000000000000141400141400141400000000001313000000001213111013131011131212131110131310111312121311101313101113121213111013131011131212131110131310111312121311101313101113121213111013131011131212131110131310111312121311101313101113120000000000000000
0000000000000000000014141414141414141414000000001313000000001213111013131011131212131110131310111312121311101313101113121213111013131011131212131110131310111312121311101313101113121213111013131011131212131110131310111312121311101313101113120000000000000000
0000000000000000000013001313131313130013000011101010101100001213111010101011131212131110101010111312121311101010101113121213111010101011131212131110101010111312121311101010101113121213111010101011131212131110101010111312121311101010101113120000000000000000
0000000000000000000013001300000000130013121311111111111113121213111111111111131212131111111111111312121311111111111113121213111111111111131212131111111111111312121311111111111113121213111111111111131212131111111111111312121311111111111113120000000000000000
0000000000000000000012001200000000120012000000000000000000001213131313131313131212131313131313131312121313131313131313121213131313131313131212131313131313131312121313131313131313121213131313131313131212131313131313131312121313131313131313120000000000000000
0000000000000000000000000012000012000000000000000000000000001212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212120000000000000000
__sfx__
0001000000000090200a03011030000002b0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000