(snake) Optimizations

This commit is contained in:
Devine Lu Linvega 2024-03-24 10:46:24 -07:00
parent 220983d694
commit c73f1e066a
1 changed files with 30 additions and 29 deletions

View File

@ -24,9 +24,9 @@
#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 .arena/w STZ
.Screen/height DEI2 #03 SFT2 NIP .arena/h STZ POP .Screen/height DEI2 #03 SFT2 .arena/h STZ
[ LIT2 01 -Screen/auto ] DEO POP [ LIT2 01 -Screen/auto ] DEO
reset BRK reset BRK
@on-frame ( -> ) @on-frame ( -> )
@ -35,7 +35,7 @@
[ LIT2 00 _&f ] STR [ LIT2 00 _&f ] STR
#00 <draw-snake> #00 <draw-snake>
<move> <move>
#02 .snake/dead LDZ ADD <draw-snake> [ LIT2 02 -snake/dead ] LDZ ADD <draw-snake>
#83 <draw-apple> #83 <draw-apple>
<draw-score> <draw-score>
BRK BRK
@ -71,30 +71,29 @@
SWP2r SWP2r
( push ) DUPk ADD .snake/tail ADD STH2r ROT STZ2 ( push ) DUPk ADD .snake/tail ADD STH2r ROT STZ2
INC GTHk ?&>loop INC GTHk ?&>loop
POP2 POP2r .snake/dead LDZ #00 EQU JMP POP2 POP2r
JMP2r ( ) .snake/dead LDZ #00 [ EQU JMP JMP2r ]
.snake/direction LDZ DUP #01 NEQ ?&no-up ( ) .snake/direction LDZ DUP #01 NEQ ?{
.snake/y LDZ #01 SUB .arena/h LDZ LTHk JMP .snake/y LDZ #01 SUB .arena/h LDZ [ LTHk JMP SWP POP ] .snake/y STZ }
SWP POP .snake/y STZ DUP #02 NEQ ?{
&no-up DUP #02 NEQ ?&no-down .snake/y LDZ INC .arena/h LDZ DIVk MUL SUB .snake/y STZ }
.snake/y LDZ INC .arena/h LDZ DIVk MUL SUB .snake/y STZ DUP #04 NEQ ?{
&no-down DUP #04 NEQ ?&no-left .snake/x LDZ #01 SUB .arena/w LDZ [ LTHk JMP SWP POP ] .snake/x STZ }
.snake/x LDZ #01 SUB .arena/w LDZ LTHk JMP DUP #08 NEQ ?{
SWP POP .snake/x STZ .snake/x LDZ INC .arena/w LDZ DIVk MUL SUB .snake/x STZ }
&no-left DUP #08 NEQ ?&no-right POP
.snake/x LDZ INC .arena/w LDZ DIVk MUL SUB .snake/x STZ ( | detect collision with apple )
&no-right POP .snake/x LDZ2 .apple/x LDZ2 NEQ2 ?{
( detect collision with apple ) .snake/x LDZ2 .apple/x LDZ2 NEQ2 ?{
#00 <draw-apple> #00 <draw-apple>
.snake/length LDZ INC .snake/length STZ .snake/length LDZ INC .snake/length STZ
<add-apple> <add-apple>
<move> } <move> }
.snake/length LDZ #01 .snake/length LDZ #01
&loop-body ( -- ) &>loop-body ( -- )
( pop ) DUPk ADD .snake/tail ADD LDZ2 .snake/x LDZ2 NEQ2 ?{ DUPk ADD .snake/tail ADD LDZ2 .snake/x LDZ2 NEQ2 ?{
#01 .snake/dead STZ [ LIT2 01 -snake/dead ] STZ
#03 <draw-snake> } #03 <draw-snake> }
INC GTHk ?&loop-body INC GTHk ?&>loop-body
POP2 JMP2r POP2 JMP2r
@<add-apple> ( -- ) @<add-apple> ( -- )
@ -110,24 +109,24 @@
STH STH
( | draw tail ) ( | draw tail )
;snake-icns .Screen/addr DEO2 ;snake-icns .Screen/addr DEO2
.snake/tail .snake/length LDZ DUP ADD OVR ADD SWP [ LIT2 -snake/tail -snake/length ] LDZ DUP ADD OVR ADD SWP
&>loop ( -- ) &>loop ( -- )
LDZk #0005 SFT2 .Screen/x DEO2 LDZk #0005 SFT2 .Screen/x DEO2
INCk LDZ #0005 SFT2 .Screen/y DEO2 INCk LDZ #0005 SFT2 .Screen/y DEO2
STHkr .Screen/sprite DEO DUPr [ LITr -Screen/sprite ] DEOr
INC INC GTHk ?&>loop INC 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
.snake/y LDZ #0005 SFT2 .Screen/y DEO2 .snake/y LDZ #0005 SFT2 .Screen/y DEO2
;snake-icns/face .Screen/addr DEO2 ;snake-icns/face .Screen/addr DEO2
STHr .Screen/sprite DEO [ LITr -Screen/sprite ] DEOr
JMP2r JMP2r
@<draw-apple> ( color -- ) @<draw-apple> ( color -- )
.apple/x LDZ #0005 SFT2 .Screen/x DEO2 .apple/x LDZ #0005 SFT2 .Screen/x DEO2
.apple/y LDZ #0005 SFT2 .Screen/y DEO2 .apple/y LDZ #0005 SFT2 .Screen/y DEO2
;apple-chr .Screen/addr DEO2 STHk ;apple-chr ;fill-icn [ STHr ?{ SWP2 } POP2 ] .Screen/addr DEO2
.Screen/sprite DEO .Screen/sprite DEO
JMP2r JMP2r
@ -136,10 +135,10 @@
.Screen/y DEO2 .Screen/y DEO2
.snake/length LDZ .snake/length LDZ
( x0 ) DUP #04 SFT <draw-num> ( x0 ) DUP #04 SFT <draw-num>
( 0x ) !<draw-num> ( >> )
@<draw-num> ( num -- ) @<draw-num> ( num -- )
#00 SWP #0f AND #30 SFT2 ;font-hex ADD2 .Screen/addr DEO2 #000f ROT AND #30 SFT2 ;font-hex ADD2 .Screen/addr DEO2
[ LIT2 41 -Screen/sprite ] DEO [ LIT2 41 -Screen/sprite ] DEO
JMP2r JMP2r
@ -152,6 +151,8 @@
@apple-chr [ @apple-chr [
0000 76ff ffff 7e3c 1008 0000 0000 0000 ] 0000 76ff ffff 7e3c 1008 0000 0000 0000 ]
@fill-icn [ ffff ffff ffff ffff ]
@font-hex [ @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