469,925 Members | 1,541 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

Timed input

I'm working on a simple video game program that will require timed responses. The easiest example I can think of would be something like:

char ans; // answer to question
(can't remember the variable type) starttime; //when clock starts, I'm at work and don't have access to my C++
(same story, when I get home, I'll fix this) timer; // makes sure person does go over time limit
cout<<"Press choose your answer shown on the screen (A/B/C/D)\n";

/*not actually what I'm doing, just an easier version with the same idea, basically just a multiple choice question with limited time to answer. */
Expand|Select|Wrap|Line Numbers
  1. starttime=time(NULL); //once again, I believe that's right, but am at work
  2. do
  3. {
  4.   cout<<"You're in space, you:\nA) Die.\nB) Vomit.\nC) Play.\nD) Fall anyway.\n"; // I want this to stay, so no "CLS," but I want it to disappear after time is up
  5.   timer=time(NULL); //makes timer soon after starttime
  6.   while( (starttime+10>=timer)&&( (ans!='A')||(ans!='a')||(ans!='B')||(ans!='b')||(ans!='C')||(ans!='c')||(ans!='D')||(ans!='d') )
  7.   {
  8. /*This upcoming part is what is actually confusing me, because I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input.  Any input?*/
  9.  
  10.  
  11.      cin>>ans; // I want them to be able to enter for limited time, but a while loop doesn't stop until the cin is entered, will it?
  12.      timer=time(NULL); // resets timer in case while loop has more time and person didn't enter a valid answer
  13.   }
  14.  
  15.  
  16.  
  17.   if( (ans!='A')&&(ans!='a')&&(ans!='B')&&(ans!='b')&&(ans!='C')&&(ans!='c')&&(ans!='D')&&(ans!='d') ) // if person doesn't answer in time, they are assigned an answer
  18.   {
  19.     ans='L';
  20.   }
  21. }
  22. while( (ans!='A')&&(ans!='a')&&(ans!='B')&&(ans!='b')&&(ans!='C')&&(ans!='c')&&(ans!='D')&&(ans!='d')&&(ans!='L' ); // person really REALLY has an answer
  23. if( (ans=='a')||(ans=='A') )
  24. {
  25.   cout<<"Your head a splode."; // gotta have humor
  26. }
  27. if( (ans=='b')||(ans=='B') )
  28. {
  29.   cout<<"\nYou technicolor yawn."; // I'm bored at work, what can I say?
  30. }
  31. if( (ans=='c')||(ans=='C') )
  32. {
  33.   cout<<"You twiddle your thumbs."; // Some jokes aren't meant to be funny.
  34. }
  35. if( (ans=='d')||(ans=='D') )
  36. {
  37.   cout<<"You fall to your doom.\n\n...That's kinda weird, what with the lack of gravity.  You're my hero!"; // Gotta admit, REALLY bored at work
  38. }
  39. if(ans=='L')
  40. {
  41.   cout<<"\nBefore you can react, you get hit by a bus."
  42. }
  43.  
  44. /*That's all that I think really relates, but I'll write more when I get home.*/
  45.  
Feb 21 '07 #1
12 3893
RedSon
5,000 Expert 4TB
One thing you can do is get the current system time and save it off. Then ask the user for input. Then get the system time immediately after they have entered their input. You can subtract the time and see if it is longer then your threshold. If it is then they took to long. If it isnt they they got their answer in, under the time limit.
Feb 21 '07 #2
One thing you can do is get the current system time and save it off. Then ask the user for input. Then get the system time immediately after they have entered their input. You can subtract the time and see if it is longer then your threshold. If it is then they took to long. If it isnt they they got their answer in, under the time limit.
Sorry, still unfamiliar with these forums, but I will be using them more. I had said "I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input."

That is totally an option, but it's more of a plan B, if I can't just stop the input from happening.
Feb 21 '07 #3
sicarie
4,677 Expert Mod 4TB
Sorry, still unfamiliar with these forums, but I will be using them more. I had said "I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input."

That is totally an option, but it's more of a plan B, if I can't just stop the input from happening.
Did you consider the 'sleep()' function in correlation with 'fork()'? (Would be Linux-dependent.)

It looks like there is a "CreateProcess()" in the Win32 API, but I haven't looked into if it runs parallel to the other process, etc...
Feb 21 '07 #4
Did you consider the 'sleep()' function in correlation with 'fork()'? (Would be Linux-dependent.)

It looks like there is a "CreateProcess()" in the Win32 API, but I haven't looked into if it runs parallel to the other process, etc...

...I really have very little understanding of what that means. So it's probably the right stuff for me to know. Murphy's law.

Anyway, could you please explain in more laymen's terms?
Feb 22 '07 #5
sicarie
4,677 Expert Mod 4TB
...I really have very little understanding of what that means. So it's probably the right stuff for me to know. Murphy's law.

Anyway, could you please explain in more laymen's terms?
What operating system are you using to code this, and what operating systems do you want it to be for? (If you only code for Windows or Linux, it will not be usable on the other type of system)
Feb 22 '07 #6
What operating system are you using to code this, and what operating systems do you want it to be for? (If you only code for Windows or Linux, it will not be usable on the other type of system)
Well, I use windows (XP), but the C++ program is Dev-C++. I'm trying to make a video game (like almost everyone else) that I'd be able to play in my free time...after using all my free time to make it. So probably the same one.

I'm not very good with computers, but I am with math and video games, which is why I took a C++ class in HS, but now I'm a few years since and don't remember everything I learned in my mere two trimesters of it...so basically I need computer terminology to be stated like I'm a five year old (great movie).
Feb 22 '07 #7
sicarie
4,677 Expert Mod 4TB
Well, I use windows (XP), but the C++ program is Dev-C++. I'm trying to make a video game (like almost everyone else) that I'd be able to play in my free time...after using all my free time to make it. So probably the same one.

I'm not very good with computers, but I am with math and video games, which is why I took a C++ class in HS, but now I'm a few years since and don't remember everything I learned in my mere two trimesters of it...so basically I need computer terminology to be stated like I'm a five year old (great movie).
Well, this is something a quick google search of createprocess win32 api returned, looks like it has a good explanation of it.

And to the ends of creating a video game, I'm probably one of the people on this site least able to help you (sorry!). I'm all application stuff - really even prefer hardware to coding, but right now it's where the job market is taking me.

Can anyone else put up a few good links for video game development?

(The type of game you're trying to create might help here - MMO/RPG, FPS, etc...)
Feb 22 '07 #8
AdrianH
1,251 Expert 1GB
Try istream::readsome(). It will read from the stream buffer and will return immediatly even if your buffer is not filled.


Adrian
Feb 22 '07 #9
I'm doing this for a complex RPG, this is just one itty-bitty part of it. The problem is that I want people to get the sense of rushed answers, and so after the time limit, I want the answers and such to disappear and the default message to be posted.

Basically at this point I can use the time between when they input and when they should have, but I'd rather just stop the input available, like in the keyboarding videogames, where you have limited time and you're supposed to type things, and after the time limit it stops you. But it is only one character that is being input(ed?), so I suppose the difference between the times would work.

Also, much more basic question, can the arrow keys be input as characters?
Feb 22 '07 #10
AdrianH
1,251 Expert 1GB
I'm doing this for a complex RPG, this is just one itty-bitty part of it. The problem is that I want people to get the sense of rushed answers, and so after the time limit, I want the answers and such to disappear and the default message to be posted.

Basically at this point I can use the time between when they input and when they should have, but I'd rather just stop the input available, like in the keyboarding videogames, where you have limited time and you're supposed to type things, and after the time limit it stops you. But it is only one character that is being input(ed?), so I suppose the difference between the times would work.

Also, much more basic question, can the arrow keys be input as characters?
Then readsome() is what you want, provided you want to be using the ANSI standard C++ library. If this is specific to some platform and it will not be ported to another OS, you could use library calls that are specific to it.

If the user has a time limit, I'd put some sort of prompt that changes showing how much time is left.

Arrow keys don't add to the input buffer. If you want that, you require using a terminal library, such as ncurses. Again this is a terminal standard. If you are programming for a particular platform and it will not be ported, you can try using that OSs interface library instead.


Adrian
Feb 23 '07 #11
nmadct
83 Expert
What exactly is your end product gonna look like? Is this a terminal-based adventure/RPG game? Are there graphics involved? If you want complete control of I/O I'd recommend using ncurses on the terminal or SDL if you're using graphics. Even on the console, the SDL has some convenient features for I/O, timers and sound.
Feb 23 '07 #12
Then readsome() is what you want, provided you want to be using the ANSI standard C++ library. If this is specific to some platform and it will not be ported to another OS, you could use library calls that are specific to it.

If the user has a time limit, I'd put some sort of prompt that changes showing how much time is left.

Arrow keys don't add to the input buffer. If you want that, you require using a terminal library, such as ncurses. Again this is a terminal standard. If you are programming for a particular platform and it will not be ported, you can try using that OSs interface library instead.


Adrian
...(Yay elipsis!!!) Yeah, really don't understand most, so I'll try summarizing, and just tell me when I'm completely wrong.

I'm using DevC++, which I was recently told sucks, as to whether or not this has ANSI or how I can tell, I am oblivious.

The platform that it will be used on...is that like windows, or would that be the console? I'm really not sure about most of this stuff, and if someone knew of an idiot's dictionary for C++ online, I'd love a link. Most of the webpages I've looked at go from naming the words to using them in a program and use pretty high-end terminology.

Don't really know what 'input buffer' is, but I do understand that arrow keys don't do anything.

I don't really understand anything in the 'terminal' paragraph except that I need a new library...probably.



Again, I'm really not all that well taught, and forgot quite a bit, the best path is to treat me like a 5 year old. More info than I need is better than making me ask even more questions.
Feb 23 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by William F. Robertson, Jr. | last post: by
5 posts views Thread by Sachin Surana | last post: by
6 posts views Thread by Daniel Walzenbach | last post: by
reply views Thread by Alex Papadimoulis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.