我正在使用 ncurses 库用 C++ 编写 Pacman 游戏,但我无法正确移动 Pacman。我已经使用 getch()
将它向上、向下、向左和向右移动,但当我按下任何其他键时,它只会向右移动而不会向其他任何地方移动。
这是向上移动的代码片段。我已经编写了类似的代码,其中一些条件相应地改变了,以便向左、向右和向下移动。
int ch = getch();
if (ch == KEY_RIGHT)
{
int i,row,column;
//getting position of cursor by getyx function
for (i=column; i<=last_column; i+=2)
{
//time interval of 1 sec
mvprintw(row,b,"<"); //print < in given (b,row) coordinates
//time interval of 1 sec
mvprintw(row,(b+1),"O"); //print "O" next to "<"
int h = getch(); //to give the option for pressing another key
if (h != KEY_RIGHT) //break current loop if another key is pressed
{
break;
}
}
}
if (condition)
{
//code to move left
}
我是不是使用了 getch() 错误,还是我还需要做其他事情?
最佳答案
键盘上的许多“特殊”键——上、下、左、右、Home、End、功能键等实际上将键盘 Controller 的两个扫描码返回给 CPU。 “标准”键都返回一个。所以如果你想检查特殊键,你需要调用 getch() 两次。
例如向上箭头首先是 224,然后是 72。
关于c++ - 我是否错误地使用了 ncurses 库中的 getch() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553562/