cheatsheets devlog projects search

uxn-day-6-2.md

Uxn - Day 6 - Part 2

2023-01-08 2

Day 6 continued.

https://compudanzas.net/uxn_tutorial_day_6.html

This part will focus on drawing the paddles and getting them moving. This was a good example of how to clear sprites, write them out using the auto byte and how to manage movement. This was a much more involved part than the first and I imagine the last part of the ball will also be much deeper than this part.

This was also the first time I’m making pong and its interesting to see how simple the steps are being broken down into. The idea sounds complex but once you break it into steps and write each self contained function everything becomes easier to manage. The magic of abstraction!

982d9679823816766e798155608f91ac

The final code as of part 2:

( 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 }

( main program )
|0000
    @left [ &x $2 &y $2 ]
    @right [ &x $2 &y $2 ]

|0100

@setup
    #2ce9 .System/r DEO2
    #01c0 .System/g DEO2
    #2ce5 .System/b DEO2

    ;on-frame .Screen/vector DEO2

    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

    ;draw-background 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

@on-frame
    .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

    .left/x LDZ2 .left/y LDZ2 PADDLE-COLOR ;draw-paddle JSR2
    .right/x LDZ2 .right/y LDZ2 PADDLE-COLOR ;draw-paddle 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 ]