uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

snake.tal (4821B)


      1 ( uxnasm projects/examples/demos/snake.tal bin/snake.rom && uxnemu bin/snake.rom )
      2 
      3 ( devices )
      4 
      5 |00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g      $2 &b    $2 &debug  $1 &halt $1 ]
      6 |10 @Console    [ &vector $2 &read     $1 &pad    $5 &write $1 &error  $1 ]
      7 |20 @Screen     [ &vector $2 &width    $2 &height $2 &pad   $2 &x      $2 &y      $2 &addr $2 &pixel  $1 &sprite $1 ]
      8 |30 @Audio0     [ &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
      9 |80 @Controller [ &vector $2 &button   $1 &key    $1 ]
     10 |c0 @DateTime   [ &year   $2 &month    $1 &day    $1 &hour  $1 &minute $1 &second $1 &dotw $1 &doty   $2 &isdst $1 ]
     11 
     12 ( variables )
     13 
     14 |0000
     15 
     16 ( program )
     17 
     18 @arena
     19 	&w $1 &h $1 &timer $1
     20 @apple
     21 	&x $1 &y $1
     22 @snake
     23 	&direction $1 &length $1 &dead $1
     24 	&x $1 &y $1
     25 	&tail
     26 
     27 |0100 ( -> )
     28 
     29 	( theme ) 
     30 	#0f7e .System/r DEO2 
     31 	#0fe6 .System/g DEO2 
     32 	#0f62 .System/b DEO2
     33 
     34 	( vectors )
     35 	;on-frame .Screen/vector DEO2
     36 	;on-button .Controller/vector DEO2
     37 
     38 	( resize )
     39 	#00c8 .Screen/width DEO2
     40 	#0100 .Screen/height DEO2
     41 
     42 	( set arena )
     43 	.Screen/width DEI2 #03 SFT2 NIP .arena/w STZ
     44 	.Screen/height DEI2 #03 SFT2 NIP .arena/h STZ
     45 
     46 	;reset JSR2
     47 
     48 BRK
     49 
     50 @on-frame ( -> )
     51 
     52 	.arena/timer LDZ INC DUP .arena/timer STZ
     53 		#06 ( difficulty - lower value produces faster gameplay )
     54 		EQU JMP BRK
     55 
     56 	( clear ) #00 ;draw-snake JSR2
     57 	( update ) ;move JSR2
     58 	( draw ) #02 .snake/dead LDZ ADD ;draw-snake JSR2
     59 		#83 ;draw-apple JSR2
     60 	( score ) .snake/length LDZ #41 ;draw-score JSR2
     61 	( reset ) #00 .arena/timer STZ
     62 
     63 BRK
     64 
     65 @on-button ( -> )
     66 
     67 	.Controller/button DEI 
     68 	DUP #08 NEQ ,&no-escape JCN
     69 		;reset JSR2
     70 		&no-escape
     71 	#04 SFT DUP #00 EQU ,&skip JCN
     72 		DUP .snake/direction STZ
     73 		&skip
     74 	POP
     75 
     76 BRK
     77 
     78 @reset ( -- )
     79 
     80 	#00 ;draw-snake JSR2
     81 	#00 ;draw-apple JSR2
     82 
     83 	.arena/w LDZ #01 SFT #01 SUB .snake/x STZ
     84 	.arena/h LDZ #01 SFT #01 SUB .snake/y STZ
     85 	#00 .snake/dead STZ
     86 	#00 .snake/length STZ
     87 	#00 .snake/direction STZ
     88 
     89 	#03 ;draw-snake JSR2
     90 	;add-apple JSR2
     91 
     92 JMP2r
     93 
     94 @move ( -- )
     95 
     96 	( tail )
     97 	.snake/x LDZ2 STH2
     98 	.snake/length LDZ #00
     99 	&loop
    100 		( pop ) DUPk ADD .snake/tail ADD LDZ2 STH2 SWP2r
    101 		( push ) DUPk ADD .snake/tail ADD STH2r ROT STZ2
    102 		INC GTHk ,&loop JCN
    103 	POP2
    104 	POP2r
    105 
    106 	.snake/dead LDZ #00 EQU JMP JMP2r
    107 
    108 	.snake/direction LDZ
    109 	DUP #01 NEQ ,&no-up JCN
    110 		.snake/y LDZ #01 SUB
    111 			.arena/h LDZ LTHk JMP SWP POP
    112 			.snake/y STZ
    113 		&no-up
    114 	DUP #02 NEQ ,&no-down JCN
    115 		.snake/y LDZ INC
    116 			.arena/h LDZ DIVk MUL SUB
    117 			.snake/y STZ
    118 		&no-down
    119 	DUP #04 NEQ ,&no-left JCN
    120 		.snake/x LDZ #01 SUB
    121 			.arena/w LDZ LTHk JMP SWP POP
    122 			.snake/x STZ
    123 		&no-left
    124 	DUP #08 NEQ ,&no-right JCN
    125 		.snake/x LDZ INC 
    126 			.arena/w LDZ DIVk MUL SUB
    127 			.snake/x STZ
    128 		&no-right
    129 	POP
    130 
    131 	( detect collision with apple )
    132 	.snake/x LDZ2 .apple/x LDZ2 NEQ2 ,&no-collision-apple JCN
    133 		#00 ;draw-apple JSR2
    134 		.snake/length LDZ INC .snake/length STZ
    135 		;add-apple JSR2
    136 		;move JSR2
    137 		&no-collision-apple
    138 
    139 	.snake/length LDZ #01
    140 	&loop-body
    141 		( pop ) DUPk ADD .snake/tail ADD LDZ2
    142 		.snake/x LDZ2 NEQ2 ,&no-collision-body JCN
    143 			#01 .snake/dead STZ
    144 			#03 ;draw-snake JSR2
    145 			&no-collision-body
    146 		INC GTHk ,&loop-body JCN
    147 	POP2
    148 
    149 JMP2r
    150 
    151 @add-apple ( -- )
    152 
    153 	.DateTime/hour DEI2 .DateTime/minute DEI2 MUL2 #1234 MUL2 ADD
    154 		.arena/w LDZ DIVk MUL SUB .apple/x STZ
    155 	.DateTime/hour DEI2 .DateTime/minute DEI2 MUL2 #abcd MUL2 ADD
    156 		.arena/h LDZ DIVk MUL SUB .apple/y STZ
    157 
    158 JMP2r
    159 
    160 @draw-snake ( color -- )
    161 
    162 	STH
    163 	( draw tail )
    164 	;snake-icns .Screen/addr DEO2
    165 	.snake/length LDZ #00
    166 	&loop
    167 		DUPk ADD .snake/tail ADD LDZ #0005 SFT2 .Screen/x DEO2
    168 		DUPk ADD .snake/tail ADD INC LDZ #0005 SFT2 .Screen/y DEO2
    169 		STHkr .Screen/sprite DEO
    170 		INC GTHk ,&loop JCN
    171 	POP2
    172 	( draw head )
    173 	.snake/x LDZ #0005 SFT2 .Screen/x DEO2
    174 	.snake/y LDZ #0005 SFT2 .Screen/y DEO2
    175 	;snake-icns/face .Screen/addr DEO2
    176 	STHr .Screen/sprite DEO
    177 
    178 JMP2r
    179 
    180 @draw-apple ( color -- )
    181 
    182 	.apple/x LDZ #0005 SFT2 .Screen/x DEO2
    183 	.apple/y LDZ #0005 SFT2 .Screen/y DEO2
    184 	;apple-chr .Screen/addr DEO2
    185 	.Screen/sprite DEO
    186 
    187 JMP2r
    188 
    189 @draw-score ( score color -- )
    190 
    191 	STH
    192 	#0010 .Screen/x DEO2
    193 	#0010 .Screen/y DEO2
    194 	DUP #04 SFT #0005 SFT2 ;font-hex ADD2 .Screen/addr DEO2
    195 	.Screen/x DEI2 #0008 ADD2 .Screen/x DEO2
    196 		( draw ) STHkr .Screen/sprite DEO
    197 	#0f AND #0005 SFT2 ;font-hex ADD2 .Screen/addr DEO2
    198 	.Screen/x DEI2 #0008 ADD2 .Screen/x DEO2
    199 		( draw ) STHr .Screen/sprite DEO
    200 
    201 JMP2r
    202 
    203 ( assets )
    204 
    205 @snake-icns
    206 	7eff ffff ffff ff7e
    207 	&face
    208 	7eff ffdb ffe7 ff7e
    209 @apple-chr
    210 	0000 76ff ffff 7e3c
    211 	1008 0000 0000 0000
    212 @font-hex ( 0-F )
    213 	007c 8282 8282 827c 0030 1010 1010 1010
    214 	007c 8202 7c80 80fe 007c 8202 1c02 827c
    215 	000c 1424 4484 fe04 00fe 8080 7c02 827c
    216 	007c 8280 fc82 827c 007c 8202 1e02 0202
    217 	007c 8282 7c82 827c 007c 8282 7e02 827c
    218 	007c 8202 7e82 827e 00fc 8282 fc82 82fc
    219 	007c 8280 8080 827c 00fc 8282 8282 82fc
    220 	007c 8280 f080 827c 007c 8280 f080 8080 
    221