The final part of Day 6!
https://compudanzas.net/uxn_tutorial_day_6.html
This part goes over the ball handling and collision detection. This is the most complex part of the program and one where I realized I probably need to start abusing macros as I forget to type certain things like .Screen/width
needing DEI2
to actually be read in. This could instead be a macro or even a variable as I don't usually forget a LDZ2
when loading a variable.
Now I've finished up the game of pong and it was definitely a good time. The tutorial got better in the later days as we were doing stuff that I'm used to.
I think a good project could be to make a gathering game for practice and then evolve that into snake. Brick breaker might also be good or even space invaders. Lot's of things open up now. I'll write my ideas and thoughts as a separate post after I finish the last day.
The final code for day 6:
( hello-pong.tal )
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|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 ]
( macros )
%RTN { JMP2r }
%PADDLE-WIDTH { #0010 }
%PADDLE-HEIGHT { #0018 }
%PADDLE-SPEED { #0003 }
%PADDLE-COLOR { #c5 }
%CLEAR-COLOR { #40 }
%MARGIN { #0010 }
%HALF2 { #01 SFT2 }
%BALL-SIZE { #0010 }
%BALL-COLOR { #c5 }
%BALL-POSITIVE { #0002 }
%BALL-NEGATIVE { #fffe }
( main program )
|0000
@left [ &x $2 &y $2 ]
@right [ &x $2 &y $2 ]
@ball [ &x $2 &y $2 &speed-x $2 &speed-y $2 ]
|0100
@setup
#2ce9 .System/r DEO2
#01c0 .System/g DEO2
#2ce5 .System/b DEO2
;on-frame .Screen/vector DEO2
;draw-background JSR2
MARGIN .left/x STZ2
.Screen/width DEI2
MARGIN SUB2 PADDLE-WIDTH SUB2
.right/x STZ2
.Screen/height DEI2 PADDLE-HEIGHT SUB2 HALF2 DUP2
.left/y STZ2
.right/y STZ2
.Screen/width DEI2 BALL-SIZE SUB2
HALF2
.ball/x STZ2
.Screen/height DEI2 BALL-SIZE SUB2
HALF2
.ball/y STZ2
BALL-POSITIVE .ball/speed-x STZ2
BALL-POSITIVE .ball/speed-y STZ2
BALL-COLOR ;draw-ball JSR2
BRK
@draw-background
;tile-background .Screen/addr DEO2
.Screen/height DEI2 #0010 SUB2
#0010
&loop-y
DUP2 .Screen/y DEO2
.Screen/width DEI2
#0000
&loop-x
DUP2 .Screen/x DEO2
#03 .Screen/sprite DEO
#0008 ADD2
GTH2k ,&loop-x JCN
POP2 POP2
#0008 ADD2
GTH2k ,&loop-y JCN
POP POP
RTN
@draw-paddle ( x^ y^ color -- )
STH
.Screen/y DEO2
.Screen/x DEO2
;paddle .Screen/addr DEO2
#16 .Screen/auto DEO
STHr
.Screen/sprite DEOk DEOk
DEO
RTN
@update-paddles
&left
.Controller/button DEI
DUP #10 AND ,&left-up JCN
DUP #20 AND ,&left-down JCN
,&right JMP
&left-up
.left/y LDZ2 MARGIN EQU2 ,&end JCN
.left/y LDZ2 PADDLE-SPEED SUB2 .left/y STZ2
,&right JMP
&left-down
.left/y LDZ2
.Screen/height DEI2 PADDLE-HEIGHT SUB2
MARGIN SUB2
EQU2 ,&end JCN
.left/y LDZ2 PADDLE-SPEED ADD2 .left/y STZ2
,&right JMP
&right
DUP #01 AND ,&right-up JCN
DUP #02 AND ,&right-down JCN
,&end JMP
&right-up
.right/y LDZ2 MARGIN EQU2 ,&end JCN
.right/y LDZ2 PADDLE-SPEED SUB2 .right/y STZ2
,&end JMP
&right-down
.right/y LDZ2
.Screen/height DEI2 PADDLE-HEIGHT SUB2
MARGIN SUB2
EQU2 ,&end JCN
.right/y LDZ2 PADDLE-SPEED ADD2 .right/y STZ2
,&end JMP
&end
POP
RTN
@draw-ball ( color -- )
.ball/x LDZ2 .Screen/x DEO2
.ball/y LDZ2 .Screen/y DEO2
;ball-sprite .Screen/addr DEO2
#16 .Screen/auto DEO
.Screen/sprite DEOk DEO
RTN
@reset
.Screen/width DEI2 BALL-SIZE SUB2
HALF2 .ball/x STZ2
.Screen/height DEI2 BALL-SIZE SUB2
HALF2 .ball/y STZ2
RTN
@update-ball
.ball/speed-x LDZ2 .ball/x LDZ2
ADD2
.ball/x STZ2
.ball/speed-y LDZ2 .ball/y LDZ2
ADD2
.ball/y STZ2
&check-top-wall
.ball/y LDZ2
MARGIN
LTH2 ,&set-positive-speed JCN
,&check-bottom-wall JMP
&set-positive-speed
BALL-POSITIVE .ball/speed-y STZ2
,&continue JMP
&check-bottom-wall
.ball/y LDZ2 BALL-SIZE ADD2
.Screen/height DEI2 MARGIN SUB2
GTH2 ,&set-negative-speed JCN
,&continue JMP
&set-negative-speed
BALL-NEGATIVE .ball/speed-y STZ2
,&continue JMP
&continue
&check-left-paddle
.ball/x LDZ2
MARGIN PADDLE-WIDTH ADD2
LTH2 ,&x-in-left JCN
,&check-right-paddle JMP
&x-in-left
.ball/y LDZ2 DUP2
.left/y LDZ2 BALL-SIZE SUB2
GTH2 STH
.left/y LDZ2 PADDLE-HEIGHT ADD2
LTH2
STHr
AND
,&bounce-left JCN
.ball/x LDZ2 #0000 NEQ2 ,&finish JCN
&reset-left
;reset JSR2
,&finish JMP
&bounce-left
BALL-POSITIVE .ball/speed-x STZ2
,&finish JMP
&check-right-paddle
.ball/x LDZ2 BALL-SIZE ADD2
.Screen/width DEI2 MARGIN SUB2 PADDLE-WIDTH SUB2
GTH2 ,&x-in-right JCN
,&finish JMP
&x-in-right
.ball/y LDZ2 DUP2
.right/y LDZ2 BALL-SIZE SUB2
GTH2 STH
.right/y LDZ2 PADDLE-HEIGHT ADD2
LTH2
STHr
AND
,&bounce-right JCN
.ball/x LDZ2 .Screen/width DEI2 NEQ2 ,&finish JCN
&reset-right
;reset JSR2
,&finish JMP
&bounce-right
BALL-NEGATIVE .ball/speed-x STZ2
,&finish JMP
&finish
RTN
@on-frame
CLEAR-COLOR ;draw-ball JSR2
.left/x LDZ2 .left/y LDZ2 CLEAR-COLOR ;draw-paddle JSR2
.right/x LDZ2 .right/y LDZ2 CLEAR-COLOR ;draw-paddle JSR2
;update-paddles JSR2
;update-ball JSR2
.left/x LDZ2 .left/y LDZ2 PADDLE-COLOR ;draw-paddle JSR2
.right/x LDZ2 .right/y LDZ2 PADDLE-COLOR ;draw-paddle JSR2
BALL-COLOR ;draw-ball JSR2
BRK
@tile-background 1122 4488 1122 4488
@paddle
&tile0 [ 3f 7f e7 c3 c3 c3 c3 c3 00 00 18 3c 3c 3c 3c 3c ]
&tile1 [ fc fe ff ff ff ff ff ff 00 00 00 00 00 00 06 06 ]
&tile2 [ c3 c3 c3 c3 e7 ff ff ff 3c 3c 3c 3c 18 00 00 00 ]
&tile3 [ ff ff ff ff ff ff ff ff 06 06 06 06 06 06 06 06 ]
&tile4 [ ff ff ff ff ff ff 7f 3f 00 00 00 00 00 00 00 00 ]
&tile5 [ ff ff ff ff ff ff fe fc 06 06 06 06 06 1e 3c 00 ]
@ball-sprite
&tile0 [ 03 0f 1f 39 70 70 f9 ff 00 00 00 06 0f 0f 06 00 ]
&tile1 [ c0 f0 f8 fc fe fe ff ff 00 00 00 00 08 0c 06 06 ]
&tile2 [ ff ff 7f 7f 3f 1f 0f 03 00 00 00 00 18 0f 01 00 ]
&tile3 [ ff ff fe fe fc f8 f0 c0 06 06 0c 1c 38 f0 c0 00 ]