0

Hello I'm creating a game in C.

I want there to be a frame printed every 0.1 seconds. During that time, the user may or may not input using getch().

How do I write such a program? Heres what I can offer you guys to work with.

do{
usleep(100000);  // simple 100 mili second delay
if (getch()==32) (ASCII for a space) // may or may not be inputed in 0.1 second timeframe.
playerJumps;
// even if user inputs early, I still want game printed exactly every 0.1 sec not sooner/later.
printGame;  
}while(notDead);

I really hope I kept code nice and clear

user3371097
  • 1
  • 1
  • 1
  • 1

1 Answers1

0

I've done this before, you are going to have to talk about what platform you are on. All of the C library input functions block to wait for input. One way to do it is with threads -- you have one thread block on the user input, and the other does the game, and it gets notified by the input thread when there is input. The other way is to use a function like poll() on linux, which I believe is what I used, they basically allow you to specify a wait period, or to just try to see if there is input and return immediately if there isn't. Though I think select() should also work, and I think that should be relatively cross platform.

user26347
  • 594
  • 3
  • 4