DEV Community

Draculinio
Draculinio

Posted on

Writing a NES game, day 13, no more moondance

Up until now, when I moved left or right, the character was just changing the positions, but the character itself didn't change at all, when it was going left it just felt like "moonwalking". At least I need the character to be able to look to another direction when waking left.
One problem with that is that creating new sprites for that can be kind of a "waste" because space is very limited, so, what I need to do is reuse the same sprites and flip them.

First in ZEROPAGE segment I need a new variable which is for the direction, so 0 is right, 1 is left.

pdir: .res 1 ;player direction 00 right, 01 left 
Enter fullscreen mode Exit fullscreen mode

The second thing that I need is to update the value every time **moveLeft **or **moveRight **is called

moveLeft: LDA p1x SEC SBC #01 STA p1x LDA p2x SEC SBC #01 STA p2x LDA #$01 STA pdir RTS moveRight: LDA p1x CLC ADC #01 STA p1x LDA p2x CLC ADC #01 STA p2x LDA #$00 STA pdir RTS 
Enter fullscreen mode Exit fullscreen mode

And finally, this will affect the update of the sprites, so I needed to do some changes here for the character part:

 LDA #$00 CMP pdir BEQ rightCharTiles LDA #$01 STA $0201 LDA #$00 STA $0205 LDA #$11 STA $0209 LDA #$10 STA $020D LDA #$40 STA $0202 STA $0206 STA $020A STA $020E JMP charTilesDone rightCharTiles: LDA #$00 STA $0201 LDA #$01 STA $0205 LDA #$10 STA $0209 LDA #$11 STA $020D LDA #$00 STA $0202 STA $0206 STA $020A STA $020E charTilesDone: LDA p1y STA $0200 STA $0204 LDA p2y STA $0208 STA $020C LDA p1x STA $0203 STA $020B LDA p2x STA $0207 STA $020F 
Enter fullscreen mode Exit fullscreen mode

Now I can turn the character around:

Image description

As always, the code can be found at my github repo.

Top comments (0)