【问题标题】:How to implement input without enter in C++ console game如何在C++控制台游戏中实现不输入输入
【发布时间】:2019-01-19 16:08:46
【问题描述】:

我卡在没有输入输入的输入中。 我尝试使用 conio.h 中的 kbhit() + getch() 并且它在我的系统(Win10 和 Ubuntu - unistd.h 和 termios.h)上不起作用。程序只是跳过这些函数的块。
然后我使用了 windows.h 中的 GetAsynkKeyState。它在游戏(关卡)中工作,虽然有问题,但不在菜单中。程序也可以通过输入调度跳过(或其他)块。
菜单输入:

// The menu interface
bool Menu::SelectLevel() {
    cout << "Select the level:" << endl;
    size_t arrow_pos = 0;
    // Prints level's names and char to exit the game
    for (size_t i = 0; i <= _levels.size(); ++i) {
        // Draw arrow before selected level
        if (i == arrow_pos) {
            cout << '>' << i + 1 << " - " << _levels[i].first[0] << endl;;
        }
        // Draw arrow before the exit select
        else if (i == _levels.size() && i == arrow_pos) {
            cout << '>' << "Exit" << endl;
        }
        // Draw the exit option
        else if (i == _levels.size()) {
            cout << ' ' << "Exit" << arrow_pos << endl;
        }
        // Draw levels list
        else {
            cout << ' ' << i + 1 << " - " << _levels[i].first[0] << endl;
        }
    }
    // Input from keyboard TODO DOESN'T WORK!:
    // If 's' pressed move arrow down
    PoollingDelay(1);
    if (GetAsyncKeyState(0x53) & 0x8000) {
        ++arrow_pos;
        // If arrow reached top it goes to the bottom
        if (arrow_pos == _levels.size() + 1) {
            arrow_pos = 0;
        }
    }
    // If 'w' pressed move arrow up
    else if (GetAsyncKeyState(0x57) & 0x8000) {
        --arrow_pos;
        // If arrow reached bottom it goes to the top
        if (arrow_pos == 65535) {
            arrow_pos = _levels.size() + 1;
        }
    }
    // If Return pressed
    else if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
        // Don't think it would be worthy
        if (arrow_pos < 1 || arrow_pos > _levels.size() - 1) {
            throw runtime_error("Wrong select: " + to_string(arrow_pos));
        }
        // If player tired of this shit
        if (arrow_pos == _levels.size() - 1) {
            ClearTerminal();
            return false;
        }
        // Play
        PlayLevel(arrow_pos);
    }
    ClearTerminal();
    return true;
}

电平输入:

// TO DO DOESN'T WORK!:
void Level::ReadCommand() {
    PoollingDelay(100);
    if (GetAsyncKeyState(0x57)) {
        Move(_NORTH);
    }
    else if (GetAsyncKeyState(0x41)) {
        Move(_WEST);
    }
    else if (GetAsyncKeyState(0x53)) {
        Move(_SOUTH);
    }
    else if (GetAsyncKeyState(0x44)) {
        Move(_EAST);
    }
    else if (GetAsyncKeyState(0x45)) {
        throw runtime_error(exit_the_lvl);
    }
}

【问题讨论】:

  • 请在问题中发布您的代码 (edit)。不接受指向存储在其他地方的代码的链接。
  • 我想在主机游戏中不可能做到这一点,但我不是 100% 确定这一点
  • @EdinHajdarevic 我在 Stackoverflow 和 gitHub 上找到了几个实现该功能的示例。但在我的系统上,这些示例不起作用..
  • 我不能 100% 确定,它可能有效,也可能无效。在 OOP 中是可能的,但我不知道控制台应用程序。

标签: c++ input console


【解决方案1】:

简答:不能只使用 C++ 及其标准库。

这是因为该语言不是为处理低级硬件事件而设计的。为此,您需要依赖一个单独的库,专门用于处理 I/O。其中很多,有些或多或少容易集成。对于简单的游戏,SDL 很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多