; =========================================================================== ; BUZZ RUN - a one-button flying game for the Commodore 64 ; Guide a bumblebee through the gaps in an endless row of hedge pillars. ; ; Controls: FIRE (joystick port 1 or 2) or SPACE to flap. ; Build : ca65 buzzrun.asm -o buzzrun.o && ld65 buzzrun.o -C buzzrun.cfg -o buzzrun.prg ; ; Technique notes ; - Multicolor text mode, 38-column mode, hardware X fine scroll ($D016). ; - Two raster IRQs: line 251 (score row unscrolled, frame tick, sprite ; update) and line 58 (apply fine scroll to the playfield). ; - Coarse scroll copies rows top-to-bottom starting in the lower border, ; staying ahead of the beam, so there is no tearing. ; - Sprite/background collision register $D01F gives pixel-exact hits. ; - Gap heights come from an 8-bit LFSR seeded by the frame counter ; at the moment the player presses fire. ; =========================================================================== ; ---------------------------------------------------------------- hardware SCREEN = $0400 COLRAM = $D800 CHARSET = $3000 SPRDATA = $2000 ; sprite block $80 SPRPTR = SCREEN + $03F8 VIC_SPR0X = $D000 VIC_SPR0Y = $D001 VIC_SPRXHI = $D010 VIC_CTRL1 = $D011 VIC_RASTER = $D012 VIC_SPREN = $D015 VIC_CTRL2 = $D016 VIC_MEM = $D018 VIC_IRQ = $D019 VIC_IRQEN = $D01A VIC_SPRMC = $D01C VIC_SBCOLL = $D01F VIC_BORDER = $D020 VIC_BG0 = $D021 VIC_BG1 = $D022 VIC_BG2 = $D023 VIC_SPRMC0 = $D025 VIC_SPRMC1 = $D026 VIC_SPR0COL = $D027 SID = $D400 CIA1_PRA = $DC00 CIA1_PRB = $DC01 CIA1_ICR = $DC0D CIA2_ICR = $DD0D IRQVEC = $0314 KERNAL_IRQ_EXIT = $EA81 ; ---------------------------------------------------------------- tuning BEE_X = 88 ; sprite X register (fixed) Y_CEIL = 56 ; topmost sprite Y (art starts 3 lines down) Y_FLOOR = 221 ; art bottom sits on the grass Y_START = 110 GRAV = $0016 ; 8.8 px/frame^2 (~0.086) FLAP_VEL = $FE4D ; -1.7 px/frame (~18 px rise incl. apex hang) MAX_FALL = $0240 ; +2.25 px/frame GRAV_HANG = $000C ; gravity near the top of the arc (~0.05) HANG_WINDOW = $0080 ; "near the top" = |vel| < 0.5 px/frame .assert GRAV < $100 && GRAV_HANG < $100 && HANG_WINDOW < $100, error, "physics assumes 8-bit gravity/window" TITLE_BOB_Y = 120 GAP = 6 ; gap height in rows PERIOD = 14 ; columns from one pillar to the next SCORE_COL = 7 ; screen column checked for "pillar passed" ; game states ST_TITLE = 0 ST_PLAY = 1 ST_DEAD = 2 ; custom character codes (multicolor) CH_SKY = $20 CH_BL = $40 ; pillar body left / mid / right CH_BM = $41 CH_BR = $42 CH_CBL = $43 ; cap at bottom end of upper pillar CH_CBM = $44 CH_CBR = $45 CH_CTL = $46 ; cap at top end of lower pillar CH_CTM = $47 CH_CTR = $48 CH_G1 = $49 ; grass row CH_G2 = $4A CH_D1 = $4B ; dirt row CH_D2 = $4C ; ---------------------------------------------------------------- macros .macro SET_PTRS row, col, str lda #<(SCREEN+(row)*40+(col)) sta ptr lda #>(SCREEN+(row)*40+(col)) sta ptr+1 lda #str sta ptr2+1 .endmacro .macro PRINT_AT row, col, str SET_PTRS row, col, str jsr print_hires .endmacro ; ---------------------------------------------------------------- zero page .segment "ZEROPAGE" ptr: .res 2 ptr2: .res 2 state: .res 1 frame_flag: .res 1 fine: .res 1 fine_cur: .res 1 ; read by the line-58 IRQ spr_y: .res 1 ; written to VIC by the IRQ spr_ptr: .res 1 ypos: .res 2 ; lo = fraction, hi = pixel vel: .res 2 fire_prev: .res 1 anim: .res 1 colphase: .res 1 colparity: .res 1 gap_top: .res 1 score_lo: .res 1 ; BCD, two lower digits score_hi: .res 1 ; BCD, hundreds digit best_lo: .res 1 best_hi: .res 1 timer: .res 1 sfx1_t: .res 1 sfx1_f: .res 1 tmp: .res 1 seed: .res 1 ; ---------------------------------------------------------------- BASIC stub .segment "LOADADDR" .word $0801 .segment "CODE" basic: .word basic_end, 10 ; 10 SYS 2061 .byte $9E, "2061", 0 basic_end: .word 0 ; =========================================================================== start: sei cld lda #$7F ; CIA interrupts off (no KERNAL keyscan) sta CIA1_ICR sta CIA2_ICR lda CIA1_ICR lda CIA2_ICR jsr copy_charset jsr copy_sprites jsr init_sid ; --- VIC setup lda #14 sta VIC_BORDER sta VIC_BG0 lda #13 ; %01 multicolor: light green sta VIC_BG1 lda #0 ; %10 multicolor: black outline sta VIC_BG2 lda #$1C ; screen $0400, charset $3000 sta VIC_MEM lda #$1B sta VIC_CTRL1 lda #$10 ; multicolor, 38 columns, xscroll 0 sta VIC_CTRL2 lda #BEE_X sta VIC_SPR0X lda #0 sta VIC_SPRXHI lda #1 sta VIC_SPREN sta VIC_SPRMC lda #0 sta VIC_SPRMC0 ; black lda #1 sta VIC_SPRMC1 ; white lda #7 sta VIC_SPR0COL ; yellow lda #0 sta best_lo sta best_hi sta frame_flag lda #$10 sta fire_prev ; require a fresh press ; --- raster IRQ lda #irq_bottom sta IRQVEC+1 lda #251 sta VIC_RASTER lda #1 sta VIC_IRQEN lda #$FF sta VIC_IRQ jsr new_game cli ; =========================================================================== main_loop: lda frame_flag beq main_loop lda #0 sta frame_flag ; ---- scrolling (only while playing) lda state cmp #ST_PLAY bne @no_scroll dec fine bpl @fine_only lda #7 sta fine sta fine_cur ; must be set before raster line 58 jsr shift_screen jsr new_column jsr check_score jmp @no_scroll @fine_only: lda fine sta fine_cur @no_scroll: jsr read_fire ; A = 1 on a fresh press sta tmp .ifdef AUTOTEST ; test build: autostart + autopilot lda state cmp #ST_PLAY bne :+ lda #0 sta tmp lda ypos+1 cmp #140 bcc :+ lda vel+1 bmi :+ inc tmp : lda state bne :+ inc tmp : .endif lda state cmp #ST_TITLE beq do_title cmp #ST_PLAY beq do_play jmp do_dead ; ---------------------------------------------------------------- TITLE do_title: lda tmp beq @idle ; start the game jsr clear_playfield lda #ST_PLAY sta state lda anim ; seed the RNG from player timing ora #1 sta seed lda VIC_SBCOLL ; discard stale collisions jsr flap jmp frame_end @idle: jsr physics lda ypos+1 cmp #TITLE_BOB_Y bcc @noflap lda #FLAP_VEL sta vel+1 @noflap: jmp frame_end ; ---------------------------------------------------------------- PLAY do_play: lda tmp beq @nofl jsr flap @nofl: jsr physics ; ceiling lda ypos+1 cmp #Y_CEIL bcs @below_ceil lda #Y_CEIL sta ypos+1 lda #0 sta ypos sta vel sta vel+1 @below_ceil: ; floor or pillar hit? lda ypos+1 cmp #Y_FLOOR bcs @die lda VIC_SBCOLL .ifdef INVINCIBLE lda #0 .endif and #1 beq frame_end @die: jsr kill_bee jmp frame_end ; ---------------------------------------------------------------- DEAD do_dead: jsr physics lda ypos+1 cmp #Y_FLOOR bcc @air lda #Y_FLOOR sta ypos+1 lda #0 sta ypos sta vel sta vel+1 @air: lda timer beq @ready dec timer bne @wait ; timer just expired: show game over PRINT_AT 9, 15, msg_over PRINT_AT 12, 10, msg_again @wait: jmp frame_end_noanim @ready: lda tmp beq @wait jsr new_game jmp frame_end ; ---------------------------------------------------------------- frame end frame_end: inc anim ; wing buzz lda anim lsr lsr and #1 ora #(SPRDATA/64) sta spr_ptr frame_end_noanim: lda ypos+1 sta spr_y jsr update_sfx jmp main_loop ; =========================================================================== ; Game helpers ; =========================================================================== new_game: lda #ST_TITLE sta state lda #0 sta score_lo sta score_hi sta fine sta fine_cur sta colphase sta ypos sta vel sta vel+1 lda #Y_START sta ypos+1 sta spr_y lda #(SPRDATA/64) sta spr_ptr jsr clear_playfield jsr draw_hud PRINT_AT 7, 19, msg_title PRINT_AT 10, 14, msg_sub PRINT_AT 13, 14, msg_ctrl rts flap: lda #FLAP_VEL sta vel+1 ; flap sound: rising triangle chirp on voice 1 lda #$00 sta SID+4 lda #$06 sta SID+5 lda #$00 sta SID+6 lda #$14 sta sfx1_f sta SID+1 lda #$11 sta SID+4 lda #6 sta sfx1_t rts kill_bee: lda #ST_DEAD sta state lda #50 sta timer lda #0 sta sfx1_t sta vel sta vel+1 ; crunch: noise burst on voice 1 lda #$80 sta SID+4 lda #$0A sta SID+5 lda #$00 sta SID+6 lda #$08 sta SID+1 lda #$81 sta SID+4 ; new best? lda score_hi cmp best_hi bcc @done bne @newbest lda score_lo cmp best_lo bcc @done beq @done @newbest: lda score_lo sta best_lo lda score_hi sta best_hi jsr draw_hud @done: rts ; vel += GRAV (clamped), ypos += vel physics: ; apex hang: weaker gravity while |vel| is small (top of the arc) ldx #MAX_FALL bcc @noclamp lda #MAX_FALL sta vel+1 @noclamp: clc lda ypos adc vel sta ypos lda ypos+1 adc vel+1 sta ypos+1 rts ; returns A=1 on a fresh press of fire (joy 1/2) or space read_fire: lda #$7F ; select keyboard column with SPACE sta CIA1_PRA lda CIA1_PRA ; joystick 2 and CIA1_PRB ; joystick 1 fire / space and #$10 eor #$10 ; $10 = pressed tax ldy fire_prev stx fire_prev cpx #$10 bne @none cpy #$10 beq @none lda #1 rts @none: lda #0 rts ; after a coarse scroll: did a pillar's right edge just pass the bee? check_score: lda SCREEN+40+SCORE_COL cmp #CH_BR bne @no sed clc lda score_lo adc #1 sta score_lo lda score_hi adc #0 and #$0F sta score_hi cld jsr draw_hud ; ding on voice 2 lda #$10 sta SID+7+4 lda #$09 sta SID+7+5 lda #$00 sta SID+7+6 lda #$00 sta SID+7+0 lda #$38 sta SID+7+1 lda #$11 sta SID+7+4 @no: rts update_sfx: lda sfx1_t beq @off dec sfx1_t bne @sweep lda #$10 ; gate off sta SID+4 rts @sweep: lda sfx1_f clc adc #$06 sta sfx1_f sta SID+1 @off: rts ; =========================================================================== ; Screen ; =========================================================================== ; coarse scroll: rows 1..24, columns 0..38 <- 1..39 (top to bottom) shift_screen: .repeat 24, R ldx #0 : lda SCREEN+(R+1)*40+1,x sta SCREEN+(R+1)*40,x inx cpx #39 bne :- .endrepeat rts ; fill column 39 of the playfield with the next slice of the world new_column: lda colphase bne @not_new ; a new pillar starts: choose the gap : jsr rand and #$0F cmp #12 bcs :- clc adc #3 ; gap_top = 3..14 sta gap_top @not_new: ldx colphase cpx #3 bcs @sky ; phases 3.. are open sky ; pillar column x (0=left,1=mid,2=right) lda #<(SCREEN+40+39) sta ptr lda #>(SCREEN+40+39) sta ptr+1 ldy #1 ; Y = screen row @row: tya sec sbc gap_top ; row - gap_top cmp #$FF beq @capb ; row = gap_top-1 cmp #GAP beq @capt ; row = gap_top+GAP bcc @gap ; gap_top <= row < gap_top+GAP lda body_chars,x bne @put @capb: lda capb_chars,x bne @put @capt: lda capt_chars,x bne @put @gap: lda #CH_SKY @put: sty tmp ldy #0 sta (ptr),y ldy tmp jsr ptr_next_row iny cpy #23 bne @row jmp @ground @sky: lda #CH_SKY sta SCREEN+40+39 sta SCREEN+40+39+40*1 sta SCREEN+40+39+40*2 sta SCREEN+40+39+40*3 sta SCREEN+40+39+40*4 sta SCREEN+40+39+40*5 sta SCREEN+40+39+40*6 sta SCREEN+40+39+40*7 sta SCREEN+40+39+40*8 sta SCREEN+40+39+40*9 sta SCREEN+40+39+40*10 sta SCREEN+40+39+40*11 sta SCREEN+40+39+40*12 sta SCREEN+40+39+40*13 sta SCREEN+40+39+40*14 sta SCREEN+40+39+40*15 sta SCREEN+40+39+40*16 sta SCREEN+40+39+40*17 sta SCREEN+40+39+40*18 sta SCREEN+40+39+40*19 sta SCREEN+40+39+40*20 sta SCREEN+40+39+40*21 @ground: lda colparity eor #1 sta colparity tax lda grass_chars,x sta SCREEN+23*40+39 lda dirt_chars,x sta SCREEN+24*40+39 inc colphase lda colphase cmp #PERIOD bcc :+ lda #0 sta colphase : rts ; 8-bit Galois LFSR, period 255 (seed must be non-zero) rand: lda seed asl bcc :+ eor #$1D : sta seed rts ptr_next_row: lda ptr clc adc #40 sta ptr bcc :+ inc ptr+1 : rts ; clear rows 1..22 to sky, draw ground, reset colors clear_playfield: ldx #0 @c: lda #CH_SKY sta SCREEN+40,x sta SCREEN+40+220,x sta SCREEN+40+440,x sta SCREEN+40+660,x ; rows 1..22 = 880 bytes, 4 x 220 lda #13 ; multicolor + green sta COLRAM+40,x sta COLRAM+40+220,x sta COLRAM+40+440,x sta COLRAM+40+660,x inx cpx #220 bne @c ldx #39 @g: txa and #1 tay lda grass_chars,y sta SCREEN+23*40,x lda dirt_chars,y sta SCREEN+24*40,x lda #15 ; multicolor + yellow sta COLRAM+23*40,x sta COLRAM+24*40,x lda #1 ; HUD row: hi-res white sta COLRAM,x dex bpl @g lda #1 sta colparity rts draw_hud: ldx #39 lda #CH_SKY : sta SCREEN,x dex bpl :- SET_PTRS 0, 1, msg_score jsr print_str SET_PTRS 0, 29, msg_best jsr print_str ; score digits at col 7 ldx #7 lda score_hi ldy score_lo jsr put_bcd3 ldx #34 lda best_hi ldy best_lo jsr put_bcd3 rts ; A = hundreds digit, Y = BCD tens/ones, X = column on row 0 put_bcd3: ora #'0' sta SCREEN,x tya lsr lsr lsr lsr ora #'0' sta SCREEN+1,x tya and #$0F ora #'0' sta SCREEN+2,x rts ; copy ASCII string at (ptr2) to (ptr) as screen codes; returns Y = length print_str: ldy #0 @l: lda (ptr2),y beq @d and #$3F sta (ptr),y iny bne @l @d: rts ; as print_str, then colour the cells yellow (hi-res, so text is readable ; inside the multicolor playfield) print_hires: jsr print_str lda ptr+1 pha clc adc #>(COLRAM-SCREEN) sta ptr+1 lda #7 @c: dey bmi @x sta (ptr),y jmp @c @x: pla sta ptr+1 rts ; =========================================================================== ; Init ; =========================================================================== copy_charset: lda #$33 ; char ROM visible at $D000 sta $01 ldx #0 : lda $D000,x sta CHARSET,x lda $D100,x sta CHARSET+$100,x lda $D200,x sta CHARSET+$200,x lda $D300,x sta CHARSET+$300,x inx bne :- lda #$37 sta $01 ldx #(custom_chars_end-custom_chars-1) : lda custom_chars,x sta CHARSET+CH_BL*8,x dex bpl :- rts copy_sprites: ldx #127 : lda bee_f1,x sta SPRDATA,x dex bpl :- rts init_sid: ldx #$18 lda #0 : sta SID,x dex bpl :- lda #$0F ; volume 15 sta SID+$18 rts ; =========================================================================== ; IRQs ; =========================================================================== irq_bottom: lda #$FF sta VIC_IRQ lda #$10 ; HUD row: no fine scroll sta VIC_CTRL2 lda spr_y sta VIC_SPR0Y lda spr_ptr sta SPRPTR inc frame_flag lda #irq_top sta IRQVEC+1 lda #58 sta VIC_RASTER jmp KERNAL_IRQ_EXIT irq_top: lda #$FF sta VIC_IRQ lda fine_cur ora #$10 sta VIC_CTRL2 lda #irq_bottom sta IRQVEC+1 lda #251 sta VIC_RASTER jmp KERNAL_IRQ_EXIT ; =========================================================================== ; Data ; =========================================================================== body_chars: .byte CH_BL, CH_BM, CH_BR capb_chars: .byte CH_CBL, CH_CBM, CH_CBR capt_chars: .byte CH_CTL, CH_CTM, CH_CTR grass_chars: .byte CH_G1, CH_G2 dirt_chars: .byte CH_D1, CH_D2 msg_score: .byte "SCORE", 0 msg_best: .byte "BEST", 0 msg_title: .byte "BUZZ RUN", 0 msg_sub: .byte "FLAP PAST THE HEDGES", 0 msg_ctrl: .byte "FIRE OR SPACE", 0 msg_over: .byte "GAME OVER", 0 msg_again: .byte "PRESS FIRE TO RETRY", 0 ; multicolor chars: %00 sky, %01 light green, %10 black, %11 colour RAM custom_chars: ; body L/M/R (inset one pixel on each side, narrower than the caps) .byte $27,$27,$27,$27,$27,$27,$27,$27 .byte $FF,$DF,$FF,$FF,$FF,$DF,$FF,$FF .byte $FA,$F8,$FA,$F8,$FA,$F8,$FA,$F8 ; cap at bottom of upper pillar L/M/R .byte $95,$95,$95,$95,$95,$95,$AA,$00 .byte $7F,$7F,$7F,$7F,$7F,$7F,$AA,$00 .byte $FE,$FE,$FE,$FE,$FE,$FE,$AA,$00 ; cap at top of lower pillar L/M/R .byte $00,$AA,$95,$95,$95,$95,$95,$95 .byte $00,$AA,$7F,$7F,$7F,$7F,$7F,$7F .byte $00,$AA,$FE,$FE,$FE,$FE,$FE,$FE ; grass 1/2 .byte $55,$55,$65,$AA,$FF,$FB,$FF,$BF .byte $55,$55,$56,$AA,$FF,$BF,$FF,$FB ; dirt 1/2 .byte $FF,$EF,$FF,$FE,$FF,$BF,$FF,$FF .byte $FF,$FF,$FB,$FF,$EF,$FF,$FF,$FE custom_chars_end: ; multicolor sprite: %01 black, %10 yellow, %11 white bee_f1: .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$F3,$C0,$03,$FF,$F0,$03 .byte $FF,$F0,$00,$FF,$C0,$06,$9A,$50,$1A,$69,$A4,$6A,$69,$9D,$6A,$69 .byte $A9,$1A,$69,$A4,$06,$9A,$50,$01,$04,$00,$00,$00,$00,$00,$00,$00 .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 bee_f2: .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 .byte $00,$00,$00,$00,$00,$06,$9A,$50,$FF,$F9,$A4,$FF,$FD,$9D,$3F,$F9 .byte $A9,$1F,$E9,$A4,$06,$9A,$50,$01,$04,$00,$00,$00,$00,$00,$00,$00 .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 code_end: .assert code_end <= SPRDATA, error, "program overlaps sprite data at $2000"