Linux的输入键是不可管理的,我怎样才能在C++中使用它?
问题描述:
我想跟随使用C++的蛇教程游戏。但是,似乎他们正在使用一些Windows库,在我的情况下,我正在Linux上编译它。问题出在我的Input()方法上,我尝试用我在这里找到的推荐代码进行更改,但这对我来说并没有什么效果。有没有办法解决这个或任何建议?谢谢。Linux的输入键是不可管理的,我怎样才能在C++中使用它?
#include <iostream>
#include <stdio.h>
#include <ncurses.h>
#include <unistd.h>
.....
//Get the key input inorder to move the snake around
void Input() {
//Tried this from the stackoverlow recommendations, did not work for my situation
if (getch() == '\033') {
getch();
switch(getch()) { // the real value
case 'A':
// code for arrow up
direction = UP;
break;
case 'B':
// code for arrow down
direction = DOWN;
break;
case 'C':
// code for arrow right
direction = RIGHT;
break;
case 'D':
// code for arrow left
direction = LEFT;
break;
case 'Q':
gameOver = true;
break;
default:
break;
}
}
}
.....
我与遇到的问题是,Linux不接受的kbhit()这是什么蛇游戏教程采用的,所以我想与我有什么上述修改它,但它不动蛇。
答
这个case 'A':
期望输入大写字母。通常情况并非如此:)。所以:
switch(getch()) { // the real value
case 'A': // either capital
case 'a': // or low case
// code for arrow up
direction = UP;
break;
+0
在开关之前可以使用'toupper()'或'tolower()':'switch(toupper(getch()))' –
+1
这不是问题;方向键确实发送逃生,然后是大写字母(在大多数终端上)。 –
什么“没有工作”? –
@BradS。我的输入法,教程设置的方式是使用kbhit(),并且仅适用于Windows。不过,我想找到一种方法来做到这一点在Linux中。 – KonoDDa
所以,你的问题是你必须打回来? –