Added game reset

This commit is contained in:
Devine Lu Linvega 2021-11-02 14:04:26 -07:00
parent 1bf6eaeec0
commit a2f826121d
1 changed files with 93 additions and 51 deletions

View File

@ -10,10 +10,10 @@
%RTN { JMP2r } %RTN { JMP2r }
%TOS { #00 SWP } %TOS { #00 SWP }
%SWP? { #01 JCN SWP } %SWP? { #01 JCN SWP }
%RTN? { #01 JCN RTN }
%BRK? { #01 JCN BRK } %BRK? { #01 JCN BRK }
%DEBUG { ;print-hex JSR2 #0a .Console/write DEO } %DIFFICULTY { #06 }
%DEBUG2 { SWP ;print-hex JSR2 ;print-hex JSR2 #0a .Console/write DEO }
( devices ) ( devices )
@ -35,72 +35,89 @@
@apple @apple
&x $1 &y $1 &x $1 &y $1
@snake @snake
&direction $1 &length $1 &direction $1 &length $1 &dead $1
&x $1 &y $1 &x $1 &y $1
&tail &tail
|0100 ( -> ) |0100 ( -> )
( theme ) ( theme )
#0f2f .System/r DEO2 #0f7e .System/r DEO2
#0ff2 .System/g DEO2 #0fe6 .System/g DEO2
#0022 .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 )
#00c0 .Screen/width DEO2 #00c8 .Screen/width DEO2
#0100 .Screen/height DEO2 #0100 .Screen/height DEO2
( set arena )
.Screen/width DEI2 8// NIP .arena/w STZ .Screen/width DEI2 8// NIP .arena/w STZ
.Screen/height DEI2 8// NIP .arena/h STZ .Screen/height DEI2 8// NIP .arena/h STZ
( put snake ) ;reset JSR2
.arena/w LDZ 2/ #01 - .snake/x STZ
.arena/h LDZ 2/ #01 - .snake/y STZ
;add-apple JSR2
BRK BRK
@on-frame ( -> ) @on-frame ( -> )
.arena/timer LDZ INC DUP .arena/timer STZ .arena/timer LDZ INC DUP .arena/timer STZ
#06 = BRK? DIFFICULTY = BRK?
#00 ;draw-snake JSR2
;move JSR2
#02 ;draw-snake JSR2
( clear ) #00 ;draw-snake JSR2
( update ) ;move JSR2
( draw ) #02 .snake/dead LDZ + ;draw-snake JSR2
( score ) .snake/length LDZ #01 ;draw-score JSR2
( reset ) #00 .arena/timer STZ ( reset ) #00 .arena/timer STZ
BRK BRK
@on-button ( -> ) @on-button ( -> )
.Controller/button DEI #04 SFT .Controller/button DEI
DUP #00 = ,&skip JCN DUP #08 ! ,&no-escape JCN
;reset JSR2
&no-escape
#04 SFT DUP #00 = ,&skip JCN
DUP .snake/direction STZ DUP .snake/direction STZ
&skip &skip
POP POP
BRK BRK
@reset ( -- )
#00 ;draw-snake JSR2
#00 ;draw-apple JSR2
.arena/w LDZ 2/ #01 - .snake/x STZ
.arena/h LDZ 2/ #01 - .snake/y STZ
#00 .snake/dead STZ
#00 .snake/length STZ
#00 .snake/direction STZ
#03 ;draw-snake JSR2
;add-apple JSR2
RTN
@move ( -- ) @move ( -- )
( tail ) ( tail )
.snake/x LDZ2 STH2 .snake/x LDZ2 STH2
.snake/length LDZ #00 .snake/length LDZ #00
&loop &loop
DUP 2* .snake/tail + LDZ2 STH2 ( pop ) DUP 2* .snake/tail + LDZ2 STH2 SWP2r
SWP2r ( push ) DUP 2* .snake/tail + STH2r ROT STZ2
DUP 2* .snake/tail + STH2r ROT STZ2
INC GTHk ,&loop JCN INC GTHk ,&loop JCN
POP2 POP2
POP2r POP2r
.snake/dead LDZ #00 = RTN?
.snake/direction LDZ .snake/direction LDZ
DUP #01 ! ,&no-up JCN DUP #01 ! ,&no-up JCN
.snake/y LDZ #01 - .snake/y LDZ #01 -
@ -124,37 +141,42 @@ BRK
&no-right &no-right
POP POP
( detect collision ) ( detect collision with apple )
.snake/x LDZ .apple/x LDZ ! ,&no-collision JCN .snake/x LDZ2 .apple/x LDZ2 [ ROT = ROT ROT = #0101 !! ] ,&no-collision-apple JCN
.snake/y LDZ .apple/y LDZ ! ,&no-collision JCN
#00 ;draw-apple JSR2 #00 ;draw-apple JSR2
.snake/length LDZ INC DUP DEBUG .snake/length STZ .snake/length LDZ INC .snake/length STZ
;add-apple JSR2 ;add-apple JSR2
;move JSR2 ;move JSR2
&no-collision &no-collision-apple
.snake/length LDZ #01
&loop-body
( pop ) DUP 2* .snake/tail + LDZ2
.snake/x LDZ2 [ ROT = ROT ROT = #0101 !! ] ,&no-collision-body JCN
#01 .snake/dead STZ
#03 ;draw-snake JSR2
&no-collision-body
INC GTHk ,&loop-body JCN
POP2
RTN RTN
@add-apple ( -- ) @add-apple ( -- )
( seed )
.DateTime/hour DEI2 .DateTime/minute DEI2 MUL2 #1234 MUL2 + .DateTime/hour DEI2 .DateTime/minute DEI2 MUL2 #1234 MUL2 +
.arena/w LDZ MOD .apple/x STZ .arena/w LDZ MOD .apple/x STZ
.DateTime/hour DEI2 .DateTime/minute DEI2 MUL2 #abcd MUL2 + .DateTime/hour DEI2 .DateTime/minute DEI2 MUL2 #abcd MUL2 +
.arena/h LDZ MOD .apple/y STZ .arena/h LDZ MOD .apple/y STZ
#83 ;draw-apple JSR2
#03 ;draw-apple JSR2
RTN RTN
@draw-snake ( color -- ) @draw-snake ( color -- )
.snake/x LDZ TOS 8** .Screen/x DEO2 STH
.snake/y LDZ TOS 8** .Screen/y DEO2
;snake-icns .Screen/addr DEO2
STHk .Screen/sprite DEO
( draw tail ) ( draw tail )
;snake-icns .Screen/addr DEO2
.snake/length LDZ #00 .snake/length LDZ #00
&loop &loop
DUP 2* .snake/tail + LDZ TOS 8** .Screen/x DEO2 DUP 2* .snake/tail + LDZ TOS 8** .Screen/x DEO2
@ -162,33 +184,53 @@ RTN
STHkr .Screen/sprite DEO STHkr .Screen/sprite DEO
INC GTHk ,&loop JCN INC GTHk ,&loop JCN
POP2 POP2
POPr ( draw head )
.snake/x LDZ TOS 8** .Screen/x DEO2
.snake/y LDZ TOS 8** .Screen/y DEO2
;snake-icns/face .Screen/addr DEO2
STHr .Screen/sprite DEO
RTN RTN
@draw-apple ( color -- ) @draw-apple ( color -- )
.apple/x LDZ TOS 8** .Screen/x DEO2
.apple/y LDZ TOS 8** .Screen/y DEO2 .apple/x LDZ TOS 8** .Screen/x DEO2
;apple-icn .Screen/addr DEO2 .apple/y LDZ TOS 8** .Screen/y DEO2
.Screen/sprite DEO ;apple-chr .Screen/addr DEO2
RTN .Screen/sprite DEO
RTN RTN
@print-hex ( value -- ) @draw-score ( score color -- )
STHk #04 SFT ,&parse JSR .Console/write DEO STH
STHr #0f AND ,&parse JSR .Console/write DEO #0010 .Screen/x DEO2
RTN #0010 .Screen/y DEO2
&parse ( value -- char ) DUP #04 SFT TOS 8** ;font-hex ++ .Screen/addr DEO2
DUP #09 GTH ,&above JCN #30 ADD RTN &above #09 SUB #60 ADD RTN .Screen/x DEI2 #0008 ++ .Screen/x DEO2
( draw ) STHkr .Screen/sprite DEO
#0f AND TOS 8** ;font-hex ++ .Screen/addr DEO2
.Screen/x DEI2 #0008 ++ .Screen/x DEO2
( draw ) STHr .Screen/sprite DEO
RTN RTN
( assets ) ( assets )
@snake-icns @snake-icns
ffff ffff ffff ffff 7eff ffff ffff ff7e
@apple-icn &face
ffff ffff ffff ffff 7eff ffdb ffe7 ff7e
@apple-chr
0000 76ff ffff 7e3c
1008 0000 0000 0000
@font-hex ( 0-F )
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