Display score using background tile

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Display score using background tile
by on (#50322)
Hello everybody. Thanks to bunnyboys nerdy nights over at NA, I've been doing a attempt at homebrewing.
The tutorials started with how to make a PONG game. I started from there and made a closed area where the ball keeps bouncing around.

The speed of my ball is adjustable (1-6). I can adjust this speed with the UP and Down buttons.
To display the speed I changed the background tile in the NMI. This semi-works. Before I change the speed (up or down), it displays 1.
After I change the speed, it shoves the "1" one place to the right and displays the actual speed (1 to 6).

Image

This happens only one time. The number 1 just shoves over to the right and the new number is adjustable (changing the speed - as intended)
Anyone have any idea what's wrong ? Here is my code for displaying the speed: http://nintendoage.pastebin.com/f61fb15b5
The "Drawscore:" gets called upon in the NMI.


--------------


Also, would it be better if I removed the highlighted pieces of codes (in the pastebin) and add a Jump after a compare (if the compare = 1) ?
In the below example, first it would load A with the ball speed. If it's not equal to six, it will jump to the next value (NotSix). A is still loaded with the ball speed, so there is no need to load it again ?
If the value is equal to six, the Jump is then used to skip unnecessary code and to prevent that the tile number stored in A get's compared with other speeds (5-1)

So you would get:

Drawscore:

LDA #$23 ;load the first part of the location for the score
STA $2006 ; store into input port of PPU
LDA #$53 ;load the second part of the location for the score
STA $2006 ; store again

    LDA ballspeedx
    CMP #$06
    BNE NotSix
    LDA #$42
    STA $2007
    JMP DrawScoreDone ;if the speed of the ball is six => no need to do 5-1, jump to bottom
NotSix:
    LDA ballspeedx ; this rule gets deleted, because if it's not 6, no other value was stored in A
    CMP #$05
    BNE NotFive
    LDA #$51
    STA $2007
    JMP DrawScoreDone

NotFive:
.....
.....
DrawScoreDone:

by on (#50325)
Note that after each write to $2007 you don't skip over the other compares, so even after you've found your number and drawn it, you will still compare for every possible digit after the one that got written. All compares will fail and nothing will get drawn until the #2, but since you don't specifically check for #1 (it just gets drawn if the number is not #2), it always gets written at the end. Your bug will cause 1 to be written after every number that's not 1.

The quickest way to fix it would be to put an RTS after each write to $2007, since the routine doesn't do anything after drawing the tile.

However, that's a very clumsy way to draw numbers IMO. Ideally you'd have all your numbers stored sequentially in the pattern tables, so drawing any number would be a matter of adding it to the index of the tile that contains the digit "0".

Code:
   clc
   lda ballspeedx
   adc #INDEX_OF_ZERO
   sta $2007

You'd just have the above instead of all those compares and branches.

EDIT: If you must keep the tiles ordered like that, you can still use my suggestion if you use a small table:

Code:
table:
   .db $2d, $40, $41, $50, $51, $42


Then you could use the following after setting the output address:

Code:
   ldx ballspeedx
   lda table-1, x
   sta $2007

by on (#50327)
tokumaru wrote:
Note that after each write to $2007 you don't skip over the other compares, so even after you've found your number and drawn it, you will still compare for every possible digit after the one that got written. All compares will fail and nothing will get drawn until the #2, but since you don't specifically check for #1 (it just gets drawn if the number is not #2), it always gets written at the end. Your bug will cause 1 to be written after every number that's not 1.



I see....
if my speed would change, it would first do a write to $2007 displaying the number 2 (at the address I wanted = #$2353).
And because I do not compare at the bottom (speed 1), it would do another write to $2007.
Making 1 appear next to my speed (at the next address = #$2354).

And if my speed is changed back to 1, the tile number at #$2354 doesn't get changed, causing the "1" to stay there.
I added a compare and a BNE for speed 1 and it works now.


But I'm going to use your suggestion anyways. It's shorter and cleaner and easier :D
Thanks a lot !