(Snake) Improved formatting

This commit is contained in:
Devine Lu Linvega 2024-03-24 09:54:15 -07:00
parent 370c3a60c9
commit 641872fe21
1 changed files with 55 additions and 66 deletions

View File

@ -1,17 +1,11 @@
( uxnasm projects/examples/demos/snake.tal bin/snake.rom && uxnemu bin/snake.rom ) ( uxnemu snake.rom )
( devices ) |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
|00 @System [ &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 &debug $1 &halt $1 ] |000
|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 &error $1 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|30 @Audio0 [ &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $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 ]
( variables )
|0000
( program ) ( program )
@ -24,26 +18,26 @@
&x $1 &y $1 &x $1 &y $1
&tail &tail
|0100 ( -> ) |100 @on-reset ( -> )
( theme ) ( | theme )
#0f7e .System/r DEO2 #0f7e .System/r DEO2
#0fe6 .System/g DEO2 #0fe6 .System/g DEO2
#0f62 .System/b DEO2 #0f62 .System/b DEO2
( vectors ) ( | vectors )
;on-frame .Screen/vector DEO2 ;on-frame .Screen/vector DEO2
;on-button .Controller/vector DEO2 ;on-button .Controller/vector DEO2
( resize ) ( | resize )
#00c8 .Screen/width DEO2 #00c8 .Screen/width DEO2
#0100 .Screen/height DEO2 #0100 .Screen/height DEO2
( set arena ) ( | set arena )
.Screen/width DEI2 #03 SFT2 NIP .arena/w STZ .Screen/width DEI2 #03 SFT2 NIP .arena/w STZ
.Screen/height DEI2 #03 SFT2 NIP .arena/h STZ .Screen/height DEI2 #03 SFT2 NIP .arena/h STZ
;reset JSR2 reset
BRK BRK
@ -53,11 +47,11 @@ BRK
#06 ( difficulty - lower value produces faster gameplay ) #06 ( difficulty - lower value produces faster gameplay )
EQU JMP BRK EQU JMP BRK
( clear ) #00 ;draw-snake JSR2 ( clear ) #00 draw-snake
( update ) ;move JSR2 ( update ) move
( draw ) #02 .snake/dead LDZ ADD ;draw-snake JSR2 ( draw ) #02 .snake/dead LDZ ADD draw-snake
#83 ;draw-apple JSR2 #83 draw-apple
( score ) .snake/length LDZ #41 ;draw-score JSR2 ( score ) .snake/length LDZ #41 draw-score
( reset ) #00 .arena/timer STZ ( reset ) #00 .arena/timer STZ
BRK BRK
@ -65,63 +59,58 @@ BRK
@on-button ( -> ) @on-button ( -> )
.Controller/button DEI .Controller/button DEI
DUP #08 NEQ ,&no-escape JCN DUP #08 NEQ ?{
;reset JSR2 reset }
&no-escape #04 SFT DUP #00 EQU ?{
#04 SFT DUP #00 EQU ,&skip JCN
DUP .snake/direction STZ DUP .snake/direction STZ
&skip }
POP POP
BRK BRK
@reset ( -- ) @reset ( -- )
#00 ;draw-snake JSR2 #00 draw-snake
#00 ;draw-apple JSR2 #00 draw-apple
.arena/w LDZ #01 SFT #01 SUB .snake/x STZ .arena/w LDZ #01 SFT #01 SUB .snake/x STZ
.arena/h LDZ #01 SFT #01 SUB .snake/y STZ .arena/h LDZ #01 SFT #01 SUB .snake/y STZ
#00 .snake/dead STZ #00 .snake/dead STZ
#00 .snake/length STZ #00 .snake/length STZ
#00 .snake/direction STZ #00 .snake/direction STZ
#03 draw-snake
#03 ;draw-snake JSR2 !add-apple
;add-apple JSR2
JMP2r
@move ( -- ) @move ( -- )
( tail ) ( tail )
.snake/x LDZ2 STH2 .snake/x LDZ2 STH2
.snake/length LDZ #00 .snake/length LDZ #00
&loop &>loop ( -- )
( pop ) DUPk ADD .snake/tail ADD LDZ2 STH2 SWP2r ( pop ) DUPk ADD .snake/tail ADD LDZ2 STH2 SWP2r
( push ) DUPk ADD .snake/tail ADD STH2r ROT STZ2 ( push ) DUPk ADD .snake/tail ADD STH2r ROT STZ2
INC GTHk ,&loop JCN INC GTHk ?&>loop
POP2 POP2
POP2r POP2r
.snake/dead LDZ #00 EQU JMP JMP2r .snake/dead LDZ #00 EQU JMP JMP2r
.snake/direction LDZ .snake/direction LDZ
DUP #01 NEQ ,&no-up JCN DUP #01 NEQ ?&no-up
.snake/y LDZ #01 SUB .snake/y LDZ #01 SUB
.arena/h LDZ LTHk JMP SWP POP .arena/h LDZ LTHk JMP SWP POP
.snake/y STZ .snake/y STZ
&no-up &no-up
DUP #02 NEQ ,&no-down JCN DUP #02 NEQ ?&no-down
.snake/y LDZ INC .snake/y LDZ INC
.arena/h LDZ DIVk MUL SUB .arena/h LDZ DIVk MUL SUB
.snake/y STZ .snake/y STZ
&no-down &no-down
DUP #04 NEQ ,&no-left JCN DUP #04 NEQ ?&no-left
.snake/x LDZ #01 SUB .snake/x LDZ #01 SUB
.arena/w LDZ LTHk JMP SWP POP .arena/w LDZ LTHk JMP SWP POP
.snake/x STZ .snake/x STZ
&no-left &no-left
DUP #08 NEQ ,&no-right JCN DUP #08 NEQ ?&no-right
.snake/x LDZ INC .snake/x LDZ INC
.arena/w LDZ DIVk MUL SUB .arena/w LDZ DIVk MUL SUB
.snake/x STZ .snake/x STZ
@ -129,21 +118,21 @@ JMP2r
POP POP
( detect collision with apple ) ( detect collision with apple )
.snake/x LDZ2 .apple/x LDZ2 NEQ2 ,&no-collision-apple JCN .snake/x LDZ2 .apple/x LDZ2 NEQ2 ?{
#00 ;draw-apple JSR2 #00 draw-apple
.snake/length LDZ INC .snake/length STZ .snake/length LDZ INC .snake/length STZ
;add-apple JSR2 add-apple
;move JSR2 move
&no-collision-apple }
.snake/length LDZ #01 .snake/length LDZ #01
&loop-body &loop-body ( -- )
( pop ) DUPk ADD .snake/tail ADD LDZ2 ( pop ) DUPk ADD .snake/tail ADD LDZ2
.snake/x LDZ2 NEQ2 ,&no-collision-body JCN .snake/x LDZ2 NEQ2 ?{
#01 .snake/dead STZ #01 .snake/dead STZ
#03 ;draw-snake JSR2 #03 draw-snake
&no-collision-body }
INC GTHk ,&loop-body JCN INC GTHk ?&loop-body
POP2 POP2
JMP2r JMP2r
@ -163,11 +152,11 @@ JMP2r
( draw tail ) ( draw tail )
;snake-icns .Screen/addr DEO2 ;snake-icns .Screen/addr DEO2
.snake/length LDZ #00 .snake/length LDZ #00
&loop &>loop ( -- )
DUPk ADD .snake/tail ADD LDZ #0005 SFT2 .Screen/x DEO2 DUPk ADD .snake/tail ADD LDZ #0005 SFT2 .Screen/x DEO2
DUPk ADD .snake/tail ADD INC LDZ #0005 SFT2 .Screen/y DEO2 DUPk ADD .snake/tail ADD INC LDZ #0005 SFT2 .Screen/y DEO2
STHkr .Screen/sprite DEO STHkr .Screen/sprite DEO
INC GTHk ,&loop JCN INC GTHk ?&>loop
POP2 POP2
( draw head ) ( draw head )
.snake/x LDZ #0005 SFT2 .Screen/x DEO2 .snake/x LDZ #0005 SFT2 .Screen/x DEO2
@ -202,14 +191,14 @@ JMP2r
( assets ) ( assets )
@snake-icns @snake-icns [
7eff ffff ffff ff7e 7eff ffff ffff ff7e ]
&face &face [
7eff ffdb ffe7 ff7e 7eff ffdb ffe7 ff7e ]
@apple-chr @apple-chr [
0000 76ff ffff 7e3c 0000 76ff ffff 7e3c
1008 0000 0000 0000 1008 0000 0000 0000 ]
@font-hex ( 0-F ) @font-hex [
007c 8282 8282 827c 0030 1010 1010 1010 007c 8282 8282 827c 0030 1010 1010 1010
007c 8202 7c80 80fe 007c 8202 1c02 827c 007c 8202 7c80 80fe 007c 8202 1c02 827c
000c 1424 4484 fe04 00fe 8080 7c02 827c 000c 1424 4484 fe04 00fe 8080 7c02 827c
@ -217,5 +206,5 @@ JMP2r
007c 8282 7c82 827c 007c 8282 7e02 827c 007c 8282 7c82 827c 007c 8282 7e02 827c
007c 8202 7e82 827e 00fc 8282 fc82 82fc 007c 8202 7e82 827e 00fc 8282 fc82 82fc
007c 8280 8080 827c 00fc 8282 8282 82fc 007c 8280 8080 827c 00fc 8282 8282 82fc
007c 8280 f080 827c 007c 8280 f080 8080 007c 8280 f080 827c 007c 8280 f080 8080 ]