;Assemble IT ;Sprite to background collision ;example game: ; ;Give The Dog A Bone ;By Richard Bayliss !to "gtdab.prg",cbm ;Global labels/constants objpos = $0370 ;Sprite x,y positions gamesync = $0380 ;gamesynchronize ;a delay so we have ;a smooth interrupt ;outside the IRQ collision = $0390 chrptr = $02 sprptr = $03 dogd = $04 ;Dog down animation dogu = $05 ;'' up animation dogl = $06 ;'' left animation dogr = $07 ; right animation dogscore = $0407 ;Player score clockdelay = $09 ;Delay for clock clock3 = $0422 ;Last digit on clock clock2 = $0421 ;Second digit on clock clock1 = $0420 ;First digit on clock ;Basic starter *=$0800 !basic 2018,$4000 ;sys16384 ;Music * = $1000 !binary "bin/music.prg",,2 ;sprites * = $2000 !binary "bin/sprites.prg",,2 ;charset * = $2800 !binary "bin/charset.prg",,2 ;screendata * = $3000 !binary "bin/screen.prg",,2 * = $3400 ;colourdata !binary "bin/screencolour.prg",,2 *= $4000 sei lda #$00 sta chrptr ;Init chrptr so that ;when a char ;animation is in ;process, it does ;not delay the anim ;process sta sprptr ;Init sprite anim ;pointer sta clockdelay ;Init timer ;Green screen lda #$0d sta $d020 lda #$05 sta $d021 lda #$08 sta $d016 ;Draw the default game screen into ;screen RAM $0400. ldx #$00 drawscr lda $3000,x sta $0400,x lda $3100,x sta $0500,x lda $3200,x sta $0600,x lda $32e8,x sta $06e8,x lda $3400,x sta $d800,x lda $3500,x sta $d900,x lda $3600,x sta $da00,x lda $36e8,x sta $dae8,x inx bne drawscr ;Copy the status on to the screen memory ldx #$00 copystat lda status,x cmp #$3b bcc noinv sec sbc #$40 noinv sta $0400,x lda stat2,x cmp #$3b bcc noinv2 sec sbc #$40 noinv2 sta $07c0,x inx cpx #$28 bne copystat ;Now we want our dog and bone sprites ;set and positioned. ;Have only 2 sprites on. ;One for the dog, another ;for the bone lda #$03 sta $d015 ;Default sprite as dog lda #$80 sta $07f8 ;Default sprite as bone lda #$88 sta $07f9 ;Position the dog lda #$12 sta objpos+$00 lda #$48 sta objpos+$01 lda #$72 sta objpos+$02 lda #$88 sta objpos+$03 ;Set the sprite colours for the two ;sprites lda #$00 ;Black dog sta $d027 lda #$01 sta $d028 ;White bone ;Make our interrupt lda #irq1 sta $0314 stx $0315 lda #$00 sta $d012 lda #$7f sta $dc0d lda #$1b sta $d011 lda #$1a sta $d018 lda #$01 sta $d01a lda #$00 jsr $1000 cli mainloop lda #$00 sta gamesync cmp gamesync beq *-3 jsr expand ;Call expansion rt jsr readjoy ;Call joy read rt jsr bgrcol ;Call bgr.coll rt jsr charanim ;Call anim.chr rt jsr spranim ;Call spr.anim rt jsr sprcol ;Sprite/sprite coll. jsr time ;Game time jmp mainloop ;Expand the sprite x position for only ;the first sprite. Only 2 sprites ;for now. expand ldx #$00 eploop lda objpos+$01,x sta $d001,x lda objpos+$00,x asl a ror $d010 sta $d000,x inx inx cpx #$10 bne eploop rts irq1 inc $d019 lda #$01 sta gamesync jsr $1003 jmp $ea7e ;Move the square slowly around the ;screen with a joystick plugged into ;port 2 readjoy lda $dc00 up lsr a ;Remove 'a' if not bcs down ;using Turbo Assembler ldx objpos+$01 dex dex stx objpos+$01 lda dogu sta $07f8 rts down lsr a bcs left ldx objpos+$01 inx inx stx objpos+$01 lda dogd sta $07f8 rts left lsr a bcs right ldx objpos+$00 dex stx objpos+$00 lda dogl sta $07f8 rts right lsr a bcs fire ldx objpos+$00 inx stx objpos+$00 lda dogr sta $07f8 fire rts ;Ignore firebutton as ;we don't really need ;it. ;The hardware sprite/background ;collision routine bgrcol lda $d01f;Hardware detect lsr a ;If sprite 1 touches bcs hit ;visible char then ;collision detected. ;else sprite stays white lda #$00 sta $d027 rts ;Sprite hits a visible char so for now ;we'll make the sprite flash. hit ldx #$00 hitloop ldy #$00 hitloop2 inc $d027 dec $d027 iny bne hitloop2 inx bne hitloop lda #$12 sta objpos+$00 lda #$42 sta objpos+$01 rts ;Our charset animation routine. charanim lda chrptr cmp #$0c ;Our actual delay beq enddelay ;for the anim inc chrptr ;basically, the rts ;speed. enddelay lda #$00 sta chrptr ldx #$00 wrap1 lda $2a00,x ;Copy the whole ;char data and sta $2a40,x ;paste it to $2040 inx ;8 times cpx #$08 bne wrap1 ldx #$00 wrap2 lda $2a08,x ;Copy chars from sta $2a00,x ;$2a08 to $2a00 inx ;for a perfect cpx #$40 ;working charset bne wrap2 ;animation. rts ;The sprite animation calulation routine spranim ldx sprptr lda dogdown,x sta dogd lda dogup,x sta dogu lda dogleft,x sta dogl lda dogright,x sta dogr inx cpx #$0a ;Or #10 beq resetsp inc sprptr rts resetsp ldx #0 stx sprptr rts ;Sprite to sprite collision for the dog ;getting the bone. sprcol lda objpos+$00 ;Dog x posit sec sbc #$06 sta collision+$00 clc adc #$0c sta collision+$01 lda objpos+$01 sec sbc #$0c sta collision+$02 clc adc #$18 sta collision+$03 ;Check if player touches the bone. lda objpos+$02 cmp collision+$00 bcc nobone cmp collision+$01 bcs nobone lda objpos+$03 cmp collision+$02 bcc nobone cmp collision+$03 bcs nobone lda #$00 sta objpos+$02 sta objpos+$03 sta $d002 sta $d003 jmp gamedone nobone rts ;The countdown clock for the game time time inc clockdelay lda clockdelay cmp #$32 beq dotimer rts dotimer lda #$00 sta clockdelay ;Make last digit of clock count down dec clock3 ;Subtract one digit lda clock3 cmp #$2f ;If below char '0' bne clok ;Else clock is ok lda #$39 ;Last digit is '9' sta clock3 ;Make second but last digit of clock ;count down dec clock2 lda clock2 ;As above, but cmp #$2f ;with second digit bne clok lda #$39 sta clock2 ;Make first digit of clock count down dec clock1 lda clock1 cmp #$2f bne clok lda #$30 sta clock1 jmp gameover ;Clock has run out clok rts ;Game over sequence gameover ldx #$00 clrscr lda gomessage,x cmp #$3b bcc gook sec sbc #$40 gook sta $05e0,x lda #$0d sta $d9e0,x inx cpx #$28 bne clrscr lda #$00 sta $d015 wait lda $dc00 lsr a lsr a lsr a lsr a lsr a bcs wait jmp $4000 ;Loop ;Level is complete so add what is left ;from the time on to the player's score ;as a bonus gamedone lda #$00 sta $d015 bonus lda #$00 sta gamesync lda gamesync cmp gamesync beq *-3 jsr score jsr score jsr score jsr score jsr score dec clock3 lda clock3 cmp #$2f bne bonus lda #$39 sta clock3 dec clock2 lda clock2 cmp #$2f bne bonus lda #$39 sta clock2 dec clock1 lda clock1 cmp #$2f bne bonus lda #$30 sta clock3 sta clock2 sta clock1 jmp welldone ;Score adding routine score inc dogscore+4 ldx #$05 scloop lda dogscore+0,x cmp #$3a bne scoreok lda #$30 sta dogscore+0,x inc dogscore-1,x scoreok dex bne scloop rts welldone ldx #$00 wdmess lda wdmessage,x cmp #$3b bcc wdok sec sbc #$40 wdok sta $05e0,x lda #$0d sta $d9e0,x inx cpx #$28 bne wdmess fire3 lda $dc00 lsr a lsr a lsr a lsr a lsr a bcs fire3 jmp $4000 ;Temporary ;Data tables for the dog animation dogdown !byte $80,$80,$80,$80,$80 !byte $81,$81,$81,$81,$81 !byte 0 dogup !byte $82,$82,$82,$82,$82 !byte $83,$83,$83,$83,$83 !byte 0 dogleft !byte $84,$84,$84,$84,$84 !byte $85,$85,$85,$85,$85 dogright !byte $86,$86,$86,$86,$86 !byte $87,$87,$87,$87,$87 !byte 0 ;The status screen !ct scr status !text "score: 000000 ------" !text "--- time: 120 tnd" stat2 !text "give that dog a bone " !text "by richard bayliss!" wdmessage !text "well done, you have " !text "made it. press fire!" gomessage !text "bad luck, time has r" !text "an out. try again !!"