2016-08-22 135 views
0

編輯1:如何使程序不等待輸入

我想有一個程序,檢查輸入鍵按下,然後跳過後,它的一些代碼(有點像一些遊戲可跳過屏幕,當你啓動它們)。但我不希望它等待輸入。

if(getchar()=='\n') 
{ 
goto skip; 
} 
ClearScreen(); 
printf("%s           Welcome to Guy's game!\n\n"); 
Sleep(500); 
ClearScreen(); 
printf("%s           Welcome to Guy's game!\n\n"); 
Sleep(500); 
ClearScreen(); 
printf("%s           Welcome to Guy's game!\n\n"); 
Sleep(500); 
ClearScreen(); 
printf("%s           Welcome to Guy's game!\n\n"); 
Sleep(500); 
ClearScreen(); 
//this is the where it should skip 
skip: 
printf("%s           Welcome to Guy's game!\n\n"); 
printf("Please enter your name: "); 
gets(name); 
Sleep(250); 

我想它來檢查進入而它打印「歡迎Guy的遊戲!」所以無論何時按下輸入鍵(只要它正在打印「歡迎來到Guy的遊戲!」),我可以跳到代碼的最後部分。 我無法弄清楚如何讓這個工作。

UPDATE:

我有一個問題,我忘了問.. 如何解決「警告:函數‘睡眠’隱式聲明,當我使用:

ClearScreen(); 
printf("%s           Welcome to Guy's game!\n\n"); 
Sleep(500); 

在for循環中

+0

Google for'while循環等待輸入'也許指向您的目標解決方案。 – meAbab

+0

如果您使用的是Windows,請在每個'printf'之後使用'kbhit'來查看用戶是否推送了一個密鑰。 – user3386109

+0

@ user3386109是不是在包中? – Guy

回答

0

所以..我終於解決了!我創建了一個新程序來嘗試找出如何解決我目前正在處理的問題。

(代碼在[...]的

#include <stdio.h> 
#include <conio.h> 
#include <windows.h> 

void ClearScreen(void) 
{ 
    system("cmd /c cls"); 
} 

int main(void) 
{ 
    [...] //Everything is in here for next examples 
} 

爲下一個例子)

任何字母:

int i=0; 
    char name[20]; 

    while(1) 
    { 
    for(i=1; i>0&&i<5; i++) 
    { 
     //ClearScreen(); 
     printf("%i", i); 
     printf("           Welcome to Guy's game!\n\n"); 
     Sleep(500); 
     while (kbhit()) 
     { 
     getch(); 
     goto INPUT; 
     i=0; 
     } 
    } 
    goto INPUT; 
    } 
    INPUT: 
    ClearScreen(); 
    //this is the where it should skip 
    printf("           Welcome to Guy's game!\n\n"); 
    printf("Please enter your name: "); 
    gets(name); 
    Sleep(250); 
    printf("%s", name); 
    return 0; 

對於只有一個字母:

int i=0; 
    char name[20]; 

    while(1) 
    { 
    for(i=1; i>0&&i<5; i++) 
    { 
     //ClearScreen(); 
     printf("%i", i); 
     printf("           Welcome to Guy's game!\n\n"); 
     Sleep(500); 
     while (kbhit()&&getch()== 'p') 
     { 
     goto INPUT; 
     i=0; 
     } 
    } 
    goto INPUT; 
    } 
    INPUT: 
    ClearScreen(); 
    //this is the where it should skip 
    printf("           Welcome to Guy's game!\n\n"); 
    printf("Please enter your name: "); 
    gets(name); 
    Sleep(250); 
    printf("%s", name); 
    return 0; 

如果你喜歡,你可以在你的代碼中使用它!祝你好運,並感謝你的答案!

*順便說一句,我讓它在循環中顯示數字,以便它可以證明該程序的工作原理,但是可以將該部分取出,因爲它不是必需的。

0

啓動一個顯示你的消息的線程同時讀取鍵盤等待命中當用戶點擊輸入時終止顯示文本的線程並恢復正常流量跳到新屏幕。

+0

如何創建線程?對不起,我不知道我試圖查找它,但我不明白。 – Guy

+1

那麼你有線程庫運行在Linux下,但不是windoze,以及那些運行在windoze下,但不是在Linux上。所以,如果你使用gcc的Windows 10,你有一個霍布森的選擇。 –

2

你需要改變你的終端不做規範密鑰處理。

所有的終端都會保留一段時間的信息,以允許用戶點擊退格鍵(並且程序沒有獲取擦除的字符)。這種按鍵緩衝不會發送到程序,直到按下特殊的「緩衝器沖洗」鍵,如Enter或Return。

在遊戲中這是不受歡迎的,只要你能得到它們,你就想要鑰匙。這意味着您的程序將需要termios數據結構,然後關閉(與終端通信)Canonical密鑰處理。請注意,這意味着您必須在程序中處理退格刪除(如果您希望在程序的其他部分使用退格刪除,例如輸入姓名以獲得高分)。

// define a terminal configuration data structure 
struct termios term; 

// copy the stdin terminal configuration into term 
tcgetattr(fileno(stdin), &term); 

// turn off Canonical processing in term 
term.c_lflag &= ~ICANON; 

// set the terminal configuration for stdin according to term, now 
tcsetattr(fileno(stdin), TCSANOW, &term); 

請注意,這隻會改善響應;但是,當終端與內核進行通信時,實際上您仍然有小的延遲,然後內核會與您的程序進行通信。

例如,內核實際上捕獲按鍵/按鍵事件,並在短定時器週期內評估它們以確定按鍵是否需要「自動重複」。

我的建議是,如果你有興趣讓你的遊戲快速出來,你就不要冒險去編寫這段代碼。相反,使用一個遊戲編程庫來適當地交互和配置你的環境,比如快板。但是,如果你對這個如何在封面下工作感興趣,盡一切辦法編寫你的代碼來處理它(因爲它是一個相當迷人的話題,你真的會更好地理解終端/程序/內核通信! )。

+0

我試過使用編程庫,但我似乎無法找到在哪裏得到它們或如何使它們在我的代碼中正確工作。我嘗試了'#include '和'#include Guy

+0

這是在Windows上工作還是隻是POSIX? –

+0

像allegro和其他遊戲庫這樣的圖書館的代碼是windows/linux敏感的,並且會根據安裝的平臺做正確的事情。它不會允許你按照你設想的方式在等待輸入時進行顯示,但它可以讓你「檢測關鍵點」和「檢測關鍵點」,就像你可能對按鍵長度重要的遊戲所做的那樣,然後你做一個「獲取輸入」,「更新數據」,「繪製輸出」循環來處理繪製到屏幕上。 –