5

Is there any function that can wait for input until a certain time is reached? I'm making kind of Snake game.

My platform is Windows.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Xuicide
  • 105
  • 1
  • 9
  • 1
    No standard C++ way will do that without lingering effects unless your program ends right after the input. Even if it did, I wouldn't recommend it. – chris Jan 07 '13 at 08:26
  • 4
    Title says "C" - tags say both "C" and "C++" - which is it ? – Paul R Jan 07 '13 at 08:26
  • 1
    As mentioned, there is no standard way to do this. In Linux, I'd suggest using the ncurses [may also work for Windows]. Most OS's have a "no wait" input method, but there is no standard method for it. – Mats Petersson Jan 07 '13 at 08:31
  • C and C++ can't do it? Is there any Simple language that can do this. – Xuicide Jan 07 '13 at 08:38
  • @ChinnawudhSawee, It's not that they can't do it. It just can't be done in a standard way. You need some sort of non-standard library. – chris Jan 07 '13 at 08:38
  • Does it have to use Multi threading things? – Xuicide Jan 07 '13 at 08:42
  • If you specify which operating system you are using, you will get more answers. – Prof. Falken Jan 07 '13 at 08:47
  • 1
    Are you using any frameworks? Instead on waiting on input, you can check the keyboard state in a loop. On windows you can do it with API functions, but if you use SDL or SFML or similar, there are other options too. – Skalli Jan 07 '13 at 09:05

4 Answers4

3

For terminal based games you should take a look at ncurses.

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

Edit:

See also Is ncurses available for windows?

Community
  • 1
  • 1
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • ah, sorry, I took 'for terminal based games' to mean you had provided a solution in a terminal based language, I take back that downvote – thecoshman Jan 09 '13 at 10:15
  • 1
    Although the "nodelay" suggestion will work, using it can cause the CPU load to go up dramatically and probably drain laptop batteries much quicker. A simple alternative is "halfdelay(1)" which waits for at most 1 tenth of a second during any input request. – Brandin Jan 11 '13 at 16:54
1

I found a solution using kbhit() function of conio.h as follows :-

    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;  
    }
birubisht
  • 890
  • 8
  • 16
0

Try with bioskey(), this is an example for that:

#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;
      }
   }
}
Richard
  • 56,349
  • 34
  • 180
  • 251
Rajesh
  • 182
  • 1
  • 2
  • 9
  • Just pointing people at a link is a poor way of answering a question: links die. I copied the example you linked to. Now it's much clearer that your answer doesn't seem to address the question. – Richard Jan 07 '13 at 09:40
  • I don't have the header "bios.h". I searched and tried some on google but none of them works. I use CodeBlocks editor with MinGW. Can you suggest me some that work? – Xuicide Jan 07 '13 at 13:05
0

Based on @birubisht answer I made a function which is a bit cleaner and uses NON-deprecated versions of kbhit() and getch() - ISO C++'s _kbhit() and _getch().
Function takes: number of seconds to wait for user input
Function returns: _ when user does not put any char, otherwise it returns the inputed char.

/**
  * 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;
}
Community
  • 1
  • 1
jave.web
  • 13,880
  • 12
  • 91
  • 125