【问题标题】:How can I have getch() just in some cases in C?在 C 中的某些情况下,我如何才能拥有 getch() ?
【发布时间】:2016-05-02 09:34:11
【问题描述】:

我真的不知道如何输入标题,所以我想出了这个。所以基本上我想在 C 中制作某种蛇。但我不知道如何制作控件。假设您按下了左箭头,而蛇必须向左移动,直到按下另一个键,但我无法想出代码,因为它总是停止并等待我按下键。

我正在考虑这样的事情:

for ( i = 0; i < 10 ; i++ ) {
     if( next_move = getch() ) break;
     else Sleep(10);
}
if(!next_move)
    next_move = prev_move;
if( move ( next_move, bite_cord ) ) {
    prev_move = next_move;
    next_move = 0;
    Sleep(300);
    system("CLS");
    printf("You lost. ");
}

我的想法是等待一秒钟,如果没有按下键继续前一个键,但如果按下它现在保存按下的键。

你有什么想法吗?

【问题讨论】:

标签: c getchar getch getc


【解决方案1】:

您可以使用kbhit() 代码类似于

if (kbhit())
    input = getch(); //Or something like that

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2019-10-31
    • 1970-01-01
    • 2022-10-07
    • 2014-01-17
    • 2012-04-01
    • 2012-11-18
    • 1970-01-01
    • 2014-07-16
    相关资源
    最近更新 更多