-
Notifications
You must be signed in to change notification settings - Fork 1
/
flappy.tal
434 lines (302 loc) · 9.04 KB
/
flappy.tal
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
( flappy thing )
( released under unlicense.org )
( coding convention: roughly follows the standard forth and uxn conventions
but uses the Captal Letters to express short values )
( we use special representations to express y-axis values )
( player: 4-bit fixed point numbers )
( wall: sprite row numbers [divided by 8] )
( macros )
%+ { ADD } %- { SUB } %* { MUL } %/ { DIV }
%< { LTH } %> { GTH } %= { EQU } %! { NEQ }
%++ { ADD2 } %-- { SUB2 } %** { MUL2 } %// { DIV2 }
%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }
%NOT { #00 EQU }
%DEC { #01 SUB } %DEC2 { #0001 SUB2 }
%2* { #10 SFT } %4* { #20 SFT } %8* { #30 SFT }
%2/ { #01 SFT } %4/ { #02 SFT } %8/ { #03 SFT }
%2** { #10 SFT2 } %4** { #20 SFT2 } %8** { #30 SFT2 }
%2// { #01 SFT2 } %4// { #02 SFT2 } %8// { #03 SFT2 }
%RTN { JMP2r }
( devices )
|00 @system [ &Vector $2 &pad $6 &R $2 &G $2 &B $2 ]
|20 @screen [ &Vector $2 &Width $2 &Height $2 &auto $1 &pad $1
&X $2 &Y $2 &Addr $2 &pixel $1 &sprite $1 ]
|80 @controller [ &Vector $2 &button $1 &key $1 ]
( constants )
%Gravity { #0004 }
%Thrust { #0040 }
%Player-x { #0100 }
%Start-y { #0800 }
%Scroll { #0004 }
( variables )
|0000
@player [ &Y $2 &V $2 ]
@wall [ &X $2 &y $1 &h $1 ]
|0080 @score $2
( program )
|0100 ( -> )
( theme )
#07ff .system/R DEO2
#0e4f .system/G DEO2
#0c4f .system/B DEO2
( random seed )
#03 ;rand/a STA
( vector )
;step-game .screen/Vector DEO2
( game reset )
;reset-game JSR2
BRK
@reset-game ( -> )
;clear-screen JSR2
( player reset )
#40 ;draw-player JSR2
Start-y .player/Y STZ2
Thrust .player/V STZ2
( 1st wall )
;init-wall JSR2
#01 ;save-wall JSR2
( 2nd wall )
;init-wall JSR2
.wall/X LDZ2 #00c0 ++ .wall/X STZ2
#02 ;save-wall JSR2
( 3rd wall )
;init-wall JSR2
.wall/X LDZ2 #0180 ++ .wall/X STZ2
#03 ;save-wall JSR2
( reset score )
#0000 .score STZ2
RTN
@step-game ( -> )
( player update )
[ #40 ;draw-player JSR2 ] [ ;update-player JSR2 ]
( 1st wall update )
[ #01 ;load-wall JSR2 ] [ ;update-wall JSR2 ] [ #01 ;save-wall JSR2 ]
[ ;check-collision JSR2 ,&kill JCN ] [ ;draw-wall JSR2 ]
( 2nd wall update )
[ #02 ;load-wall JSR2 ] [ ;update-wall JSR2 ] [ #02 ;save-wall JSR2 ]
[ ;check-collision JSR2 ,&kill JCN ] [ ;draw-wall JSR2 ]
( 3rd wall update )
[ #03 ;load-wall JSR2 ] [ ;update-wall JSR2 ] [ #03 ;save-wall JSR2 ]
[ ;check-collision JSR2 ,&kill JCN ] [ ;draw-wall JSR2 ]
( player draw )
#4a ;draw-player JSR2
( score )
.score LDZ2 INC2 .score STZ2
;score LDZ2 #01 ;draw-score JSR2
BRK
&kill
( replace screen vector )
;step-kill .screen/Vector DEO2
.score LDZ2 #02 ;draw-score JSR2
BRK
@step-kill ( -> )
( increment count until 32 )
,&count LDR INC
DUP #20 > ,&term JCN
DUP ,&count STR
( blinking player with count&2 )
#01 AND #41 ADD ;draw-player JSR2
BRK
&term ( count -- )
( reset all )
POP
#00 ,&count STR
;reset-game JSR2
;step-game .screen/Vector DEO2
BRK
&count $1
( wall )
@init-wall ( -- )
( X = Width )
.screen/Width DEI2 .wall/X STZ2
( y = [rand & 0x1f] + 1 )
;rand JSR2 #1f AND #01 + .wall/y STZ
( h = 8 )
#08 .wall/h STZ
RTN
@update-wall ( -- )
( jump to init-wall if X < -32 )
.wall/X LDZ2 #0020 ++ #8000 >> ;init-wall JCN2
( X -= Scroll )
.wall/X LDZ2 Scroll -- .wall/X STZ2
RTN
@draw-wall ( -- )
( pipe from 0 to [y - 1] )
[ .wall/X LDZ2 ] [ #00 ] [ .wall/y LDZ DEC ] ;draw-pipe JSR2
( ring at [y - 1] )
[ .wall/X LDZ2 ] [ .wall/y LDZ DEC ] ;draw-ring JSR2
( ring at [y + h] )
[ .wall/X LDZ2 ] [ .wall/y LDZ .wall/h LDZ + ] ;draw-ring JSR2
( pipe from [y + h] to [height / 8 - y - h] )
( X ) .wall/X LDZ2
( y ) .wall/y LDZ .wall/h LDZ + INC
( h ) .screen/Height DEI2 8// NIP .wall/y LDZ - .wall/h LDZ -
;draw-pipe JSR2
RTN
@draw-ring ( X y -- )
( sprite position = X, y * 8 )
[ #00 SWP 8** .screen/Y DEO2 ] [ .screen/X DEO2 ]
( sprite pattern and auto increment )
[ ;&sprite .screen/Addr DEO2 ] [ #05 .screen/auto DEO ]
( draw sprites )
#01 .screen/sprite DEOk DEOk DEOk DEOk DEO
RTN
&sprite
ffef efef efef ff00
ffb9 bab9 bab9 ff00
ff40 9040 a840 ff00
ff01 0101 0101 ff00
0000 0000 0000 0000
@draw-pipe ( X y h -- )
( rs = h, &X = X, ws = Y * 8 )
[ STH ] [ ROT ROT ,&X STR2 ] [ #00 SWP 8** ]
( sprite auto increment )
#05 .screen/auto DEO
&yloop
( set position )
[ ,&X LDR2 .screen/X DEO2 ] [ DUP2 .screen/Y DEO2 ]
( draw sprites with auto increment )
;&sprite .screen/Addr DEO2
#01 .screen/sprite DEOk DEOk DEOk DEOk DEO
#0008 ++ ( next row )
STHr DEC STHk ,&yloop JCN ( loop until --h == 0 )
POP2 POPr
RTN
&X $2
&sprite
3737 3737 3737 3737
dcdd dcdd dcdd dcdd
a054 a048 a054 a048
0404 0404 0404 0404
0000 0000 0000 0000
( wall multiplexer )
@save-wall ( idx -- )
4* .wall/X + ( address of wall[idx] )
DUP .wall/X LDZ2 ROT STZ2 ( copy first word )
INC INC .wall/y LDZ2 ROT STZ2 ( copy second word )
RTN
@load-wall ( idx -- )
4* .wall/X + ( address of wall[idx] )
LDZ2k .wall/X STZ2 ( copy first word )
INC INC LDZ2 .wall/y STZ2 ( copy second word )
RTN
( player )
@update-player ( -- )
( reset V on up-key press )
;get-up-key JSR2 NOT ,&no-up JCN
Thrust .player/V STZ2
&no-up
( V += Gravity )
.player/V LDZ2 Gravity ++ .player/V STZ2
( Y += V - 0080 )
.player/Y LDZ2 .player/V LDZ2 ++ #0080 -- .player/Y STZ2
RTN
@check-collision ( -- hit )
( x-check )
( check: wall/X - player/X + 24 < 32 )
.wall/X LDZ2 Player-x -- #0018 ++ #0020 <<
( return 0 on false )
,&x-ok JCN
#00 RTN
&x-ok
( y-check )
( temp = player/Y / 8 / 16 + 1 )
.player/Y LDZ2 #07 SFT2 NIP INC
( check: temp+1 > wall/y )
DUP INC .wall/y LDZ >
( return 1 on false )
,&y-gth JCN
POP #01 RTN
&y-gth
( check: temp > wall/y + wall/h )
.wall/y LDZ .wall/h LDZ + >
( use result as return value )
RTN
@draw-player ( color -- )
STH ( use return stack to store color )
( sprite settings: pattern address and auto increment )
[ ;&sprite #00 .player/V LDZ2 #0080 >> #50 SFT2 ADD2 .screen/Addr DEO2 ] [ #05 .screen/auto DEO ]
( y-axis position )
.player/Y LDZ2 #04 SFT2
( first row )
[ Player-x .screen/X DEO2 ] [ DUP2 .screen/Y DEO2 ]
STHrk .screen/sprite DEOk DEO
( second row )
[ Player-x .screen/X DEO2 ] [ #0008 ++ .screen/Y DEO2 ]
STHr .screen/sprite DEOk DEO
RTN
&sprite
0000 0000 0001 0307
0010 3874 fec0 c0c0
0f1c 3870 0000 0000
a070 7070 7030 1000
( down )
1018 1c1e 0e05 0307
0010 3874 fec0 c0e0
0f1c 3870 0000 0000
e000 0000 0000 0000
( misc )
@clear-screen ( -- )
( sprite pattern and auto increment )
[ ;&sprite .screen/Addr DEO2 ] [ #01 .screen/auto DEO ]
( col = screen/Width / 8 )
.screen/Width DEI2 8// NIP ,&col STR
( ws: row count )
.screen/Height DEI2 8// NIP
&y-loop
DEC ( next row )
( sprite position = 0, row * 8 )
[ #0000 .screen/X DEO2 ] [ DUP #00 SWP 8** .screen/Y DEO2 ]
( ws: DEO params, rs: column count )
[ #00 .screen/sprite ] [ ,&col LDR STH ]
( row fill )
&x-loop DEOk STHr DEC STHk ,&x-loop JCN
POP2 POPr ( stack restore )
DUP ,&y-loop JCN ( loop until row == 0 )
POP
RTN
&col $1
&sprite 0000 0000 0000 0000
@get-up-key ( -- up )
.controller/button DEI #04 SFT #01 AND
RTN
@rand ( -- number )
( 8-bit PRNG https://github.com/edrosten/8bit_rng )
( t = x ^ (x << 4) )
,&x LDR DUP #40 SFT EOR
( x = y )
,&y LDR ,&x STR
( y = z )
,&z LDR ,&y STR
( z = a )
,&a LDR DUP ,&z STR
( a = z ^ t ^ (z >> 1) ^ (t << 1) )
DUP #10 SFT EOR SWP DUP #01 SFT EOR EOR
DUP ,&a STR
RTN
&x $1 &y $1 &z $1 &a $1
@draw-score ( short* color -- )
#0010 .screen/X DEO2
#0010 .screen/Y DEO2
STH SWP
DUP #04 SFT #00 SWP 8** ;font-hex ++ .screen/Addr DEO2
( draw ) STHkr .screen/sprite DEO
#0f AND #00 SWP 8** ;font-hex ++ .screen/Addr DEO2
#0018 .screen/X DEO2
( draw ) STHkr .screen/sprite DEO
DUP #04 SFT #00 SWP 8** ;font-hex ++ .screen/Addr DEO2
#0020 .screen/X DEO2
( draw ) STHkr .screen/sprite DEO
#0f AND #00 SWP 8** ;font-hex ++ .screen/Addr DEO2
#0028 .screen/X DEO2
( draw ) STHr .screen/sprite DEO
RTN
@font-hex
007c 8282 8282 827c 0030 1010 1010 1010
007c 8202 7c80 80fe 007c 8202 1c02 827c
000c 1424 4484 fe04 00fe 8080 7c02 827c
007c 8280 fc82 827c 007c 8202 1e02 0202
007c 8282 7c82 827c 007c 8282 7e02 827c
007c 8202 7e82 827e 00fc 8282 fc82 82fc
007c 8280 8080 827c 00fc 8282 8282 82fc
007c 8280 f080 827c 007c 8280 f080 8080