|
- ; ===========================================================================
- ; BLAST RUNNER - an auto-scrolling run-and-gun for the Commodore 64
- ;
- ; Controls: SPACE jump, CTRL shoot, A / D move left / right
- ; (joystick port 2 also works: up = jump, fire = shoot)
- ; Build : build.bat (python gfx.py, ca65, ld65)
- ;
- ; Technique notes
- ; - Multicolor text mode, 38 columns, hardware X fine scroll ($D016),
- ; 1 pixel per frame. Row 0 (HUD) is kept unscrolled by a raster split.
- ; - Coarse scroll copies rows top to bottom just after the frame IRQ,
- ; staying ahead of the beam. The next column is generated 4 frames
- ; earlier into a buffer, so the coarse-scroll frame only copies.
- ; - No multiplexing: sprite 0 player, 1-2 player bullets, 3-6 enemies
- ; and explosions, 7 enemy shot or bomb.
- ; - Terrain collision reads the screen: chars >= CH_SOLID are solid
- ; from above (grass and platforms). Object collision uses boxes in
- ; half-pixel X units so everything fits in 8 bits.
- ; ===========================================================================
-
- .include "gfx.inc"
-
- ; ---------------------------------------------------------------- hardware
- SCREEN = $0400
- COLRAM = $D800
- CHARSET = $2800
- SPRPTR = SCREEN + $03F8
-
- 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_SPRPRI = $D01B
- VIC_SPRMC = $D01C
- VIC_SPRDW = $D01D
- VIC_SPRDH = $D017
- 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
- PX_MIN = 32 ; player sprite X limits (screen relative)
- PX_MAX = 216
- PX_START = 64
- PY_MIN = 40
- GROUND_ROW = 21 ; grass row
- PLAT1_ROW = 17
- PLAT2_ROW = 13
- PIT_Y = 222 ; player sprite Y that counts as "fell in"
- GRAV = $0030 ; 8.8 px/frame^2 (0.19)
- JUMP_VEL = $FC40 ; -3.75 px/frame -> ~37 px high
- CUT_VEL = $FE80 ; releasing SPACE caps the rise at -1.5
- BOUNCE_VEL = $FB80 ; pit bounce while invulnerable
- MAX_FALL = $0400
- COYOTE = 5 ; frames after leaving a ledge you may jump
- JUMP_BUF = 6 ; frames a jump press is remembered
- FIRE_RATE = 11
- BUL_SPEED = 7
- BUL_LIFE = 30
- SPAWN_X = 340
-
- ; sprite Y that puts the feet on top of a character row
- .define ROW_Y(r) (30 + (r) * 8)
-
- ; key bits
- K_LEFT = $01
- K_RIGHT = $02
- K_JUMP = $04
- K_FIRE = $08
-
- ; states
- ST_TITLE = 0
- ST_PLAY = 1
- ST_DYING = 2
- ST_OVER = 3
-
- ; object types
- T_NONE = 0
- T_PLAYER = 1
- T_PBUL = 2
- T_EXPL = 3
- T_SOLDIER = 4 ; types >= T_SOLDIER are hostile
- T_DRONE = 5
- T_ESHOT = 6
- T_BOMB = 7
-
- ; sound effects
- SFX_SHOT = 0
- SFX_JUMP = 1
- SFX_BOOM = 2
- SFX_ESHOT = 3
- SFX_DIE = 4
- SFX_BOMB = 5
-
- ; colours
- COL_PLAYER = 13
- COL_SOLDIER = 2
- COL_DRONE = 15
- COL_EXPL = 7
-
- ; ---------------------------------------------------------------- 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
- 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
- ptr3: .res 2
- state: .res 1
- frame_flag: .res 1
- frame: .res 1
- scrolling: .res 1
- fine: .res 1
- fine_cur: .res 1 ; read by the line-58 IRQ
- tmp: .res 1
- tmp2: .res 1
- seed: .res 2
- keys: .res 1
- keys_prev: .res 1
- keys_new: .res 1
- timer: .res 1
- ; player
- px: .res 1
- py_f: .res 1
- py: .res 1
- pvy_l: .res 1
- pvy_h: .res 1
- on_ground: .res 1
- coyote: .res 1
- jbuf: .res 1
- fire_cd: .res 1
- panim: .res 1
- invuln: .res 1
- lives: .res 1
- ; score (BCD, s0 = lowest two digits)
- s0: .res 1
- s1: .res 1
- s2: .res 1
- h0: .res 1
- h1: .res 1
- h2: .res 1
- ; world generator
- gen_col: .res 1
- g_on: .res 1
- g_len: .res 1
- g_age: .res 1
- p1_on: .res 1
- p1_len: .res 1
- p1_age: .res 1
- p2_on: .res 1
- p2_len: .res 1
- p2_age: .res 1
- b_h: .res 1
- b_len: .res 1
- colpar: .res 1
- spawn_cnt: .res 1
- level: .res 1
- dist: .res 1
- ; solid_at arguments
- sx_l: .res 1
- sx_h: .res 1
- sy: .res 1
- t0: .res 1
- t1: .res 1
- sfx_sx: .res 1
-
- ; ---------------------------------------------------------------- BSS
- .segment "BSS"
- o_type: .res 8
- o_xl: .res 8
- o_xh: .res 8
- o_y: .res 8
- o_ptr: .res 8
- o_col: .res 8
- o_t: .res 8
- o_t2: .res 8
- o_base: .res 8
- o_bl: .res 8 ; collision box, X in half pixels
- o_w: .res 8
- o_bt: .res 8
- o_h: .res 8
- colbuf: .res 25
- sv_t: .res 3
- sv_freq: .res 3
- sv_delta: .res 3
- sv_wave: .res 3
-
- ; ---------------------------------------------------------------- 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
- ldx #$FF
- txs
- lda #$7F ; CIA interrupts off (no KERNAL keyscan)
- sta CIA1_ICR
- sta CIA2_ICR
- lda CIA1_ICR
- lda CIA2_ICR
- lda #$FF ; CIA1 port A out (keyboard columns)
- sta $DC02
- lda #$00 ; port B in (keyboard rows)
- sta $DC03
-
- jsr copy_charset
- jsr init_sid
-
- ; --- VIC setup
- lda #0
- sta VIC_BORDER
- lda #6 ; night sky
- sta VIC_BG0
- lda #9 ; %01 brown
- sta VIC_BG1
- lda #11 ; %10 dark grey
- sta VIC_BG2
- lda #$1A ; screen $0400, charset $2800
- sta VIC_MEM
- lda #$1B
- sta VIC_CTRL1
- lda #$10 ; multicolor, 38 columns, xscroll 0
- sta VIC_CTRL2
- lda #0
- sta VIC_SPREN
- sta VIC_SPRPRI
- sta VIC_SPRDW
- sta VIC_SPRDH
- sta VIC_SPRMC0 ; black
- lda #10
- sta VIC_SPRMC1 ; pink
-
- lda #0
- sta h0
- sta h1
- sta h2
- sta frame_flag
- sta frame
- lda #$FF
- sta keys_prev ; ignore keys held at start-up
- lda #$5A
- sta seed
- lda #$C3
- sta seed+1
-
- ; --- raster IRQ
- lda #<irq_bottom
- sta IRQVEC
- 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
- inc frame
-
- ; sprites first: we are in the lower border, nothing is drawn yet
- jsr show_sprites
-
- ; ---- scrolling
- lda scrolling
- beq @no_scroll
- dec fine
- bpl @fine_only
- lda #7
- sta fine
- sta fine_cur ; must be set before raster line 58
- jsr shift_screen
- lda #39
- sta gen_col
- jsr write_column
- jmp @no_scroll
- @fine_only:
- lda fine
- sta fine_cur
- cmp #4 ; build the next column ahead of time
- bne @no_scroll
- jsr gen_column
- jsr column_tick
- @no_scroll:
-
- jsr read_input
- .ifdef AUTOTEST
- jsr autopilot
- .endif
-
- lda state
- cmp #ST_TITLE
- bne :+
- jmp do_title
- : cmp #ST_PLAY
- bne :+
- jmp do_play
- : cmp #ST_DYING
- bne :+
- jmp do_dying
- : jmp do_over
-
- ; ---------------------------------------------------------------- TITLE
- do_title:
- lda keys_new
- and #(K_JUMP|K_FIRE)
- beq frame_end
- jsr start_game
- jmp frame_end
-
- ; ---------------------------------------------------------------- PLAY
- do_play:
- jsr update_player
- jsr update_objects
- jsr collisions
- jmp frame_end
-
- ; ---------------------------------------------------------------- DYING
- do_dying:
- dec timer
- lda timer
- cmp #74
- bcc @hide
- sbc #74 ; 0..15, high = early
- lsr
- lsr
- tay
- lda expl_seq,y
- sta o_ptr
- lda frame
- and #2
- lsr
- clc
- adc #7 ; flicker yellow/orange
- sta o_col
- jmp @cont
- @hide:
- lda #T_NONE
- sta o_type
- @cont:
- lda timer
- bne @go
- lda lives
- beq @over
- jsr respawn
- jmp @go
- @over:
- jsr game_over
- jmp frame_end
- @go:
- jsr update_objects
- jsr collisions
- jmp frame_end
-
- ; ---------------------------------------------------------------- GAME OVER
- do_over:
- lda timer
- beq @ready
- dec timer
- bne frame_end
- PRINT_AT 10, 10, msg_again
- jmp frame_end
- @ready:
- lda keys_new
- and #(K_JUMP|K_FIRE)
- beq frame_end
- jsr new_game
- ; fall through
-
- frame_end:
- jsr update_sfx
- jmp main_loop
-
- ; ===========================================================================
- ; Game flow
- ; ===========================================================================
- new_game:
- lda #ST_TITLE
- sta state
- lda #0
- sta scrolling
- sta s0
- sta s1
- sta s2
- jsr clear_objects
- lda #3
- sta lives
- jsr build_world
- jsr init_colors
- jsr draw_hud
- PRINT_AT 4, 8, msg_title
- PRINT_AT 7, 6, msg_ctrl1
- PRINT_AT 9, 5, msg_ctrl2
- PRINT_AT 11, 10, msg_start
- rts
-
- start_game:
- lda frame ; seed from player timing
- eor #$A5
- sta seed
- ora #1
- sta seed+1
- lda #0
- sta s0
- sta s1
- sta s2
- sta level
- sta dist
- lda #3
- sta lives
- lda #20
- sta spawn_cnt
- jsr clear_objects
- jsr build_world
- jsr init_colors
- jsr draw_hud
- lda #PX_START
- sta px
- lda #ROW_Y(GROUND_ROW)
- sta py
- lda #0
- sta py_f
- sta pvy_l
- sta pvy_h
- sta jbuf
- sta fire_cd
- sta invuln
- lda #T_PLAYER
- sta o_type
- lda #SP_PRUN_A
- sta o_ptr
- jsr set_player_obj
- lda #ST_PLAY
- sta state
- lda #1
- sta scrolling
- rts
-
- respawn:
- lda #ST_PLAY
- sta state
- lda #PX_START
- sta px
- lda #60
- sta py
- lda #0
- sta py_f
- sta pvy_l
- sta pvy_h
- sta coyote
- sta jbuf
- lda #150
- sta invuln
- lda #T_PLAYER
- sta o_type
- lda #SP_PJUMP
- sta o_ptr
- jmp set_player_obj
-
- kill_player:
- lda #ST_DYING
- sta state
- lda #T_EXPL
- sta o_type
- lda #90
- sta timer
- dec lives
- jsr draw_hud
- lda #SFX_DIE
- jmp play_sfx
-
- game_over:
- lda #ST_OVER
- sta state
- lda #0
- sta scrolling
- lda #100
- sta timer
- ; new high score?
- lda s2
- cmp h2
- bcc @done
- bne @new
- lda s1
- cmp h1
- bcc @done
- bne @new
- lda s0
- cmp h0
- bcc @done
- @new:
- lda s0
- sta h0
- lda s1
- sta h1
- lda s2
- sta h2
- jsr draw_hud
- @done:
- PRINT_AT 8, 14, msg_over
- rts
-
- clear_objects:
- ldx #7
- lda #T_NONE
- : sta o_type,x
- dex
- bpl :-
- rts
-
- ; ===========================================================================
- ; Player
- ; ===========================================================================
- update_player:
- lda invuln
- beq :+
- dec invuln
- :
- ; --- left / right
- lda keys
- and #K_LEFT
- beq @noleft
- lda px
- sec
- sbc #2
- cmp #PX_MIN
- bcs :+
- lda #PX_MIN
- : sta px
- @noleft:
- lda keys
- and #K_RIGHT
- beq @noright
- lda px
- clc
- adc #2
- cmp #PX_MAX+1
- bcc :+
- lda #PX_MAX
- : sta px
- @noright:
-
- ; --- jump (buffered press + coyote time)
- lda keys_new
- and #K_JUMP
- beq :+
- lda #JUMP_BUF
- sta jbuf
- : lda jbuf
- beq @nojump
- dec jbuf
- lda coyote
- beq @nojump
- lda #<JUMP_VEL
- sta pvy_l
- lda #>JUMP_VEL
- sta pvy_h
- lda #0
- sta coyote
- sta jbuf
- lda #SFX_JUMP
- jsr play_sfx
- @nojump:
- ; releasing jump while rising cuts the jump short
- lda keys
- and #K_JUMP
- bne @nocut
- lda pvy_h
- bpl @nocut
- lda pvy_l
- cmp #<CUT_VEL
- lda pvy_h
- sbc #>CUT_VEL
- bvc :+
- eor #$80
- : bpl @nocut ; already slower than CUT_VEL
- lda #<CUT_VEL
- sta pvy_l
- lda #>CUT_VEL
- sta pvy_h
- @nocut:
- ; --- gravity
- lda pvy_l
- clc
- adc #<GRAV
- sta pvy_l
- lda pvy_h
- adc #>GRAV
- sta pvy_h
- bmi @noclamp
- cmp #>MAX_FALL
- bcc @noclamp
- lda #<MAX_FALL
- sta pvy_l
- lda #>MAX_FALL
- sta pvy_h
- @noclamp:
- lda py_f
- clc
- adc pvy_l
- sta py_f
- lda py
- adc pvy_h
- sta py
- cmp #PY_MIN
- bcs :+
- lda #PY_MIN
- sta py
- lda #0
- sta pvy_l
- sta pvy_h
- :
- ; --- landing (only while falling, within the top 5 lines of a row)
- lda #0
- sta on_ground
- lda pvy_h
- bmi @air
- lda py
- clc
- adc #21
- sta sy
- sec
- sbc #51
- and #7
- cmp #5
- bcs @air
- lda #0
- sta sx_h
- lda px
- clc
- adc #6
- sta sx_l
- jsr solid_at
- bcs @land
- lda px
- clc
- adc #18
- sta sx_l
- jsr solid_at
- bcc @air
- @land:
- lda sy
- sec
- sbc #51
- and #$F8
- clc
- adc #(51-21)
- sta py
- lda #0
- sta py_f
- sta pvy_l
- sta pvy_h
- lda #1
- sta on_ground
- lda #COYOTE
- sta coyote
- jmp @after
- @air:
- lda coyote
- beq :+
- dec coyote
- : lda py
- cmp #PIT_Y
- bcc @after
- .ifndef INVINCIBLE
- lda invuln
- bne @bounce
- jmp kill_player
- .endif
- @bounce:
- lda #<BOUNCE_VEL
- sta pvy_l
- lda #>BOUNCE_VEL
- sta pvy_h
- @after:
-
- ; --- shooting (hold CTRL for auto-fire)
- lda fire_cd
- beq :+
- dec fire_cd
- jmp @noshoot
- : lda keys
- and #K_FIRE
- beq @noshoot
- ldx #1
- lda o_type+1
- beq @slot
- inx
- lda o_type+2
- bne @noshoot
- @slot: lda #T_PBUL
- sta o_type,x
- lda px
- clc
- adc #20
- sta o_xl,x
- lda #0
- adc #0
- sta o_xh,x
- lda py
- clc
- adc #7
- sta o_y,x
- lda #SP_PBUL
- sta o_ptr,x
- lda #7
- sta o_col,x
- lda #BUL_LIFE
- sta o_t,x
- lda #FIRE_RATE
- sta fire_cd
- lda #SFX_SHOT
- jsr play_sfx
- @noshoot:
-
- ; --- animation
- lda on_ground
- beq @jumpf
- inc panim
- lda panim
- lsr
- lsr
- and #3
- tay
- lda run_seq,y
- bne @setp
- @jumpf: lda #SP_PJUMP
- @setp: sta o_ptr
- ; fall through
- set_player_obj:
- lda px
- sta o_xl
- lda #0
- sta o_xh
- lda py
- sta o_y
- lda #COL_PLAYER
- sta o_col
- rts
-
- ; ---------------------------------------------------------------------------
- ; solid_at: is the character under sprite-coordinate point (sx_h:sx_l, sy)
- ; solid? Returns C=1 if solid. Points right of the screen count as solid
- ; (enemies wait there before scrolling in). Preserves X.
- solid_at:
- lda sx_l
- sec
- sbc #24
- sta t0
- lda sx_h
- sbc #0
- sta t1
- lda t0
- sec
- sbc fine
- sta t0
- lda t1
- sbc #0
- bmi @no ; left of the screen
- lsr
- ror t0
- lsr
- ror t0
- lsr
- ror t0
- lda t0 ; column
- cmp #40
- bcs @yes
- lda sy
- sec
- sbc #51
- bcc @no
- lsr
- lsr
- lsr
- cmp #25
- bcs @no
- tay
- lda row_lo,y
- sta ptr3
- lda row_hi,y
- sta ptr3+1
- ldy t0
- lda (ptr3),y
- cmp #CH_SOLID ; C=1 if solid
- rts
- @yes: sec
- rts
- @no: clc
- rts
-
- ; ===========================================================================
- ; Objects
- ; ===========================================================================
- update_objects:
- ; --- player bullets
- ldx #1
- @b: lda o_type,x
- cmp #T_PBUL
- bne @bn
- lda o_xl,x
- clc
- adc #BUL_SPEED
- sta o_xl,x
- lda o_xh,x
- adc #0
- sta o_xh,x
- dec o_t,x
- beq @kill
- lda o_xh,x
- beq @bn
- lda o_xl,x
- cmp #<344
- bcc @bn
- @kill: lda #T_NONE
- sta o_type,x
- @bn: inx
- cpx #3
- bne @b
-
- ; --- enemies, explosions, enemy shots
- ldx #3
- @e: lda o_type,x
- cmp #T_SOLDIER
- bne :+
- jsr upd_soldier
- jmp @en
- : cmp #T_DRONE
- bne :+
- jsr upd_drone
- jmp @en
- : cmp #T_EXPL
- bne :+
- jsr upd_expl
- jmp @en
- : cmp #T_ESHOT
- bne :+
- jsr upd_eshot
- jmp @en
- : cmp #T_BOMB
- bne @en
- jsr upd_bomb
- @en: inx
- cpx #8
- bne @e
- rts
-
- ; obj_sub_x: move object X left by A pixels; frees it once off the left edge
- obj_sub_x:
- sta tmp
- lda o_xl,x
- sec
- sbc tmp
- sta o_xl,x
- lda o_xh,x
- sbc #0
- sta o_xh,x
- bpl :+
- lda #T_NONE
- sta o_type,x
- : rts
-
- ; feet sample point for object X: (x + A, y + 21)
- obj_feet:
- clc
- adc o_xl,x
- sta sx_l
- lda o_xh,x
- adc #0
- sta sx_h
- lda o_y,x
- clc
- adc #21
- sta sy
- rts
-
- upd_soldier:
- ; walk left on odd frames if there is floor ahead
- lda frame
- and #1
- beq @nowalk
- lda #4
- jsr obj_feet
- jsr solid_at
- bcc @nowalk
- lda #1
- jsr obj_sub_x
- @nowalk:
- lda #1 ; scroll with the world
- jsr obj_sub_x
- lda o_type,x
- bne :+
- rts
- : ; gravity
- lda #12
- jsr obj_feet
- jsr solid_at
- bcc @fall
- lda sy ; snap feet to the top of the row
- sec
- sbc #51
- and #$F8
- clc
- adc #(51-21)
- sta o_y,x
- jmp @anim
- @fall: lda o_y,x
- clc
- adc #3
- sta o_y,x
- cmp #245
- bcc @anim
- lda #T_NONE
- sta o_type,x
- rts
- @anim: lda frame
- lsr
- lsr
- lsr
- and #1
- clc
- adc #SP_SOLD_A
- sta o_ptr,x
- ; fire at the player now and then
- dec o_t,x
- bne @done
- jsr rand
- and #63
- clc
- adc #45
- sta o_t,x
- lda o_type+7
- bne @done
- lda o_xh,x
- bne @done
- lda o_xl,x
- sec
- sbc px
- bcc @done ; behind the player: hold fire
- cmp #32
- bcc @done
- lda #T_ESHOT
- sta o_type+7
- lda o_xl,x
- sta o_xl+7
- lda #0
- sta o_xh+7
- lda o_y,x
- clc
- adc #7
- sta o_y+7
- lda #SP_ESHOT
- sta o_ptr+7
- lda #10
- sta o_col+7
- lda #SFX_ESHOT
- jsr play_sfx
- @done: rts
-
- upd_drone:
- lda #2
- jsr obj_sub_x
- lda o_type,x
- beq @done
- inc o_t,x
- lda o_t,x
- and #31
- tay
- lda sintab,y
- clc
- adc o_base,x
- sta o_y,x
- lda frame
- lsr
- lsr
- and #1
- clc
- adc #SP_DRONE_A
- sta o_ptr,x
- ; drop one bomb when just ahead of the player
- lda o_t2,x
- bne @done
- lda o_type+7
- bne @done
- lda o_xh,x
- bne @done
- lda o_xl,x
- sec
- sbc px
- bcc @done
- cmp #24
- bcs @done
- inc o_t2,x
- lda #T_BOMB
- sta o_type+7
- lda o_xl,x
- clc
- adc #10
- sta o_xl+7
- lda #0
- sta o_xh+7
- lda o_y,x
- clc
- adc #12
- sta o_y+7
- lda #1
- sta o_t+7 ; fall speed
- lda #0
- sta o_t2+7
- lda #SP_BOMB
- sta o_ptr+7
- lda #1
- sta o_col+7
- lda #SFX_BOMB
- jsr play_sfx
- @done: rts
-
- upd_expl:
- lda #1
- jsr obj_sub_x
- dec o_t,x
- bne :+
- lda #T_NONE
- sta o_type,x
- rts
- : lda o_t,x
- lsr
- lsr
- tay
- lda expl_seq,y
- sta o_ptr,x
- lda frame
- and #2
- lsr
- clc
- adc #7
- sta o_col,x
- rts
-
- upd_eshot:
- lda #3
- jmp obj_sub_x
-
- upd_bomb:
- lda #1
- jsr obj_sub_x
- lda o_type,x
- beq @done
- inc o_t2,x ; accelerate every 6 frames
- lda o_t2,x
- cmp #6
- bcc :+
- lda #0
- sta o_t2,x
- lda o_t,x
- cmp #4
- bcs :+
- inc o_t,x
- : lda o_y,x
- clc
- adc o_t,x
- sta o_y,x
- cmp #240
- bcs @gone
- ; hit the ground or a platform?
- lda #2
- clc
- adc o_xl,x
- sta sx_l
- lda o_xh,x
- adc #0
- sta sx_h
- lda o_y,x
- clc
- adc #5
- sta sy
- jsr solid_at
- bcc @done
- ; burst: explosion centred on the bomb
- lda #8
- jsr obj_sub_x
- lda o_type,x
- beq @done
- lda o_y,x
- sec
- sbc #12
- sta o_y,x
- jsr make_expl
- lda #SFX_BOOM
- jmp play_sfx
- @gone: lda #T_NONE
- sta o_type,x
- @done: rts
-
- ; turn object X into an explosion
- make_expl:
- lda #T_EXPL
- sta o_type,x
- lda #15
- sta o_t,x
- lda #SP_EXPL_1
- sta o_ptr,x
- lda #COL_EXPL
- sta o_col,x
- rts
-
- ; ---------------------------------------------------------------------------
- collisions:
- ; boxes for every live object
- ldx #7
- @box: ldy o_type,x
- beq @bn
- lda o_xh,x
- lsr
- lda o_xl,x
- ror ; X / 2
- clc
- adc box_x0,y
- sta o_bl,x
- lda box_w,y
- sta o_w,x
- lda o_y,x
- clc
- adc box_y0,y
- sta o_bt,x
- lda box_h,y
- sta o_h,x
- @bn: dex
- bpl @box
-
- ; player bullets vs hostiles
- ldx #1
- @bl: lda o_type,x
- cmp #T_PBUL
- bne @bnext
- ldy #3
- @el: lda o_type,y
- cmp #T_SOLDIER
- bcc @enext
- jsr overlap
- bcc @enext
- lda #T_NONE
- sta o_type,x
- jsr enemy_hit
- jmp @bnext
- @enext: iny
- cpy #8
- bne @el
- @bnext: inx
- cpx #3
- bne @bl
-
- ; player vs hostiles
- lda state
- cmp #ST_PLAY
- bne @done
- lda invuln
- bne @done
- .ifdef INVINCIBLE
- rts
- .endif
- ldx #0
- ldy #3
- @pl: lda o_type,y
- cmp #T_SOLDIER
- bcc @pn
- jsr overlap
- bcc @pn
- jmp kill_player
- @pn: iny
- cpy #8
- bne @pl
- @done: rts
-
- ; overlap: do the boxes of objects X and Y overlap? C=1 yes. Keeps X, Y.
- ; with d = left(X) - left(Y): overlap iff (d + wX - 1) < (wX + wY - 1)
- overlap:
- lda o_w,x
- clc
- adc o_w,y
- sec
- sbc #1
- sta tmp
- lda o_bl,x
- sec
- sbc o_bl,y
- clc
- adc o_w,x
- sec
- sbc #1
- cmp tmp
- bcs @no
- lda o_h,x
- clc
- adc o_h,y
- sec
- sbc #1
- sta tmp
- lda o_bt,x
- sec
- sbc o_bt,y
- clc
- adc o_h,x
- sec
- sbc #1
- cmp tmp
- bcs @no
- sec
- rts
- @no: clc
- rts
-
- ; a player bullet hit object Y. Keeps X.
- enemy_hit:
- stx tmp2
- tya
- tax
- lda o_type,x
- cmp #T_SOLDIER
- bne :+
- jsr make_expl
- lda #$01 ; 100
- ldy #$00
- jmp @score
- : cmp #T_DRONE
- bne :+
- jsr make_expl
- lda #$02 ; 250
- ldy #$50
- jmp @score
- : lda #T_NONE ; shot or bomb: just pop it
- sta o_type,x
- lda #$00 ; 10
- ldy #$10
- jsr add_score
- ldx tmp2
- lda #SFX_ESHOT
- jmp play_sfx
- @score: jsr add_score
- ldx tmp2
- lda #SFX_BOOM
- jmp play_sfx
-
- ; ---------------------------------------------------------------------------
- ; show_sprites: write the object table straight to the VIC (called in the
- ; lower border, before the next frame starts drawing)
- show_sprites:
- lda #0
- sta tmp ; enable
- sta tmp2 ; X msb
- sta t0 ; multicolor
- ldx #7
- @l: ldy o_type,x
- beq @off
- cpx #0
- bne :+
- lda invuln ; blink while invulnerable
- and #4
- bne @off
- : txa
- asl
- tay ; Y = register offset x*2
- lda o_xl,x
- sta $D000,y
- lda o_y,x
- sta $D001,y
- lda o_ptr,x
- sta SPRPTR,x
- lda o_col,x
- sta VIC_SPR0COL,x
- ldy o_type,x
- lda type_mc,y
- lsr
- rol t0
- lda o_xh,x
- lsr
- rol tmp2
- sec
- rol tmp
- jmp @n
- @off: asl t0
- asl tmp2
- asl tmp
- @n: dex
- bpl @l
- lda tmp2
- sta VIC_SPRXHI
- lda t0
- sta VIC_SPRMC
- lda tmp
- sta VIC_SPREN
- rts
-
- ; ===========================================================================
- ; Input
- ; ===========================================================================
- read_input:
- lda #0
- sta tmp
- lda #$FD ; column 1: A = row 2
- sta CIA1_PRA
- lda CIA1_PRB
- and #$04
- bne :+
- lda #K_LEFT
- sta tmp
- : lda #$FB ; column 2: D = row 2
- sta CIA1_PRA
- lda CIA1_PRB
- and #$04
- bne :+
- lda tmp
- ora #K_RIGHT
- sta tmp
- : lda #$7F ; column 7: CTRL = row 2, SPACE = row 4
- sta CIA1_PRA
- lda CIA1_PRB
- tax
- and #$04
- bne :+
- lda tmp
- ora #K_FIRE
- sta tmp
- : txa
- and #$10
- bne :+
- lda tmp
- ora #K_JUMP
- sta tmp
- : ; joystick port 2
- lda #$FF
- sta CIA1_PRA
- lda CIA1_PRA
- eor #$FF
- tax
- and #$01 ; up
- beq :+
- lda tmp
- ora #K_JUMP
- sta tmp
- : txa
- and #$04 ; left
- beq :+
- lda tmp
- ora #K_LEFT
- sta tmp
- : txa
- and #$08 ; right
- beq :+
- lda tmp
- ora #K_RIGHT
- sta tmp
- : txa
- and #$10 ; fire
- beq :+
- lda tmp
- ora #K_FIRE
- sta tmp
- : lda tmp
- sta keys
- lda keys_prev
- eor #$FF
- and keys
- sta keys_new
- lda keys
- sta keys_prev
- rts
-
- .ifdef AUTOTEST
- ; test build: start at once, hold fire, jump at pit edges and now and then
- autopilot:
- lda state
- cmp #ST_PLAY
- beq @play
- lda frame
- and #63
- bne :+
- lda #K_JUMP
- sta keys_new
- : rts
- @play: lda py ; debug: py and on_ground in the HUD
- ldx #24
- jsr put_hex
- lda on_ground
- ldx #26
- jsr put_hex
- lda keys
- ora #K_FIRE
- sta keys
- lda frame
- and #31
- bne :+
- lda keys_new
- ora #K_JUMP
- sta keys_new
- lda keys
- ora #K_JUMP
- sta keys
- : rts
-
- put_hex:
- pha
- lsr
- lsr
- lsr
- lsr
- tay
- lda hexd,y
- sta SCREEN,x
- pla
- and #$0F
- tay
- lda hexd,y
- sta SCREEN+1,x
- rts
- hexd: .byte "0123456789",1,2,3,4,5,6
- .endif
-
- ; ===========================================================================
- ; World generator
- ; ===========================================================================
- ; column_tick: once per column of travel - distance score, difficulty, spawns
- column_tick:
- lda state
- cmp #ST_PLAY
- beq :+
- rts
- : inc dist
- lda dist
- and #3
- bne :+
- lda #$00 ; 10 points per 4 columns
- ldy #$10
- jsr add_score
- : lda dist
- and #63
- bne :+
- lda level
- cmp #8
- bcs :+
- inc level
- : dec spawn_cnt
- beq :+
- rts
- : lda #14
- sec
- sbc level
- sta spawn_cnt
- jsr rand
- and #3
- clc
- adc spawn_cnt
- sta spawn_cnt
- ; fall through
-
- spawn_enemy:
- ldx #3
- : lda o_type,x
- beq @got
- inx
- cpx #7
- bne :-
- rts
- @got:
- lda #<SPAWN_X
- sta o_xl,x
- lda #>SPAWN_X
- sta o_xh,x
- jsr rand
- sta o_t,x
- lda level
- lsr
- clc
- adc #2
- sta tmp ; drone chance in eighths
- jsr rand
- and #7
- cmp tmp
- bcc @drone
- ; soldier: on a platform (if one runs on) or on the ground
- jsr rand
- and #1
- beq @ground
- lda p2_on
- beq :+
- lda p2_len
- cmp #3
- bcc :+
- lda #ROW_Y(PLAT2_ROW)
- bne @soldier
- : lda p1_on
- beq @ground
- lda p1_len
- cmp #3
- bcc @ground
- lda #ROW_Y(PLAT1_ROW)
- bne @soldier
- @ground:
- lda g_on
- beq @drone
- lda g_len
- cmp #3
- bcc @drone
- lda #ROW_Y(GROUND_ROW)
- @soldier:
- sta o_y,x
- lda #T_SOLDIER
- sta o_type,x
- lda o_t,x
- and #31
- clc
- adc #20
- sta o_t,x ; first shot delay
- lda #SP_SOLD_A
- sta o_ptr,x
- lda #COL_SOLDIER
- sta o_col,x
- rts
- @drone:
- jsr rand
- and #63
- clc
- adc #56
- sta o_base,x
- sta o_y,x
- lda #T_DRONE
- sta o_type,x
- lda #0
- sta o_t2,x
- lda #SP_DRONE_A
- sta o_ptr,x
- lda #COL_DRONE
- sta o_col,x
- rts
-
- ; build_world: fresh generator state and a full screen of terrain
- build_world:
- lda #0
- sta fine
- sta fine_cur
- sta colpar
- sta p1_on
- sta p2_on
- sta b_h
- lda #1
- sta g_on
- sta g_age ; no left edge on column 0
- sta b_len
- lda #48 ; safe run-up: 40 columns + 8 ahead
- sta g_len
- lda #16
- sta p1_len
- lda #6
- sta p2_len
- lda #0
- sta gen_col
- : jsr gen_column
- jsr write_column
- inc gen_col
- lda gen_col
- cmp #40
- bne :-
- rts
-
- ; gen_column: build the next column of the world into colbuf[1..24]
- gen_column:
- ldx #24
- lda #' '
- : sta colbuf,x
- dex
- bne :-
-
- ; --- stars
- jsr rand
- and #7
- bne @nostar
- jsr rand
- and #7
- clc
- adc #2
- tax
- jsr rand
- and #1
- clc
- adc #CH_STAR1
- sta colbuf,x
- @nostar:
-
- ; --- skyline
- lda b_len
- bne @bcont
- lda b_h
- beq @newb
- lda #0 ; building ends: gap of 1-2
- sta b_h
- jsr rand
- and #1
- clc
- adc #1
- sta b_len
- bne @bcont
- @newb: jsr rand
- and #3
- clc
- adc #2
- sta b_h ; 2..5 rows
- jsr rand
- and #3
- clc
- adc #3
- sta b_len ; 3..6 wide
- @bcont: dec b_len
- lda b_h
- beq @nob
- lda #GROUND_ROW
- sec
- sbc b_h
- tax
- lda #CH_BTOP
- sta colbuf,x
- @bl: inx
- cpx #GROUND_ROW
- bcs @nob
- jsr rand
- and #1
- clc
- adc #CH_BLDA
- sta colbuf,x
- bne @bl
- @nob:
-
- ; --- low platform (row 17)
- lda p1_len
- bne @p1c
- lda p1_on
- eor #1
- sta p1_on
- beq @p1gap
- jsr rand
- and #3
- clc
- adc #4
- sta p1_len ; 4..7 long
- lda #0
- sta p1_age
- beq @p1c
- @p1gap: jsr rand
- and #7
- clc
- adc #4
- sta p1_len ; 4..11 gap
- @p1c: lda p1_on
- beq @p1off
- ldx #CH_PLM
- lda p1_age
- bne :+
- ldx #CH_PLL
- : lda p1_len
- cmp #1
- bne :+
- ldx #CH_PLR
- : stx colbuf+PLAT1_ROW
- inc p1_age
- @p1off: dec p1_len
-
- ; --- high platform (row 13): only above a low one, so it is reachable
- lda p2_len
- bne @p2c
- lda p2_on
- beq @p2try
- lda #0
- sta p2_on
- lda #6
- sta p2_len
- bne @p2c
- @p2try: lda p1_on
- beq @p2wait
- lda p1_len
- cmp #3
- bcc @p2wait
- jsr rand
- and #1
- beq @p2wait
- lda #1
- sta p2_on
- lda #0
- sta p2_age
- jsr rand
- and #3
- clc
- adc #3
- sta p2_len ; 3..6 long
- bne @p2c
- @p2wait:
- lda #1
- sta p2_len
- @p2c: lda p2_on
- beq @p2off
- ldx #CH_PLM
- lda p2_age
- bne :+
- ldx #CH_PLL
- : lda p2_len
- cmp #1
- bne :+
- ldx #CH_PLR
- : stx colbuf+PLAT2_ROW
- inc p2_age
- @p2off: dec p2_len
-
- ; --- ground and pits
- lda g_len
- bne @gc
- lda g_on
- eor #1
- sta g_on
- beq @gpit
- jsr rand
- and #7
- clc
- adc #6
- sta g_len ; 6..13 ground
- lda #0
- sta g_age
- beq @gc
- @gpit: jsr rand
- and #1
- clc
- adc #3 ; 3..4 pit
- ldx level
- cpx #4 ; C=1 from level 4 on: one column wider
- adc #0
- sta g_len
- @gc: lda g_on
- beq @gpitd
- lda colpar
- eor #1
- sta colpar
- tax
- lda grass_tab,x
- ldy dirt_tab,x
- sty tmp
- ldy dirt_tab+1,x ; other parity for the middle dirt row
- sty tmp2
- ldy g_age
- bne :+
- lda #CH_DL
- sta tmp
- sta tmp2
- lda #CH_GL
- : ldy g_len
- cpy #1
- bne :+
- lda #CH_DR
- sta tmp
- sta tmp2
- lda #CH_GR
- : sta colbuf+GROUND_ROW
- lda tmp
- sta colbuf+GROUND_ROW+1
- sta colbuf+GROUND_ROW+3
- lda tmp2
- sta colbuf+GROUND_ROW+2
- inc g_age
- @gpitd: dec g_len
- rts
-
- ; write_column: colbuf[1..24] -> screen column gen_col
- write_column:
- ldx #1
- : lda row_lo,x
- sta ptr3
- lda row_hi,x
- sta ptr3+1
- ldy gen_col
- lda colbuf,x
- sta (ptr3),y
- inx
- cpx #25
- bne :-
- rts
-
- ; 16-bit xorshift (John Metcalf), returns A; keeps X and Y
- rand:
- lda seed+1
- lsr
- lda seed
- ror
- eor seed+1
- sta seed+1
- ror
- eor seed
- sta seed
- eor seed+1
- sta seed+1
- rts
-
- ; ===========================================================================
- ; Screen
- ; ===========================================================================
- ; coarse scroll: rows 1..24, columns 0..38 <- 1..39, top to bottom.
- ; Three adjacent bytes per loop pass (39 = 3 x 13) keeps ahead of the beam.
- ; Must run strictly left to right, or it reads cells it already wrote.
- shift_screen:
- .repeat 24, R
- ldx #0
- : lda SCREEN+(R+1)*40+1,x
- sta SCREEN+(R+1)*40,x
- lda SCREEN+(R+1)*40+2,x
- sta SCREEN+(R+1)*40+1,x
- lda SCREEN+(R+1)*40+3,x
- sta SCREEN+(R+1)*40+2,x
- inx
- inx
- inx
- cpx #39
- bne :-
- .endrepeat
- rts
-
- init_colors:
- ldx #0
- @r: lda row_lo,x
- sta ptr
- lda row_hi,x
- clc
- adc #>(COLRAM-SCREEN)
- sta ptr+1
- lda row_color,x
- ldy #39
- : sta (ptr),y
- dey
- bpl :-
- inx
- cpx #25
- bne @r
- rts
-
- draw_hud:
- ldx #39
- lda #' '
- : sta SCREEN,x
- dex
- bpl :-
- SET_PTRS 0, 1, msg_score
- jsr print_str
- SET_PTRS 0, 16, msg_lives
- jsr print_str
- SET_PTRS 0, 28, msg_hi
- jsr print_str
- jsr draw_score
- lda lives
- ora #'0'
- sta SCREEN+22
- ldx #31
- lda h2
- jsr put_bcd
- lda h1
- jsr put_bcd
- lda h0
- jmp put_bcd
-
- draw_score:
- ldx #7
- lda s2
- jsr put_bcd
- lda s1
- jsr put_bcd
- lda s0
- ; fall through
- ; put_bcd: two BCD digits of A at row 0, column X; X += 2
- put_bcd:
- pha
- lsr
- lsr
- lsr
- lsr
- ora #'0'
- sta SCREEN,x
- pla
- and #$0F
- ora #'0'
- sta SCREEN+1,x
- inx
- inx
- rts
-
- ; add_score: A = BCD hundreds/thousands, Y = BCD tens/ones. Keeps nothing.
- add_score:
- sta tmp
- sed
- clc
- tya
- adc s0
- sta s0
- lda s1
- adc tmp
- sta s1
- lda s2
- adc #0
- sta s2
- cld
- jmp draw_score
-
- ; 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 white in hi-res (readable over the
- ; multicolor playfield)
- print_hires:
- jsr print_str
- lda ptr+1
- pha
- clc
- adc #>(COLRAM-SCREEN)
- sta ptr+1
- lda #1
- @c: dey
- bmi @x
- sta (ptr),y
- jmp @c
- @x: pla
- sta ptr+1
- rts
-
- ; ===========================================================================
- ; Sound
- ; ===========================================================================
- ; play_sfx: A = effect number. Keeps X.
- play_sfx:
- stx sfx_sx
- asl
- asl
- asl
- tay
- ldx sfx_tab,y ; voice 0..2
- lda sfx_tab+3,y
- sta sv_wave,x
- lda sfx_tab+4,y
- sta sv_freq,x
- lda sfx_tab+5,y
- sta sv_delta,x
- lda sfx_tab+6,y
- sta sv_t,x
- lda voice_off,x
- tax
- lda #0
- sta SID+4,x
- lda sfx_tab+1,y
- sta SID+5,x
- lda sfx_tab+2,y
- sta SID+6,x
- lda #0
- sta SID+0,x
- lda sfx_tab+4,y
- sta SID+1,x
- lda #$08
- sta SID+3,x ; 50% pulse width
- lda sfx_tab+3,y
- ora #1 ; gate on
- sta SID+4,x
- ldx sfx_sx
- rts
-
- update_sfx:
- ldx #2
- @l: lda sv_t,x
- beq @n
- dec sv_t,x
- bne @sweep
- ldy voice_off,x
- lda sv_wave,x ; gate off, release
- sta SID+4,y
- jmp @n
- @sweep: lda sv_freq,x
- clc
- adc sv_delta,x
- sta sv_freq,x
- ldy voice_off,x
- sta SID+1,y
- @n: dex
- bpl @l
- rts
-
- init_sid:
- ldx #$18
- lda #0
- : sta SID,x
- dex
- bpl :-
- lda #0
- sta sv_t
- sta sv_t+1
- sta sv_t+2
- lda #$0F ; volume 15
- sta SID+$18
- rts
-
- ; ===========================================================================
- ; Init
- ; ===========================================================================
- copy_charset:
- lda #$33 ; char ROM visible at $D000
- sta $01
- ldx #0
- : lda $D000,x ; ROM chars 0..63 (letters, digits)
- sta CHARSET,x
- lda $D100,x
- sta CHARSET+$100,x
- inx
- bne :-
- lda #$37
- sta $01
- ldx #0
- : lda custom_chars,x ; chars $40..$5F
- sta CHARSET+CUSTOM_FIRST*8,x
- inx
- bne :-
- rts
-
- ; ===========================================================================
- ; IRQs
- ; ===========================================================================
- irq_bottom:
- lda #$FF
- sta VIC_IRQ
- lda #$10 ; HUD row: no fine scroll
- sta VIC_CTRL2
- inc frame_flag
- lda #<irq_top
- sta IRQVEC
- 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
- lda #>irq_bottom
- sta IRQVEC+1
- lda #251
- sta VIC_RASTER
- jmp KERNAL_IRQ_EXIT
-
- ; ===========================================================================
- ; Data
- ; ===========================================================================
- .segment "RODATA"
-
- row_lo:
- .repeat 25, R
- .byte <(SCREEN + R*40)
- .endrepeat
- row_hi:
- .repeat 25, R
- .byte >(SCREEN + R*40)
- .endrepeat
-
- ; colour RAM per row: 0 HUD hi-res white, 1-10 stars white, 11-20 lit
- ; windows and platform tops yellow, 21 grass green, 22-24 dirt
- row_color:
- .byte 1
- .byte 9,9,9,9,9,9,9,9,9,9
- .byte 15,15,15,15,15,15,15,15,15,15
- .byte 13
- .byte 8,8,8
-
- grass_tab: .byte CH_G1, CH_G2
- dirt_tab: .byte CH_D1, CH_D2, CH_D1
-
- run_seq: .byte SP_PRUN_A, SP_PRUN_B, SP_PRUN_C, SP_PRUN_B
- ; indexed by timer/4: high = early
- expl_seq: .byte SP_EXPL_3, SP_EXPL_2, SP_EXPL_2, SP_EXPL_1
-
- ; per object type: none player pbul expl soldier drone eshot bomb
- type_mc: .byte 0, 1, 0, 1, 1, 1, 0, 0
- box_x0: .byte 0, 3, 0, 0, 3, 0, 0, 0
- box_w: .byte 0, 6, 4, 0, 6, 12, 2, 2
- box_y0: .byte 0, 2, 0, 0, 2, 3, 0, 0
- box_h: .byte 0, 18, 3, 0, 18, 10, 3, 5
-
- voice_off: .byte 0, 7, 14
-
- ; voice, AD, SR, waveform, start freq hi, freq step per frame, frames, pad
- sfx_tab:
- .byte 0, $08, $00, $80, $28, $FC, 5, 0 ; SFX_SHOT noise zap
- .byte 1, $06, $00, $10, $10, $03, 8, 0 ; SFX_JUMP triangle chirp
- .byte 2, $0A, $00, $80, $14, $FF, 18, 0 ; SFX_BOOM noise burst
- .byte 1, $05, $00, $20, $40, $FA, 6, 0 ; SFX_ESHOT saw zap
- .byte 2, $0C, $00, $80, $30, $FF, 40, 0 ; SFX_DIE long rumble
- .byte 1, $05, $00, $10, $30, $FE, 12, 0 ; SFX_BOMB falling whistle
-
- msg_score: .byte "SCORE", 0
- msg_lives: .byte "LIVES", 0
- msg_hi: .byte "HI", 0
- msg_title: .byte "B L A S T R U N N E R", 0
- msg_ctrl1: .byte "SPACE - JUMP CTRL - SHOOT", 0
- msg_ctrl2: .byte "A / D - MOVE (OR JOYSTICK 2)", 0
- msg_start: .byte "PRESS SPACE TO START", 0
- msg_over: .byte "GAME OVER", 0
- msg_again: .byte "PRESS SPACE TO PLAY", 0
-
- .assert * <= $2000, error, "program overlaps sprite data at $2000"
|