【问题标题】:How to use getch() without waiting for input?如何在不等待输入的情况下使用 getch()?
【发布时间】:2014-07-20 08:34:18
【问题描述】:
 for (;;)
{
    cout << "You are playing for:" << playtime << "seconds." << endl;
    cout << "You have " << bytes << " bytes." << endl;
    cout << "You are compiling " << bps << " bytes per second." << endl;
    cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
    switch(getch())
    {
        case 'a': bytes = bytes - 10; bps++; break;
    }
    bytes = bytes + bps;
playtime++;
Sleep(1000);
system("cls");
}

假设这是我的增量游戏。我想在 1 秒后刷新我的游戏。如何让 getch() 在不停止所有其他内容的情况下等待输入?

【问题讨论】:

  • 对于跨平台的东西,您可以在STDIN_FILENOselect() 了解哪些按键正在等待读取。
  • 我还是不明白。你能在我的代码中给我看一下吗?

标签: c++ windows input getch conio


【解决方案1】:

使用khbit()函数来检测是否有按键被按下:)

类似:

 for (;;)
{
    cout << "You are playing for:" << playtime << "seconds." << endl;
    cout << "You have " << bytes << " bytes." << endl;
    cout << "You are compiling " << bps << " bytes per second." << endl;
    cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
    if(kbhit()){  //is true when a key was pressed
        char c = getch();   //capture the key code and insert into c

        switch(c)
        {
            case 'a': bytes = bytes - 10; bps++; break;
        }
    }
    bytes = bytes + bps;
    playtime++;
    Sleep(1000);
    system("cls");
}

【讨论】:

    【解决方案2】:

    您可以使用另一个线程来获取用户输入。

    for (;;) 是不必要的,你应该使用while (true)

    #include <Windows.h>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    DWORD WINAPI SpeedThread(LPVOID lpParam);
    
    
    
    int main ()
    {
        int playtime = 0,
            bytes = 0,
            bps = 1;
    
        bool bKeyPressed = false;
    
        CreateThread( NULL, 0, SpeedThread, &bKeyPressed, 0, NULL);
    
        while (true)
        {
            cout << "You are playing for:" << playtime << "seconds." << endl;
            cout << "You have " << bytes << " bytes." << endl;
            cout << "You are compiling " << bps << " bytes per second." << endl;
            cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
            if (bKeyPressed && bytes >= 10)
            {
                bytes -= 10;    
                bps++; 
    
                bKeyPressed = false;
            }
            bytes = bytes + bps;
            playtime++;
            Sleep(1000);
            system("cls");
        }
    
    }
    
    DWORD WINAPI SpeedThread (LPVOID lpParam)
    {
        bool * bKeyPressed = (bool *) lpParam;
    
        while (true)
        {
            if (_getch () == 'a')
                *bKeyPressed = true;
        }
    }
    

    【讨论】:

    • 你试图帮助我,我很感激这一点,但不幸的是它不起作用:/
    • @Fairlight 我现在已经测试了代码并对其进行了编辑。在我这边,它现在可以工作,但请告诉我你遇到的错误。您还必须在 bKeyPressed 部分添加一个 if 条件,以便字节永远不会低于 0-
    • 我说错了。您的代码似乎不错,但我是 C++ 的新手。我不知道如何使用 WinAPI(这些 DWORD 等)。我不知道把我的代码放在哪里。
    【解决方案3】:

    对我有用的不是使用getch(),而是使用scanf()。 为了阻止scanf 停止,您必须使用:

    scanf("%c \n",example);
    

    请记住,example 是一个指针 (char* example;)

    【讨论】:

    • scanf 是一个阻塞函数,它不能解决 OP 问题,因为他需要在两者之间做其他事情。
    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多