首页 > 解决方案 > SetConsoleCursorPosition 在 C 中无法正常工作:字符在随机位置打印

问题描述

我正在尝试制作一个简单的控制台游戏引擎,能够在指定位置显示字符数组。目前,一切正常,但控制台不想配合:

我使用 SetConsoleCursorPosition 函数(从Windows.h)为了在控制台中设置正确的位置,并putchar()为了一个一个地打印字符(我也测试过putc()fputc()_putch(),但它没有帮助)

为了测试,我设置了简单的程序。在GraphicsEngine.h

void setCoordinates(int x, int y) {
    COORD cursorPos;
    cursorPos.X = x;
    cursorPos.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPos);
}

void printObject(const unsigned char* object, int x, int y, int* xOld, int* yOld) {

    int i = 0;
    int j = 0;
    *xOld = x;
    *yOld = y;
    while (object[i] != '\t') {
        putchar(object[i]);
        fflush(stdout);
        if (object[i] == '\n') { //if approached newline, travel one line down
            y++;
            setCoordinates(x, y);
        }
        i++;
    }
}

main.c

    int x = 0;
    int y = 10;
    int xOld = 0;
    int yOld = 0;
    setCoordinates(x, y);
    printObject(ship, x, y, &xOld, &yOld, true);

我传递给printObject()函数的数组:

const unsigned char ship[] = 
{
32,32,64,'\n',
32,47,'#',92,'\n',
174,'#','#','#',175,'\n','\t'
};

例如,给定 x = 20 和 y = 20 代码似乎可以正确打印船: 看起来不错

但通常(随机,有时每次,有时每 10 次启动)打印的数组完全无序:

看起来很糟糕

这是一个大问题,因为我想制作一款允许玩家在 2D 平面(x 轴和 y 轴)上移动的游戏,并且在多次刷新屏幕时,一些角色会出现一些故障。

标签: cwindowswinapiconsole

解决方案


您的线程缺少可以重现问题的代码。我修改了您的代码并添加了部分代码,现在它应该可以满足您的需求。

#include <iostream>
#include <Windows.h>

const unsigned char ship[] =
{
32,32,64,'\n',
32,47,'#',92,'\n',
174,'#','#','#',175,'\n','\t'
};

const unsigned char ship_remove[] =
{
32,32,32,'\n',
32,32,32,32,'\n',
32,32,32,32,32,'\n','\t'
};

void hidecursor()
{
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
}

void setCoordinates(int x, int y) {
    COORD cursorPos;
    cursorPos.X = x;
    cursorPos.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPos);
}

void printObject(const unsigned char* object, int x, int y, int *xOld, int *yOld) {

    int i = 0;
    *xOld = x;
    *yOld = y;
    while (object[i] != '\t') {
        char r = object[i];
        putchar(object[i]);
        fflush(stdout);
        if (object[i] == '\n') { //if approached newline, travel one line down
            y++;
            setCoordinates(x, y);
        }
        i++;
    }
}

void clear(const unsigned char* object, int x, int y)
{
    setCoordinates(x, y);
    int i = 0;
    while (object[i] != '\t') {
        char r = object[i];
        putchar(object[i]);
        fflush(stdout);
        if (object[i] == '\n') { //if approached newline, travel one line down
            y++;
            setCoordinates(x, y);
        }
        i++;
    }
}

int main()
{  
    hidecursor();
    int x = 20;
    int y = 20;
    int xOld = 0;
    int yOld = 0;
    setCoordinates(x, y);
    printObject(ship, x, y, &xOld, &yOld);
    while (1)
    {
        if (GetAsyncKeyState(0x41) & 0x0001)
        {           
            clear(ship_remove, xOld, yOld);
            x = xOld;
            y = yOld;
            setCoordinates(x - 1, y);
            printObject(ship, x - 1, y, &xOld, &yOld);
        }
        if (GetAsyncKeyState(0x44) & 0x0001)
        {
            clear(ship_remove, xOld, yOld);
            x = xOld;
            y = yOld;
            setCoordinates(x + 1, y);
            printObject(ship, x + 1, y, &xOld, &yOld);
        }
        if (GetAsyncKeyState(0x57) & 0x0001)
        {
            clear(ship_remove, xOld, yOld);
            x = xOld;
            y = yOld;
            setCoordinates(x, y - 1);
            printObject(ship, x, y - 1, &xOld, &yOld);
        }
        if (GetAsyncKeyState(0x53) & 0x0001)
        {
            clear(ship_remove, xOld, yOld);
            x = xOld;
            y = yOld;
            setCoordinates(x, y + 1 );
            printObject(ship, x, y + 1, &xOld, &yOld);
        }
    }
    return 0;
}

操作:'A' ← 'D' → 'W' ↑ 'S' ↓</p>

调试:

1


推荐阅读