; SPDX-License-Identifier: MIT ; Bank 0 (continued): ball physics, collisions, scoring, turns, sound, and the ; values the kernels read. ; --------------------------------------------------------------------------- Physics: lda ballY sta t2 ; old y ; x clc lda ballXL adc vxL sta ballXL lda ballX adc vxH sta ballX cmp #200 bcs @left ; wrapped below 0 cmp #XMIN bcc @left cmp #XMAX+1 bcc @y lda #<(2*XMAX) ; mirror at the right wall sec sbc ballX sta ballX jmp @wall @left: lda #2*XMIN sec sbc ballX sta ballX @wall: jsr NegVX ldx #SFX_WALL jsr Sfx0 @y: clc lda ballYL adc vyL sta ballYL lda ballY adc vyH sta ballY bit vyH bpl @down jmp @up @down: ; moving down cmp #PLAYH bcc @bat lda floor bne @dropFloor jmp BallLost @dropFloor: dec floor lda ballY sec sbc #PLAYH sta ballY lda #0 sta cool ldx #SFX_DOWN jsr Sfx1 jmp Collide @bat: ; the top floor has no bat: the ball falls jsr FloorOpen bne @nobat ; the ball's bottom line crossed the bat's top this frame? lda t2 clc adc #BH-1 cmp #BATTOP bcs @nobat lda ballY clc adc #BH-1 cmp #BATTOP bcc @nobat lda ballX clc adc #2 sec sbc batX ; offset 0..batW+1 when touching bcc @nobat ldx batW inx stx t0 cmp t0 bcc @hit beq @hit @nobat: jmp Collide @hit: tax lda batW cmp #16 beq @z16 lda Zone24,x jmp @zone @z16: lda Zone16,x @zone: ; zone 0-3 left (class 3-zone), 4-7 right (class zone-4) ldx #$80 cmp #4 bcs @r eor #3 jmp @cls @r: sbc #4 ldx #0 @cls: sta t3 ; class stx t1 ; direction (up) lda #BATTOP-BH sta ballY lda kick cmp #KICKWIN bcc @plain ; a kick: faster and steeper lda speed clc adc #2 cmp #8 bcc @sp lda #7 @sp: sta speed lda t3 beq @k0 dec t3 @k0: lda #0 sta kick ; one kick per press ldx #SFX_KICK jsr Sfx0 jmp @vel @plain: jsr BaseSpeed cmp speed bcs @nodecay dec speed @nodecay: ldx #SFX_BAT jsr Sfx0 @vel: lda t3 jsr SetVelocity jmp Collide @up: lda floor cmp #2 bne @notTop lda ballY cmp #CEIL bcs @cl lda #2*CEIL ; the roof sec sbc ballY sta ballY jsr NegVY ldx #SFX_WALL jsr Sfx0 @cl: jmp Collide @notTop: lda ballY cmp #200 bcc Collide inc floor ; up through the top: the floor above clc adc #PLAYH sta ballY lda #0 sta cool ldx #SFX_UP jsr Sfx1 ; fall through Collide: ; leading line: top line moving up, bottom line moving down lda ballY bit vyH bmi @u clc adc #BH-1 @u: sta t2 ; leading line lda ballX clc adc #1 sta t3 ; centre column lda floor beq HitBricks cmp #1 bne @t jmp HitMiddle @t: jmp HitBalloons ; --- bottom floor: rows on lines 11+8r .. 16+8r --- HitBricks: lda cool ; set only by the Big Top refill on this floor bne @none lda t2 sec sbc #9 cmp #48 bcs @none tax and #7 cmp #2 bcc @none ; gap lines txa lsr lsr lsr tay ; row lda t3 sec sbc #16 lsr lsr lsr lsr tax ; column lda BitTab,x and bricks,y beq @none eor bricks,y sta bricks,y jsr NegVY lda RowPoints,y ldx #0 jsr AddScore ldx SfxBrick,y jsr Sfx0 ldx #5 @all: lda bricks,x bne @none dex bpl @all lda #0 jmp FloorCleared @none: rts RowPoints: .byte $60, $50, $40, $30, $20, $10 SfxBrick: .byte SFX_BRICK0, SFX_BRICK0+1, SFX_BRICK0+2, SFX_BRICK0+3, SFX_BRICK0+4, SFX_BRICK0+5 ; --- middle floor --- HitMiddle: ; drop targets: brick lines 45-48 lda t2 cmp #45 bcc @nodrop cmp #49 bcs @nodrop lda t3 sec sbc #16 lsr lsr lsr lsr tax lda BitTab,x and drops beq @nodrop eor drops sta drops jsr NegVY lda #0 ldx #$01 ; 100 jsr AddScore ldx #SFX_DROP jsr Sfx0 lda drops bne @r lda dropT ; still coming back (MoveDrops): not a clear bne @r lda #1 jmp FloorCleared @nodrop: lda cool bne @r ; the clown: lines 2-17, x clownX .. clownX+15. Hit from below only ; (2026-11-21, Aubrey's playtest): a ball falling in from the top floor ; enters right where the clown walks, and bouncing it straight back up ; was too fast to follow; now it passes in front of the clown. A ball ; moving down on lines 2-17 can only have come from above (the middle ; floor has no roof) or off the clown itself (then `cool` is set). lda clownHit bne @noclown bit vyH bpl @noclown lda t2 cmp #2 bcc @noclown cmp #18 bcs @noclown lda t3 sec sbc clownX cmp #16 bcs @noclown jsr NegVY lda #60 sta clownHit lda #10 sta cool lda #0 ldx #$05 ; 500 jsr AddScore ldx #SFX_CLOWN jmp Sfx0 @noclown: ; bumpers: row A centred on line 28 (x 48, 80, 112), row B on 94 (48, 112) lda ballY clc adc #2 sta t2 ; ball centre line sec sbc #28 jsr Abs cmp #7 bcs @rowB lda #28 sta t0 ldx #2 jmp @bx @rowB: lda t2 sec sbc #94 jsr Abs cmp #7 bcs @r lda #94 sta t0 ldx #1 ; B: entries 0 and 1 of BumpXB @bxB: lda t3 sec sbc BumpXB,x jsr AbsS cmp #6 bcc @bump dex bpl @bxB @r: rts @bx: lda t3 sec sbc BumpXA,x jsr AbsS cmp #6 bcc @bump dex bpl @bx rts @bump: ; A = |dx| (0-5), t1 D7 = left of the bumper (from AbsS) lsr cmp #3 bcc @cl lda #3 @cl: sta t3 ; class from |dx| lda t2 cmp t0 bcc @upw ; above the centre: bounce up lda t1 ora #$40 sta t1 @upw: lda speed ; a speed step, up to BUMP_MAX: only kicks make a cmp #BUMP_MAX ; ball strong enough to punch (2026-11-23; it was 7, bcs @fast ; and the bot's ball sat at the cap) inc speed @fast: lda #10 sta cool lda #8 sta flash lda t3 jsr SetVelocity lda #$20 ldx #0 jsr AddScore ldx #SFX_BUMP jmp Sfx0 BumpXA: .byte 48, 80, 112 BumpXB: .byte 48, 112 ; Abs: |A| of a signed byte. AbsS: the same, and t1 D7 = sign (1 = negative). Abs: bpl @p eor #$FF clc adc #1 @p: rts AbsS: pha and #$80 sta t1 pla jmp Abs ; --- top floor: rows' balloon bodies on lines 14+19r .. 25+19r --- HitBalloons: lda cool ; set only by the Big Top refill on this floor bne @none lda t2 sec sbc #14 bcc @none ldy #0 @rw: cmp #12 bcc @inrow sec sbc #19 bcc @none iny cpy #3 bcc @rw @none: rts @inrow: lda t3 sec sbc rowX,y bcc @none cmp #96 bcs @none tax and #15 cmp #9 bcs @none txa lsr lsr lsr lsr tax ; balloon 0-5 lda BalBit,x and balloons,y beq @none eor balloons,y ; pop sta balloons,y ; Speed as power (2026-11-23, Aubrey's idea; provisional): from PUNCH_SPEED ; up the ball punches through instead of bouncing, so it can pop several in ; a line; every pop spends one speed step (not below the level's base), so ; a fast ball burns itself down. The speed cap (7, the velocity table's ; last step) stays. lda speed cmp #PUNCH_SPEED bcs @punch jsr NegVY ; a plain pop bounces @punch: jsr SpeedDown lda #$50 ldx #0 jsr AddScore ldx #SFX_POP jsr Sfx0 lda balloons ora balloons+1 ora balloons+2 bne @none lda #2 jmp FloorCleared BalBit: .byte 1, 8, 2, 16, 4, 32 ; SpeedDown: one speed step less, not below the level's base speed, keeping the ; ball's direction and angle class. Every velocity comes from the table (walls only ; change signs), so |vx| matches exactly one class at the current speed. Uses t1, ; tmp, row; changes X and Y. SpeedDown: jsr BaseSpeed cmp speed bcs @r ; at the base already lda #0 sta t1 lda vxL ; |vx| into tmp (low), row (high); t1 D7 = left sta tmp lda vxH sta row bpl @px lda #$80 sta t1 sec lda #0 sbc tmp sta tmp lda #0 sbc row sta row @px: bit vyH ; t1 D6 = down bmi @cls lda t1 ora #$40 sta t1 @cls: lda speed asl asl tax ; speed * 4: this speed's four classes ldy #0 @c: lda VXLO,x cmp tmp bne @n lda VXHI,x cmp row beq @f @n: inx iny cpy #4 bcc @c ldy #1 ; (no match: cannot happen; class 1) @f: dec speed tya jmp SetVelocity @r: rts ; --------------------------------------------------------------------------- ; The top floor has no bat (2026-11-21, Aubrey's playtest; kept after his second ; playtest): each visit is one burst among the balloons, then the ball falls back. ; (A top bat until its balloons were gone made Big Top ten times rarer under the ; bot; an open net on the cleared middle floor was dropped on 2026-11-22 at ; Aubrey's request: the middle floor keeps its bat and its targets come back.) ; FloorOpen: A nonzero (Z clear) on the top floor. FloorOpen: OpenTopOnly: ; (the harness reads this label to know the rule) lda floor and #2 rts ; FloorCleared: A = floor. Every clear pays 1000 (times the multiplier). ; - Bricks (0): stay cleared until Big Top. ; - Drop targets (1): the clear is remembered (cleared bit 1, its HUD marker ; widens) until Big Top, and the targets come back (MoveDrops), so they keep ; scoring; clearing them again pays again. ; - Balloons (2): they drift back (MoveBalloons); the clear raises this ball's ; multiplier one step after its own 1000 is paid (2026-11-22, Aubrey's idea: ; "a small bonus for future scores for that particular ball"; provisional). ; Big Top when the bricks are cleared and the drop targets have been cleared at ; least once this level: 5000 (times the multiplier), and every floor comes back ; harder (more brick rows, faster balloons and clown, a faster base speed). MULT_MAX = 5 ; the multiplier's cap (multK = (multiplier - 1) * 6) FloorCleared: cmp #2 bne @lower lda #0 ldx #$10 ; 1000 jsr AddScore lda multK ; then one step up, to MULT_MAX cmp #(MULT_MAX-1)*6 bcs @clear adc #6 sta multK jmp @clear @lower: tax lda BitTab,x ora cleared sta cleared lda #0 ldx #$10 ; 1000 jsr AddScore lda cleared and #3 ; (the floor clears; keeps Big Top to bits 0-1) cmp #3 beq @bigtop @clear: ldx #SFX_CLEAR jsr Sfx1 lda #MSG_CLEAR jmp ShowMsg @bigtop: lda #0 sta cleared ldx #$50 ; 5000 jsr AddScore inc level jsr FillBricks jsr ResetBalloons lda #$FF sta drops lda #0 sta dropT lda #20 ; no collisions while the ball leaves the refilled floor sta cool lda balls cmp #9 bcs @nx inc balls @nx: ldx #SFX_FANFARE jsr Sfx1 lda #MSG_BIGTOP jmp ShowMsg ; AddScore: A = BCD for the last two digits, X = BCD for the middle two; added ; once per multiplier step (multK = (multiplier - 1) * 6). Keeps Y. AddScore: sta tmp stx row tya pha ldy multK @rep: sed clc lda tmp adc score+2 sta score+2 lda row adc score+1 sta score+1 lda #0 adc score+0 sta score+0 cld tya sec sbc #6 tay bcs @rep pla tay rts ; --------------------------------------------------------------------------- BallLost: lda #ST_LOST sta gameState lda #90 sta stateTimer lda #200 sta ballY ; off every line lda #0 sta floor ldx #SFX_LOST jmp Sfx1 ; After a lost ball: one fewer; the other player's turn in a two-player game ; while they have balls; game over when nobody has. NextTurn: dec balls lda twoPlayer beq @solo lda stash+(balls-pStart) beq @solo ; the other player is out: keep going alone ldx #PBLK-1 ; swap the player blocks @sw: lda pStart,x ldy stash,x sta stash,x sty pStart,x dex bpl @sw lda curPlayer eor #1 sta curPlayer jsr ServeBall lda curPlayer beq @m1 lda #MSG_P2 jmp ShowMsg @m1: lda #MSG_P1 jmp ShowMsg @solo: lda balls beq @end jmp ServeBall @end: lda #ST_OVER sta gameState lda #180 sta stateTimer ldx #SFX_OVER jsr Sfx1 lda #MSG_OVER sta msgIdx lda #180 sta msgT rts ; --------------------------------------------------------------------------- ; Sound. Each effect is a list of steps: (AUDC << 4 | AUDV), AUDF, frames; ; a 0 ends it. Channel 0 plays the play sounds, channel 1 the longer ones. ; All sounds here are original to this game. Sfx0: lda SfxLo,x sta sfxPtr0 lda SfxHi,x sta sfxPtr0+1 lda #0 sta sfxCnt0 rts Sfx1: lda SfxLo,x sta sfxPtr1 lda SfxHi,x sta sfxPtr1+1 lda #0 sta sfxCnt1 rts Sound: ldx #0 jsr @chan ldx #3 ; channel 1 state is 3 bytes on @chan: lda sfxPtr0+1,x beq @r lda sfxCnt0,x beq @next dec sfxCnt0,x @r: rts @next: lda (sfxPtr0,x) ; first byte of the step beq @stop sta t0 lda ChanOf,x tay lda t0 lsr lsr lsr lsr sta AUDC0,y lda t0 and #15 sta AUDV0,y jsr @adv lda (sfxPtr0,x) sta AUDF0,y jsr @adv lda (sfxPtr0,x) sta sfxCnt0,x dec sfxCnt0,x jmp @adv @stop: lda #0 sta sfxPtr0+1,x lda ChanOf,x tay lda #0 sta AUDV0,y rts @adv: inc sfxPtr0,x bne @a inc sfxPtr0+1,x @a: rts ChanOf: .byte 0, 0, 0, 1 .include "sounds.s" ; --------------------------------------------------------------------------- ; Prep: everything the kernels read, from the state after this frame's logic. Prep: lda gameState bne @play lda #1 sta kernelMode jmp PrepScore @play: lda #0 sta kernelMode ; the ball lda #PLAYH-BH+1 sec sbc ballY sta ballK clc adc #2 sta ballK2 ; (ballK + 1 comes from a borrow: BALLAT1) lda ballX sec sbc #low::BatGfx sta batPtr+1 stx batCol lda gameState cmp #ST_SERVE beq @showbat cmp #ST_PLAY bne @hidebat jsr FloorOpen ; no bat on the top floor beq @showbat @hidebat: lda #<(low::BatGfx+low::BatBlank) ; no bat while a ball is lost or at the end sta batPtr @showbat: ; status: reserve balls and the floor markers ldx balls dex txa bpl @p lda #0 @p: cmp #5 bcc @p4 lda #4 @p4: tax lda Pips,x sta pipsPF ; floor markers: wide for the bricks cleared, the drop targets cleared ; this level, and the top floor while all its balloons are popped lda balloons ora balloons+1 ora balloons+2 cmp #1 ; carry clear: no balloons up lda cleared and #3 ; (and keeps the carry) bcs @bal ora #4 @bal: sta t0 ldx #2 @fl: lda BitTab,x and t0 beq @nc lda #$3C bne @st @nc: lda #$18 @st: cpx floor bne @nf lda #$FF @nf: sta floorPF,x dex bpl @fl ; middle floor lda clownX sec sbc #low::Font ldx #11 @hi: sta dp,x dex dex bpl @hi lda gameState beq @digits ; the title shows the last score lda msgT beq @digits ; a message ldx msgIdx ldy #0 @mc: lda Messages,x sta dp,y inx iny iny cpy #12 bcc @mc rts @digits: ; six BCD digits, leading zeros blank (the last digit always shown). ; On the title in a two-player game the two scores take turns. ldx #0 lda gameState bne @own lda twoPlayer beq @own lda frame bpl @own ldx #PBLK ; the stashed player's score (stash follows pEnd) lda curPlayer eor #1 beq @o1 lda #C_SCORE2 bne @o2 @o1: lda #C_SCORE1 @o2: sta scoreCol @own: txa clc adc #3 sta t1 ; end index ldy #0 lda #0 sta t0 ; 0 while still blanking @d: lda score,x lsr lsr lsr lsr jsr @put lda score,x and #15 cpy #10 bne @np inc t0 ; the last digit always shows @np: jsr @put inx cpx t1 bcc @d rts @put: cmp #0 bne @nz pha lda t0 bne @zero pla lda #CH_SP sta dp,y iny iny rts @zero: pla @nz: inc t0 asl asl asl sta dp,y iny iny rts PairA: .byte $00, $E0, $0E, $EE ; two bricks in PF1 (D7 first) PairB: .byte $00, $07, $70, $77 ; two bricks in PF2 (D0 first) Pips: .byte $00, $80, $A0, $A8, $AA