cprintfcarriage-returngameboygbdk

"\r" Literally Prints Carriage Return Symbol


I'm teaching myself the C programming language. Except I'm learning in context of designing a Gameboy game (using GBDEK).

I'm working on a simple Breakout clone, and have decided to use the printf() function to show the player's score. When the player's score increases, the displayed score should obviously change too. Here is the relevant code:

int score = 0;

void main() {
   printf(" \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n%d", score);
}

void moveBall() {
    if((ballY == paddleY-8) && (ballX >= paddleX-8) && (paddleX+24 >= ballX-8)) {
       score+=10;           
       printf("\r%d", score);
    }
}

When the game starts, the console prints a bunch of empty lines to position the score. When the score changes (in this case, when the ball hits the paddle), it should return to the beginning of the line and print a new number. However, it prints the Carriage Return symbol (a weird CR symbol) and doesn't erase the previous score. Here is a screenshot to show you what I mean.

enter image description here

I'm unsure how to fix this. Help?


Solution

  • There is gotogxy function in drawing.h (presume it's GBDK source codes used in the question) :

    /* Sets the current text position to (x,y).  Note that x and y have units
       of cells (8 pixels) */
    void
        gotogxy(UBYTE x, UBYTE y);
    

    You can try to use it before printf like:

    gotogxy(0,7);
    printf("%d", score);