简体   繁体   中英

“\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.

在此输入图像描述

I'm unsure how to fix this. Help?

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);

Carriage returns are just characters unless interpreted by the terminal as something else. It seems to be the case that this the carriage return is meaningless.

You may need to use some other character positioning code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM