0

I received an assignment to build the Snake game, and I provided with the following 'gotoxy' function:

void gotoxy(int y, int x) {
    printf("\x1b[%d;%df", x + 1, y + 1);
}

which i should use to print the borders and the snake on the console.

First, I tried to print the borders of the game, but when I run the code the console prints this:

Screenshot of the console.

The printing borders functions:

void printBorders(){
    int initX = 0;
    int initY = 7;
    printCols(initX, initY);
    printLines(initX, initY);

    printCols(initX, initY + 25);
    printLines(initX + 75, initY);
    return;
}

void printCols(int x, int y){
    while(x <75)
    {
        gotoxy(x, y);
        printf("#");
        x++;
    }
    return;
}

void printLines(int x, int y){
    while (y < 32)
    {
        gotoxy(x, y);
        y++;
        printf("#");
    }

But I don't know what is the problem and how to fix it, thank you for your help.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Yair
  • 35
  • 3
  • Is there a reason why you're not using a library like curses? – Barmar Jan 07 '21 at 06:05
  • Whatever console you're using does not recognise the ANSI escape sequences. See https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences – paxdiablo Jan 07 '21 at 06:07
  • Try using `\x1b[%d;%dH` instead of `\x1b[%d;%df` – Barmar Jan 07 '21 at 06:08
  • It seems as if you are trying this in a Windows' cmd. This primitive console supports just very few commands. You might like to call the system's API directly, start reading at [Console Virtual Terminal Sequences](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences) – the busybee Jan 07 '21 at 07:23

0 Answers0