c - SetConsoleCursorPosition : Getting black rows in console upon use

标签 c windows console cursor-position

关注我的 previous question为了让 snake 程序更流畅,我去尝试了 ANSI 转义序列和控制台函数,将文本光标放回左上角。

我想让光标位于屏幕的左上角,但是当它这样做时,我每隔一行在屏幕上出现黑色条纹。

我在 Windows 环境中工作,为 Windows 控制台制作这个程序。

这是绘画功能:

    void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
    //system("cls");
    //printf("\033[1;1H");
    COORD destCoord;
    destCoord.X = 0;
    destCoord.Y = 0;
    SetConsoleCursorPosition(hconsole, destCoord);
    for (int i = 0; i < ligneMax; i++)
    {
        for (int j = 0; j < colonneMax; j++)
        {
            printf("%c", tab[i][j]);
        }
        printf("\n");
    }
}

我尝试在调用 paint 函数之前将转义代码和控制台函数放在 main 中,但我得到了相同的结果。

如果有人想测试,这里是整个程序:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>

void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
    //system("cls");
    //printf("\033[1;1H");
    COORD destCoord;
    destCoord.X = 0;
    destCoord.Y = 0;
    SetConsoleCursorPosition(hconsole, destCoord);
    for (int i = 0; i < ligneMax; i++)
    {
        for (int j = 0; j < colonneMax; j++)
        {
            printf("%c", tab[i][j]);
        }
        printf("\n");
    }
}

void create(int tab[28][120], int Nbligne, int Nbcolonne, 
            int randligne, int randcols)
{
    for (int i = 0; i < Nbligne; i++)
    {
        for (int j = 0; j < Nbcolonne; j++)
        {
            tab[i][j] = ' ';
            if (i == 0 || i == Nbligne - 1)
                tab[i][j] = 205;
            if (j == 0 || j == Nbcolonne - 1)
                tab[i][j] = 186;
            if (i == 0 && j == 0)
                tab[i][j] = 201;
            if (i == 0 && j == Nbcolonne - 1)
                tab[i][j] = 187;
            if (i == Nbligne - 1 && j == 0)
                tab[i][j] = 200;
            if (i == Nbligne - 1 && j == Nbcolonne - 1)
                tab[i][j] = 188;
            if (i == 14 && j == 60)
                tab[i][j] = 219;
            if (i == 14 && j == 59)
                tab[i][j] = 79;
            if (i == 14 && j == 58)
                tab[i][j] = 35;
            if (i == randligne && j == randcols)
                tab[i][j] = 176;
        }
    }
}

void destroyTail(int tab[28][120], int Nbligne, int Nbcolonne) 
{
    for (int i = 0; i < Nbligne; i++)
    {
        for (int j = 0; j < Nbcolonne; j++)
        {
            if (tab[i][j] == 35)
            {
                tab[i][j] = ' ';
                if (tab[i][j + 1] == 79)
                    tab[i][j + 1] = 35;
                else if (tab[i][j - 1] == 79)
                    tab[i][j - 1] = 35;
                else if (tab[i + 1][j] == 79)
                    tab[i + 1][j] = 35;
                else if (tab[i - 1][j] == 79)
                    tab[i - 1][j] = 35;
                goto stop;
            }
        }
    }
    stop: NULL;
}

void translate(int tab[28][120], char direction, int Nbligne, int Nbcolonne)
{
    for (int i = 0; i < Nbligne; i++)
    {
        for (int j = 0; j < Nbcolonne; j++)
        {
            if (tab[i][j] == 219)
            {
                if (direction == 'R')
                {
                    tab[i][j] = 79;
                    tab[i][j + 1] = 219;
                }
                if (direction == 'D')
                {
                    tab[i][j] = 79;
                    tab[i + 1][j] = 219;
                }
                if (direction == 'L')
                {
                    tab[i][j] = 79;
                    tab[i][j - 1] = 219;
                }
                if (direction == 'U')
                {
                    tab[i][j] = 79;
                    tab[i - 1][j] = 219;
                }
                goto stop;
            }
        }
    }
    stop: NULL;
}

int checkExpand(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
    for (int i = 0; i < Nbligne; i++)
    {
        for (int j = 0; j < Nbcolonne; j++)
        {
            if ((direction == 'R' && tab[i][j] == 219 && tab[i][j + 1] == 176) ||
                (direction == 'L' && tab[i][j] == 219 && tab[i][j - 1] == 176) ||
                (direction == 'U' && tab[i][j] == 219 && tab[i - 1][j] == 176) ||
                (direction == 'D' && tab[i][j] == 219 && tab[i + 1][j] == 176))
                return 1;
        }
    }
    return 0;
}

int checkDeath(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
    for (int i = 0; i < Nbligne; i++)
    {
        for (int j = 0; j < Nbcolonne; j++)
        {
            if ((direction == 'R' && tab[i][j] == 219 && (tab[i][j + 1] == 186 || tab[i][j + 1] == 79)) ||
                (direction == 'L' && tab[i][j] == 219 && (tab[i][j - 1] == 186 || tab[i][j - 1] == 79)) ||
                (direction == 'U' && tab[i][j] == 219 && (tab[i - 1][j] == 205 || tab[i - 1][j] == 79)) ||
                (direction == 'D' && tab[i][j] == 219 && (tab[i + 1][j] == 205 || tab[i + 1][j] == 79)))
                return 1;
        }
    }
    return 0;
}

int main()
{
    HANDLE  hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, 241);

    int tab[28][120];
    int randligne = rand() % 26 + 1;
    int randcols = rand() % 118 + 1;
    create(tab, 28, 120, randligne, randcols);
    paint(tab, 28, 120, hConsole);
    char i = '1';
    char direction = 'R';
    int eaten = 0;
    int expand = 0;
    int death = 0;
    while(i != 'k')
    {
        if (kbhit())
            i = getch();
        switch(i) {
            case 'z':
                if (direction != 'D')
                    direction = 'U';
                break;
            case 's':
                if (direction != 'U')
                    direction = 'D';
                break;
            case 'd':
                if (direction != 'L')
                    direction = 'R';
                break;
            case 'q':
                if (direction != 'R')
                    direction = 'L';
                break;
        }
        randligne = rand() % 26 + 1;
        randcols = rand() % 118 + 1;
        death = checkDeath(tab, 28, 120, direction);
        if (death == 1)
            break;
        translate(tab, direction, 28, 120);
        expand = checkExpand(tab, 28, 120, direction);
        if (expand == 0)
            destroyTail(tab, 28, 120);
        else
        {
            while (tab[randligne][randcols] != ' ')
            {
                randligne = rand() % 26 + 1;
                randcols = rand() % 118 + 1;
            }
            tab[randligne][randcols] = 176;
            eaten++;
        }
        printf("Number of biscuits eaten : %d ; direction : %c ; expand : %d", eaten, direction, expand);
        paint(tab, 28, 120, hConsole);
        Sleep(100);
    }
    while(i != 'k')
    {
        if (kbhit())
            i = getch();
    }
}

最佳答案

代码太多,无法快速理解。如果你有蛇的 Windows 控制台输出,你不能简单地在下一行 printf 并将它留在那儿吗?

如果你想隐藏光标(而不是试图把它停在看不见的地方)你可以通过调用SetConsoleCursorInfo让它不可见。并且传递的 structshown here .

关于c - SetConsoleCursorPosition : Getting black rows in console upon use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50192096/

相关文章:

c# - 如何拥有适用于控制台和 wpf 的 calibburn 应用程序?

c - 如何在 C 中使用读写过去的 BUFSIZ

c - c中这些数组的值(value)是什么?

c - 为什么 fgetc 或 fgets 忽略

windows - 在 Windows 上发送数据报时出现 EACCES 错误

c++ - 在 Visual C++ 2008/2010 Express 中编写 Linux 控制台应用程序

c - 打印结构体的值不显示正确的值

c - C如何打印一个超大数?

c - 如何在 Windows 中设置 Turbo C 路径?

c# - 我可以让控制台显示中文吗?