c++ - Linux 的输入键不可管理,我如何在 C++ 中绕过它?

标签 c++ linux getch

我正在尝试使用 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/

相关文章:

c++ - 我如何使用 zlib 添加/删除 zip 文件中的特定文件?

c++ - 通过引用派生类对象从基类对象向下转换时抛出 bad_cast 异常

c++ - 如何在 Win x64 中的 32 位和 64 位应用程序之间共享 HWND?

c++ - 使用 rapidjson 进行字符串化

linux - 了解 Linux 机器的时代

c - 为什么 getch() 不读取输入的最后一个字符?

linux - ls -R忽略特定目录

linux - 将 flask ASK 移植到裸机 Linux

c - Twitter 之类的 "characters left"C 中的函数问题 : How to prevent it from going to the next line?

python - 为什么以下代码在 C 和 Python 中给出不同的输出?