Clearing screen and kbhit() function

耗尽温柔 提交于 2019-11-30 10:03:17

问题


I got some problems writing my snake game program. I need to make game working on linux and windows. I found some topics how to clear screen on linux and windows using the #ifdef Windows etc. The problem is i need to use C89 standard, and im not sure that system("cls") is in C89. Could you help me with finding C89 functions to clear screen, and tell me something about kbhit() function on linux? Sorry for my english, and thanks for help.


回答1:


C89 does not have terminal handling functions. Instead, you should use OS specific functions. So you need to have, say, a source file only for windows functions and another for linux. Another option is to use a cross platform library. I would choose ncurses for this task:

http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/

It works on any unix system, including linux and Mac OS. For windows versions, see:

Is ncurses available for windows?

With ncurses, you have functions like erase() and clear() to clear the screen.




回答2:


On Unix-liked systems including Linux and macOS, you can use ncurses library (POSIX API). In case of Windows (or even Linux or macOS), the following code will work on ANSI terminals on any systems.

printf("\033[2J\033[H");
/* or */
printf("\033[0;0f");


来源:https://stackoverflow.com/questions/17129022/clearing-screen-and-kbhit-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!