我正在尝试使用 C++ 学习蛇教程游戏。然而,他们似乎使用了一些 Windows 库,而在我的例子中,我正在 Linux 上编译它。问题出在我的 Input() 方法 中,我尝试使用我在此处找到的推荐代码对其进行更改,但这对我来说并不奏效。有没有办法解决这个问题或任何建议?谢谢。
#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;
关于c++ - Linux 的输入键不可管理,我如何在 C++ 中绕过它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697763/