uxn/projects/examples/demos/snake.tal

167 lines
4.0 KiB
Tal
Raw Normal View History

2024-03-24 12:54:15 -04:00
( uxnemu snake.rom )
2021-11-02 16:13:59 -04:00
2024-03-24 12:54:15 -04:00
|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
|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
|c0 @DateTime &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1
|000
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
@arena &w $1 &h $1
2024-03-24 13:55:51 -04:00
@apple &pos &x $1 &y $1
@snake &pos &x $1 &y $1 &direction $1 &length $1 &dead $1 &tail $40
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
|100
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
@on-reset ( -> )
2024-03-24 12:54:15 -04:00
( | theme )
#0f7e .System/r DEO2
#0fe6 .System/g DEO2
2021-11-02 17:04:26 -04:00
#0f62 .System/b DEO2
2024-03-24 12:54:15 -04:00
( | vectors )
2021-11-02 16:13:59 -04:00
;on-frame .Screen/vector DEO2
;on-button .Controller/vector DEO2
2024-03-24 12:54:15 -04:00
( | resize )
2021-11-02 17:04:26 -04:00
#00c8 .Screen/width DEO2
2021-11-02 16:13:59 -04:00
#0100 .Screen/height DEO2
2024-03-24 12:54:15 -04:00
( | set arena )
2024-03-24 13:46:24 -04:00
.Screen/width DEI2 #03 SFT2 .arena/w STZ
POP .Screen/height DEI2 #03 SFT2 .arena/h STZ
POP [ LIT2 01 -Screen/auto ] DEO
2024-03-24 13:29:34 -04:00
reset BRK
2021-11-02 16:13:59 -04:00
@on-frame ( -> )
2024-03-24 13:29:34 -04:00
[ LIT &f $1 ] INCk ,&f STR
#04 GTH ?{ BRK }
[ LIT2 00 _&f ] STR
#00 <draw-snake>
<move>
2024-03-24 13:46:24 -04:00
[ LIT2 02 -snake/dead ] LDZ ADD <draw-snake>
2024-03-24 13:29:34 -04:00
#83 <draw-apple>
<draw-score>
BRK
2021-11-02 16:13:59 -04:00
@on-button ( -> )
2024-03-24 12:54:15 -04:00
.Controller/button DEI
2024-03-24 13:29:34 -04:00
( ) DUP #f0 AND ?&arrow
( ) #0f AND ?&button
BRK
&arrow ( button -> )
#04 SFT .snake/direction STZ
BRK
&button ( -> )
reset BRK
2021-11-02 16:13:59 -04:00
2021-11-02 17:04:26 -04:00
@reset ( -- )
2024-03-24 13:29:34 -04:00
#00 <draw-snake>
#00 <draw-apple>
[ LIT2 00 -snake/dead ] STZ
[ LIT2 00 -snake/length ] STZ
[ LIT2 00 -snake/direction ] STZ
2022-11-20 17:50:19 -05:00
.arena/w LDZ #01 SFT #01 SUB .snake/x STZ
.arena/h LDZ #01 SFT #01 SUB .snake/y STZ
2024-03-24 13:29:34 -04:00
#03 <draw-snake>
!<add-apple>
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
@<move> ( -- )
2024-03-24 14:09:33 -04:00
( | copy tail )
[ LITr -snake/pos ] LDZ2r
2021-11-02 16:13:59 -04:00
.snake/length LDZ #00
2024-03-24 12:54:15 -04:00
&>loop ( -- )
2024-03-24 14:09:33 -04:00
DUPk ADD .snake/tail ADD LDZ2k STH2
SWP2r STH2r ROT STZ2
2024-03-24 13:29:34 -04:00
INC GTHk ?&>loop
2024-03-24 13:46:24 -04:00
POP2 POP2r
2024-03-24 14:09:33 -04:00
( ) .snake/dead LDZ ?&end
.snake/direction LDZ
( ) DUP #01 NEQ ?{
2024-03-24 13:46:24 -04:00
.snake/y LDZ #01 SUB .arena/h LDZ [ LTHk JMP SWP POP ] .snake/y STZ }
DUP #02 NEQ ?{
.snake/y LDZ INC .arena/h LDZ DIVk MUL SUB .snake/y STZ }
DUP #04 NEQ ?{
.snake/x LDZ #01 SUB .arena/w LDZ [ LTHk JMP SWP POP ] .snake/x STZ }
DUP #08 NEQ ?{
.snake/x LDZ INC .arena/w LDZ DIVk MUL SUB .snake/x STZ }
POP
( | detect collision with apple )
2024-03-24 13:55:51 -04:00
.snake/pos LDZ2 .apple/pos LDZ2 NEQ2 ?{
2024-03-24 13:29:34 -04:00
#00 <draw-apple>
2024-03-24 14:09:33 -04:00
.snake/length LDZk INC SWP STZ
2024-03-24 13:29:34 -04:00
<add-apple>
2024-03-24 14:09:33 -04:00
!<move> }
2024-03-24 13:55:51 -04:00
( | detect collision with body )
2021-11-02 17:04:26 -04:00
.snake/length LDZ #01
2024-03-24 13:46:24 -04:00
&>loop-body ( -- )
2024-03-24 13:55:51 -04:00
DUPk ADD .snake/tail ADD LDZ2 .snake/pos LDZ2 NEQ2 ?{
2024-03-24 13:46:24 -04:00
[ LIT2 01 -snake/dead ] STZ
#03 <draw-snake> }
INC GTHk ?&>loop-body
2024-03-24 14:09:33 -04:00
POP2 &end JMP2r
2024-03-24 13:29:34 -04:00
@<add-apple> ( -- )
.DateTime/hour DEI2 .DateTime/minute DEI2 MUL2
( ) DUP2 #1234 MUL2 ADD .arena/w LDZ DIVk MUL SUB .apple/x STZ
( ) #abcd MUL2 ADD .arena/h LDZ DIVk MUL SUB .apple/y STZ
JMP2r
(
@|drawing )
@<draw-snake> ( color -- )
2021-11-02 17:04:26 -04:00
STH
2024-03-24 13:29:34 -04:00
( | draw tail )
2021-11-02 17:04:26 -04:00
;snake-icns .Screen/addr DEO2
2024-03-24 13:46:24 -04:00
[ LIT2 -snake/tail -snake/length ] LDZ DUP ADD OVR ADD SWP
2024-03-24 12:54:15 -04:00
&>loop ( -- )
2024-03-24 13:55:51 -04:00
LDZ2k #0005 SFT2 .Screen/y DEO2
#0005 SFT2 .Screen/x DEO2
2024-03-24 13:46:24 -04:00
DUPr [ LITr -Screen/sprite ] DEOr
2024-03-24 13:36:25 -04:00
INC INC GTHk ?&>loop
2021-11-02 16:13:59 -04:00
POP2
2024-03-24 13:29:34 -04:00
( | draw head )
2024-03-24 13:55:51 -04:00
.snake/pos LDZ2 #0005 SFT2 .Screen/y DEO2
#0005 SFT2 .Screen/x DEO2
2021-11-02 17:04:26 -04:00
;snake-icns/face .Screen/addr DEO2
2024-03-24 13:46:24 -04:00
[ LITr -Screen/sprite ] DEOr
2024-03-24 13:29:34 -04:00
JMP2r
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
@<draw-apple> ( color -- )
2022-11-20 17:50:19 -05:00
.apple/x LDZ #0005 SFT2 .Screen/x DEO2
.apple/y LDZ #0005 SFT2 .Screen/y DEO2
2024-03-24 13:55:51 -04:00
STHk ;apple-chr ;fill-icn [ LITr 01 JCNr SWP2 POP2 ] .Screen/addr DEO2
2021-11-02 17:04:26 -04:00
.Screen/sprite DEO
2024-03-24 13:29:34 -04:00
JMP2r
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
@<draw-score> ( -- )
#0010 DUP2 .Screen/x DEO2
.Screen/y DEO2
.snake/length LDZ
( x0 ) DUP #04 SFT <draw-num>
2024-03-24 13:46:24 -04:00
( >> )
2021-11-02 16:13:59 -04:00
2024-03-24 13:29:34 -04:00
@<draw-num> ( num -- )
2024-03-24 13:46:24 -04:00
#000f ROT AND #30 SFT2 ;font-hex ADD2 .Screen/addr DEO2
2024-03-24 13:29:34 -04:00
[ LIT2 41 -Screen/sprite ] DEO
JMP2r
(
@|assets )
@snake-icns [ 7eff ffff ffff ff7e ]
&face [ 7eff ffdb ffe7 ff7e ]
2021-11-02 17:04:26 -04:00
2024-03-24 12:54:15 -04:00
@apple-chr [
2024-03-24 13:29:34 -04:00
0000 76ff ffff 7e3c 1008 0000 0000 0000 ]
2024-03-24 13:46:24 -04:00
@fill-icn [ ffff ffff ffff ffff ]
2024-03-24 12:54:15 -04:00
@font-hex [
2021-11-02 17:04:26 -04:00
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
2024-03-24 12:54:15 -04:00
007c 8280 f080 827c 007c 8280 f080 8080 ]
2021-11-02 16:13:59 -04:00