3

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?

Renat
  • 7,718
  • 2
  • 20
  • 34
Ben Klayer
  • 61
  • 6

2 Answers2

3

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);
Renat
  • 7,718
  • 2
  • 20
  • 34
  • Wow, you solved it! I never thought to look into drawing.h For this example, I can't use printf(). However, drawing.h has a function called gprintn() which prints a number at the position indicated by gotogxy(). Toyed around and it's now working perfectly. Thanks! – Ben Klayer Apr 18 '19 at 19:53
  • 1
    That was a blind guess actually, I just used full text search on the repository – Renat Apr 18 '19 at 19:54
1

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.

tadman
  • 208,517
  • 23
  • 234
  • 262