微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用gotoxy函数而不是clrscr

做第一个项目,它是俄罗斯方块; 现在我正在做animation的一部分,但我有清理屏幕的问题,我试过了:

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

它的工作,但不断闪烁的屏幕,有没有办法使用gotoxy函数而不是clrscr同一目的?

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

system("cls")执行一个shell命令来清除屏幕。 这对于游戏编程来说是非常无效和明确的。

不幸的是屏幕I / O是依赖于系统的。 当你引用“CLS”而不是“清除”,我想你正在与Windows控制台:

如果你有一个函数gotoxy() ,可以把它放在一行之后,并打印出很多空格。 这不是超高性能,但它是一种方法。 这个SO问题提供了gotoxy()变量,因为它是一个非标准函数

此Microsoft支持建议提供了一个更高性能的替代方案,以清除Windows上的屏幕,使用winapi控制台功能 ,如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 @R_871_4045@ion 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,&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,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 @R_871_4045@ion GetConsoleScreenBufferInfo(hc,&csbi); // Get screen info & size x = csbi.dwCursorPosition.X; y = csbi.dwCursorPosition.Y; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐