Patater

Projects Books Links Blarg About Donate

Day 9 - A Moving Sprite Program

Mike Huber

Published: 2005-Jul-21; Updated: 2010-Dec-18

What?

Today isn't really a tutorial, it's more the solution to the suggestion made yesterday about a program that allows the user to move the sprite. I believe that we have infact covered everything necessary to make a Moving Sprite Program. So, the next section will be the complete code file, and that'll be all for today.

Da Code!

;;--- CODE START ---;; .inesprg 1 .inesmap 0 .inesmir 1 .ineschr 1

.bank 1
.org $FFFA
.dw 0        ; no VBlank
.dw Start    ; address to execute on reset
.dw 0        ; no whatever

.bank 0
.org $0000 X_Pos   .db 20       ; a X position for our sprite, start at 20 Y_Pos   .db 20       ; a Y position for our sprite, start at 20

.org $8000  ; code starts at $8000 or $C000 Start:

lda #%00001000  ;
sta $2000       ;
lda #%00011110  ; Our typical PPU Setup code.
sta $2001       ;

ldx #$00    ; clear X            ;; start of palette loading code

lda #$3F    ; have $2006 tell
sta $2006   ; $2007 to start
lda #$00    ; at $3F00 (palette).
sta $2006

loadpal: ; this is a freaky loop lda tilepal, x ; that gives 32 numbers sta $2007 ; to $2007, ending when inx ; X is 32, meaning we cpx #32 ; are done. bne loadpal ; if X isn’t =32, goto “loadpal:” line. ;; end of palette loading code

infinite: ; a label to start our infinite loop waitblank: lda $2002 ; these 3 lines wait for VBlank, this loop will actually miss VBlank bpl waitblank ; alot, in a later Day, I’ll give a better way.

lda #$00   ; these lines tell $2003
sta $2003  ; to tell
lda #$00   ; $2004 to start
sta $2003  ; at $0000.

lda Y_Pos  ; load Y value
sta $2004 ; store Y value

lda #$00  ; tile number 0
sta $2004 ; store tile number

lda #$00 ; no special junk
sta $2004 ; store special junk

lda X_Pos  ; load X value
sta $2004 ; store X value
; and yes, it MUST go in that order.

lda #$01   ; these
sta $4016  ; lines
lda #$00   ; setup/strobe the
sta $4016  ; keypad.

lda $4016  ; load Abutton Status ; note that whatever we ain't interested
lda $4016  ; load Bbutton Status ; in we just load so it'll go to the next one.
lda $4016  ; load Select button status
lda $4016  ; load Start button status
lda $4016  ; load UP button status
and #1     ; AND status with #1
bne UPKEYdown  ; for some reason (not gonna reveal yet), need to use NotEquals
;with ANDs. So it'll jump (branch) if key was down.

lda $4016  ; load DOWN button status
and #1     ; AND status with #1
bne DOWNKEYdown

lda $4016  ; load LEFT button status
and #1     ; AND status with #1
bne LEFTKEYdown

lda $4016  ; load RIGHT button status
and #1     ; AND status with #1
bne RIGHTKEYdown
jmp NOTHINGdown  ; if nothing was down, we just jump (no check for conditions)
; down past the rest of everything.

UPKEYdown: lda Y_Pos ; load A with Y position sbc #1 ; subtract 1 from A. Only can do math on A register. SBC (Subtract with Borrow) sta Y_Pos ; store back to memory jmp NOTHINGdown ; jump over the rest of the handling code.

DOWNKEYdown: lda Y_Pos adc #1 ; add 1 to A. ADC (Add with Carry)((to A register)) sta Y_Pos jmp NOTHINGdown ; jump over the rest of handling code.

LEFTKEYdown: lda X_Pos sbc #1 sta X_Pos jmp NOTHINGdown ;the left and right handling code does the same as UP and Down except.. well.. with ; left and right. :)

RIGHTKEYdown: lda X_Pos adc #1 sta X_Pos ; don’t need to jump to NOTHINGdown, it’s right below. Saved several bytes of ; PRG-Bank space! :)

NOTHINGdown: jmp infinite

tilepal: .incbin “our.pal” ; a label for our palette data

.bank 2
.org $0000
.incbin "our.bkg"
.incbin "our.spr"

;;— END OF CODE FILE —;; </code>

Hopefully you still have, the our.pal,our.bkg,our.spr files from the sprite day. You should understand all of this as I have (or atleast I think I have) covered everything necessary (to be able to do this).

Note: In some emulators, you may have to hit down a few times before the sprite appears. I don't know why this happens.

This Day In Review

Thanks to the people who sent me their own implementation of the Moving Sprite Program. Atleast now I know someone reads this.

Happy coding,
-Mike H a.k.a GbaGuy