【问题标题】:how to use gotoxy function instead of clrscr如何使用 gotoxy 函数而不是 clrscr
【发布时间】:2015-04-18 18:59:31
【问题描述】:

做第一个项目,这是俄罗斯方块; 现在我正在做动画部分,但是我在清除屏幕时遇到了问题,我试过了:

void clrscr() 
{ 
  system("cls"); 
}

它有效,但它一直在闪烁屏幕,有没有办法使用gotoxy 函数而不是clrscr 来达到同样的目的?

我在 Visual Studio 2008 上使用 Windows 控制台系统 32。

【问题讨论】:

  • @Christine system() 调用 shell 来执行命令。这是非常低效的,不适用于游戏编程。屏幕 I/O 取决于系统。您在哪个操作系统上工作?
  • Windows 控制台系统 32,在 Visual Studio 2008 上

标签: c++ windows console tetris cls


【解决方案1】:

system("cls") 执行一个 shell 命令来清除屏幕。这非常低效,而且绝对不适用于游戏编程。

不幸的是,屏幕 I/O 取决于系统。当您提到“cls”而不是“clear”时,我猜您正在使用 Windows 控制台:

  • 如果你有一个函数gotoxy(),它可以在一行之后的位置上打印很多空格。它不是超高性能,但它是一种方法。这个SO question 提供gotoxy() 替代,因为它是一个非标准函数。

  • 这个microsoft support recommendation 提供了一种更高效的替代方法来清除Windows 上的屏幕,使用winapi console functions,例如GetConsoleScreenBufferInfo()FillConsoleOutputCharacter()SetConsoleCursorPosition()

编辑:

我了解您在编写控制台应用程序而不是功能齐全的 win32 图形应用程序时使用基于字符的输出。

然后您可以通过仅清除控制台的一部分来调整上面提供的代码:

void console_clear_region (int x, int y, int dx, int dy, char clearwith = ' ')
{
    HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE);  // get console handle 
    CONSOLE_SCREEN_BUFFER_INFO csbi;        // screen buffer information
    DWORD chars_written;                    // count successful output

    GetConsoleScreenBufferInfo(hc, &csbi);      // Get screen info & size 
    GetConsoleScreenBufferInfo(hc, &csbi);      // Get current text display attributes
    if (x + dx > csbi.dwSize.X)                 // verify maximum width and height
        dx = csbi.dwSize.X - x;                 // and adjust if necessary
    if (y + dy > csbi.dwSize.Y)
        dy = csbi.dwSize.Y - y;

    for (int j = 0; j < dy; j++) {              // loop for the lines 
        COORD cursor = { x, y+j };              // start filling 
        // Fill the line part with a char (blank by default)
        FillConsoleOutputCharacter(hc, TCHAR(clearwith),
            dx, cursor, &chars_written);
        // Change text attributes accordingly 
        FillConsoleOutputAttribute(hc, csbi.wAttributes,
            dx, cursor, &chars_written);
    }
    COORD cursor = { x, y };
    SetConsoleCursorPosition(hc, cursor);  // set new cursor position
}

编辑 2:

此外,这里还有两个可以与标准 cout 输出混合使用的光标定位功能:

void console_gotoxy(int x, int y)
{
    HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE);  // get console handle 
    COORD cursor = { x, y };
    SetConsoleCursorPosition(hc, cursor);  // set new cursor position
}

void console_getxy(int& x, int& y)
{
    HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE);  // get console handle 
    CONSOLE_SCREEN_BUFFER_INFO csbi;        // screen buffer information
    GetConsoleScreenBufferInfo(hc, &csbi);      // Get screen info & size 
    x = csbi.dwCursorPosition.X;
    y = csbi.dwCursorPosition.Y;
}  

【讨论】:

  • 好吧,我不想清除整个屏幕,我只想在再次打印之前清除形状,这样我就不会在整个屏幕上绘制形状,但是只有移动它。 @克里斯托夫
  • @Christine 我进行了编辑以展示如何进行部分清算。
  • @Christine 并重新阅读您的问题,我还添加了两个函数来获取和设置光标位置。现在你应该已经为你的主机游戏做好了充分的准备;-)
猜你喜欢
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 2017-12-13
  • 2021-04-20
相关资源
最近更新 更多