2017-02-19 345 views
0

我正在尝试构建一个简单的蛇游戏。 功能void Input()调用_kbhit()_getch() 但问题是,我不能实现这些功能的原因conio.h不包括在Linux中的gcc包。是否有替代方法来完成_kbhit()_getch()的任务而不使用conio头文件?如何在Linux上使用C++实现kbhit()和gethch()

void Input() // handle controls 
{ 
if(_kbhit()) // boolean positive if key is pressed 
{ 
switch(_getch()) // gets ascii val of key pressed 
{ 
     case 'a': 
     dir = LEFT; 
     break; 

     case 'd': 
     dir = RIGHT; 
     break; 

     case 'w': 
     dir = UP; 
     break; 

     case 's': 
     dir = DOWN; 
     break; 

     case 'x': 
     gameOver = true; 
     break; 
    } 
} 
} 

回答

1

这些函数有点“非法”,不再用于标准C++。

ncurses图书馆可能会有所帮助。

ncurses,顺便说一句,定义TRUE和FALSE。正确配置的ncurses将使用与ncurses'bool相同的数据类型作为用于配置ncurses的C++编译器。

下面是说明ncurses的可以像conio例如使用一个例子:

#include <ncurses.h> 
int main() 
{ 
initscr(); 
cbreak(); 
noecho(); 
scrollok(stdscr, TRUE); 
nodelay(stdscr, TRUE); 
while (true) { 
    if (getch() == 'g') { 
     printw("You pressed G\n"); 
    } 
    napms(500); 
    printw("Running\n"); 
} 
} 
+1

再次阅读问题,GetAsyncKeyState()是WinAPI的一部分,他想要的东西为Linux – CrizerPL

相关问题