简体   繁体   English

“\\ r”字面打印回车符号

[英]“\r” Literally Prints Carriage Return Symbol

I'm teaching myself the C programming language. 我正在自学C语言。 Except I'm learning in context of designing a Gameboy game (using GBDEK). 除了我在设计Gameboy游戏(使用GBDEK)的背景下学习。

I'm working on a simple Breakout clone, and have decided to use the printf() function to show the player's score. 我正在研究一个简单的Breakout克隆,并决定使用printf()函数来显示玩家的分数。 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. 但是,它会打印回车符号(一个奇怪的CR符号)并且不会删除之前的分数。 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) : drawing.h中有gotogxy函数(假定它是问题中使用的GBDK源代码):

/* 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: 您可以尝试在printf之前使用它:

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. 您可能需要使用其他一些字符定位代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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