私はモルモットプログラムを作っていますが、現在はモルが表示されランダムに消えるように設定しています。しかし、これがすべて進行している間に、私はモルを "叩く"ためにユーザー入力を受け入れる必要があります。ユーザーが何かを入力するのを待つためにループを一時停止せずに、入力をスキャンする間にループを実行させないでこれを行う方法はありますか?私のコードは以下の通りです。ルーピング中に入力をスキャンする方法(Cプログラム)
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
int main(){
// sets the mole to be initially under_ground
bool above_ground = false;
char mole_presence[1] = {0};
// keeps the mole running as long as the game is in play
while(1){
// while the mole is not above ground, wait until he randomly is
while(above_ground == false){
int r = rand() % 6;
if (r == 5){
printf("a mole has appeared, he's dancing around!\n");
mole_presence[0] = 1;
above_ground = true;
}
else{
printf("%d\n", mole_presence[0]);
sleep(1);
}
}
// while the mole is above ground, he dances until he randomly escapes
while(above_ground == true){
bool escaped = false;
// while he hasn't escaped, continue this loop
while (escaped == false){
int x = rand() % 10;
// if he randomly escapes, break out and show he is no longer above ground
if (x == 5){
printf("he disappeared!\n");
mole_presence[0] = 0;
escaped = true;
}
else{
printf("%d\n", mole_presence[0]);
sleep(1);
}
}
above_ground = false;
}
}
}
可能な重複、http://stackoverflow.com/questions/21091191/implement-a-keypress-event-in-c –