【问题标题】:Wait for input for a certain time等待输入一段时间
【发布时间】:2013-01-07 08:25:49
【问题描述】:

是否有任何功能可以等待输入直到达到特定时间?我正在制作一种 Snake 游戏。

我的平台是 Windows。

【问题讨论】:

  • 除非您的程序在输入后立即结束,否则没有标准的 C++ 方法可以做到这一点而不产生挥之不去的影响。即使有,我也不推荐。
  • 标题写着“C”——标签同时写着“C”和“C++”——它是什么?
  • 如前所述,没有标准的方法可以做到这一点。在 Linux 中,我建议使用 ncurses [可能也适用于 Windows]。大多数操作系统都有“无等待”输入法,但没有标准方法。
  • C和C++都做不到?有没有简单的语言可以做到这一点。
  • 你在使用任何框架吗?您可以在循环中检查键盘状态,而不是等待输入。在 Windows 上,您可以使用 API 函数来完成,但如果您使用 SDL 或 SFML 或类似的,还有其他选项。

标签: c++ c windows


【解决方案1】:

对于基于终端的游戏,您应该查看ncurses

 int ch;
 nodelay(stdscr, TRUE);
 for (;;) {
      if ((ch = getch()) == ERR) {
          /* user hasn't responded
           ...
          */
      }
      else {
          /* user has pressed a key ch
           ...
          */
      }
 }

编辑:

另见Is ncurses available for windows?

【讨论】:

  • 啊,对不起,我认为“基于终端的游戏”是指您提供了基于终端的语言的解决方案,我收回了那个否决
  • 虽然“nodelay”建议会起作用,但使用它会导致 CPU 负载急剧上升,并且可能会更快地耗尽笔记本电脑的电池。一个简单的替代方案是“halfdelay(1)”,它在任何输入请求期间最多等待十分之一秒。
【解决方案2】:

我找到了一个使用 conio.h 的 kbhit() 函数的解决方案,如下所示:-

    int waitSecond =10; /// number of second to wait for user input.
    while(1)
    {

     if(kbhit()) 
      {
       char c=getch();
       break;
      }

     sleep(1000); sleep for 1 sec ;
     --waitSecond;

     if(waitSecond==0)   // wait complete.
     break;  
    }

【讨论】:

  • 为什么不使用 while(waitSecond > 0) 并通过 if + break?这样做有副作用吗?
【解决方案3】:

试试bioskey()this 就是一个例子:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <ctype.h>

#define F1_Key 0x3b00
#define F2_Key 0x3c00

int handle_keyevents(){
   int key = bioskey(0);
   if (isalnum(key & 0xFF)){
      printf("'%c' key pressed\n", key);
      return 0;
   }

   switch(key){
      case F1_Key:
         printf("F1 Key Pressed");
         break;
      case F2_Key:
         printf("F2 Key Pressed");
         break;
      default:
         printf("%#02x\n", key);
         break;
   }
   printf("\n");
   return 0;
}


void main(){
   int key;
   printf("Press F10 key to Quit\n");

   while(1){
      key = bioskey(1);
      if(key > 0){
         if(handle_keyevents() < 0)
            break;
      }
   }
}

【讨论】:

  • 仅仅将人们指向一个链接是一种糟糕的回答问题的方式:链接死了。我复制了您链接到的示例。现在更清楚的是,您的回答似乎没有解决问题。
  • 我没有标题“bios.h”。我在谷歌上搜索并尝试了一些,但它们都不起作用。我将 CodeBlocks 编辑器与 MinGW 一起使用。你能给我推荐一些这样的工作吗?
【解决方案4】:

基于@birubisht answer,我制作了一个更简洁的函数,并使用了kbhit()getch() 的非弃用版本——ISO C++ 的_kbhit()_getch()
函数耗时: 等待用户输入的秒数
函数返回:_当用户没有放任何字符时,否则返回输入的字符。

/**
  * Gets: number of seconds to wait for user input
  * Returns: '_' if there was no input, otherwise returns the char inputed
**/
char waitForCharInput( int seconds ){
    char c = '_'; //default return
    while( seconds != 0 ) {
        if( _kbhit() ) { //if there is a key in keyboard buffer
            c = _getch(); //get the char
            break; //we got char! No need to wait anymore...
        }

        Sleep(1000); //one second sleep
        --seconds; //countdown a second
    }
    return c;
}

【讨论】:

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