Hopefully this code makes some sense. They're surely not the best examples..

In both examples, $200-$2FF is a copy of sprite memory. First one is from Munchie Attack, sprite at $204 is the player and sprites at $240 and up are the food objects. The second one is from Roadkill (not released, a rip-off of the David Crane's game Freeway). It compares an 8x8 player to individual 8x8 sprites instead of actual objects. In both of these, the values added and subtracted are pretty specific to the object sizes in the games. So it might not seem intuitive, at least it didn't to me when I was writing it, it took a little experimentation.

In both cases, a successful hit detect means the code makes to the end of the routine.

**Code:**

hit_detection:

ldx #0

@hitloop:

lda $241,x

sta tiletype

bne :+

jmp @itsblank

:

lda $243,x

sec

sbc #8

cmp $207

bcc :+

jmp @itsblank

:

clc

adc #24

cmp $207

bcs :+

jmp @itsblank

:

lda $240,x

sec

sbc #15

cmp $204

bcc :+

jmp @itsblank

:

clc

adc #30

cmp $204

bcs :+

jmp @itsblank

:

lda #0

sta $241,x

sta $245,x

sta $249,x

sta $24D,x

**Code:**

;----------------------------------------------------------

; Detect Hits

;----------------------------------------------------------

hit_detect: ; detect vertical hits first

ldy #0

ldx #$0C

@check_next_v:

lda $201,x ; check to see if sprite tile is 0

beq @skip_one ; if it is, do not check

lda $200,x

sec

sbc #8

sta temp

lda $208 ; load Y value of player

cmp temp

bcc @skip_one

lda $200,x

clc

adc #8

sta temp

lda $208

cmp temp

bcc @hit1

@skip_one:

txa

clc

adc #24

tax

cpx #$84

bcc @check_next_v

rts

@hit1: ; a V hit as occured, check H position

@check_next_h:

lda $201,x

beq @skip_2

lda $203,x

sec

sbc #8

sta temp

lda $20B

cmp temp

bcc @skip_2

lda $203,x

clc

adc #8

sta temp

lda $20B

cmp temp

bcc @do_hit1

@skip_2:

inx

inx

inx

inx

bne @check_next_v

rts

@do_hit1:

lda #196

sta $208

lda #18

sta p1wait

rts