【问题标题】:Is there a way to replace the kbhit() and getch() functions in Standard C?有没有办法替换标准 C 中的 kbhit() 和 getch() 函数?
【发布时间】:2014-03-31 16:08:36
【问题描述】:

我正在尝试与控制台进行快速时间事件类型的交互,并且我设法使用 conio 库获得了它。遗憾的是,我正在进行的项目要求代码在 Windows 和 Linux 上都可编译,我无法找到改变它的方法。

我可以做些什么来获得预期的效果,或者我应该放弃这个想法吗?下面是我创建的函数的代码。

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void battle(int x) {
    srand(time(0));
    printf("Use Q to dodge projectile attacks, E to block melee attacks and W to attack when the boss is stunned. Button mashing will not work! Press any key to start the battle.\n\nYou face Brutus, Lord Incarcerator!\n\n");
    getchar();
    bool ok;
    int dealt = 0 ,recieved = 0 , state, prev = 0;
    time_t start, end;
    double elapsed;
    while(dealt < 5 && recieved < 3)
    {
        do
        {
            state = rand() % 3 + 1;
        }
        while(prev == state);
        prev = state;
        time(&start);
        switch(state)
        {
        case(1):
            ok = 1;
            printf("Brutus uses Hook Attack!\n\n");
            do
            {
                time(&end);
                elapsed = difftime(end, start);
                if(kbhit())
                {
                    if( getchar() == 'q')
                    {
                        printf("Dodged!\n\n");
                        ok = 0;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            while(elapsed < 3);
            if(ok)
            {
            printf("You took damage!\n\n");
            recieved++;
            break;
            }
            break;
        case(2):
            ok = 1;
            printf("Brutus is stunned!\n\n");
            do 
            {
                time(&end);
                elapsed = difftime(end, start);
                if(kbhit())
                {
                    if( getchar() == 'w')
                    {
                        printf("You dealt damage!\n\n");
                        dealt++;
                        ok = 0;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            while(elapsed < 3);
            if(ok)
            {
            printf("Too slow!\n\n");
            break;
            }
            break;
        case(3):
            ok = 1;
            printf("Brutus uses Blood Slam!\n\n");
            do 
            {
                time(&end);
                elapsed = difftime(end, start);
                if(kbhit())
                {
                    if( getchar() == 'e')
                    {
                        printf("Blocked!\n\n");
                        ok = 0;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            while(elapsed < 3);
            if(ok)
            {
            printf("You took damage!\n\n");
            recieved++;
            break;
            }
            break;
        }
    }
    if(recieved >= 3)
    {
        printf("Battle lost; you will now respawn in town.");
        getchar();
    }
    else
    {
        printf("Battle won!");
        getchar();
    }
}
int main()
{
    battle(2);
}

【问题讨论】:

标签: c getch conio kbhit


【解决方案1】:

您可以编写自己的 getch()。

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

【讨论】:

  • Conio跟标准关系不大,OP问标准C
【解决方案2】:

并非如此,因为它们无法在某些平台上实施 实施,并且只能以巨大的成本实施 其他人。

虽然不是标准的,但有 可移植库,例如 ncurses 支持此类操作。

【讨论】:

    【解决方案3】:

    有两种选择

    1. 查找一些跨平台库,可以为您抽象出细节(SDL 可能对此有好处。)
    2. 自己进行抽象。

    要做 2. 您必须定义自己的输入函数版本。然后,您为每个平台创建不同的实现。这意味着您的核心代码在各种平台上保持通用。

    【讨论】:

      【解决方案4】:

      我认为你需要使用 ncurses 库。

      这里有类似的问题:Create a function to check for key press in unix using ncurses

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        • 1970-01-01
        • 1970-01-01
        • 2015-06-02
        相关资源
        最近更新 更多