2013-01-07 19 views
5

C'è qualche funzione che può attendere l'input fino a quando non viene raggiunto un certo tempo? Sto facendo una specie di gioco Snake.Attendi l'input per un certo tempo

La mia piattaforma è Windows.

+1

Nessun modo standard in C++ lo farà senza effetti prolungati a meno che il programma non termini subito dopo l'immissione. Anche se lo facesse, non lo consiglierei. – chris

+4

Il titolo dice "C" - i tag dicono sia "C" che "C++" - qual è? –

+1

Come già detto, non esiste un modo standard per farlo. In Linux, suggerirei di usare le ncurses [potrebbe anche funzionare per Windows]. La maggior parte dei sistemi operativi ha un metodo di input "no wait", ma non esiste un metodo standard per questo. –

risposta

0

Prova con bioskey(), this è un esempio per questo:

#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; 
     } 
    } 
} 
+0

Puntare semplicemente le persone a un link è un modo scarso di rispondere a una domanda: i collegamenti muoiono. Ho copiato l'esempio a cui ti sei collegato. Ora è molto più chiaro che la tua risposta non sembra rispondere alla domanda. – Richard

+0

Non ho l'intestazione "bios.h". Ho cercato e provato alcuni su google ma nessuno di loro funziona. Io uso l'editor di CodeBlocks con MinGW. Puoi suggerirmi che funziona? – Xuicide

2

Per i giochi basati su terminali si dovrebbe dare un'occhiata a ncurses.

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

Edit:

Vedi anche Is ncurses available for windows?

+0

La domanda è chiaramente etichettata come C o C++ – thecoshman

+1

@thecoshman 'ncurses' è una libreria per C/C++. –

+0

ah, scusa, ho scelto 'per i giochi basati su terminale' per indicare che avevi fornito una soluzione in un linguaggio basato sul terminale, riprendo quella nota precedente – thecoshman

1

ho trovato una soluzione che utilizza la funzione kbhit() di conio.h come segue: -

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; 
    } 
0

Sulla base di @birubisht answer I ha creato una funzione che è un po 'più pulita e utilizza versioni NON deprecate di kbhit() e getch() - ISO C++ _kbhit() e _getch().
funzione accetta: numero di secondi di attesa per l'input dell'utente
funzione ritorna:_ quando l'utente non mette alcun carattere, altrimenti restituisce il carattere inputed.

/** 
    * 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; 
} 
Problemi correlati