472,811 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 4247
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

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

Similar topics

2
by: Andy | last post by:
Hi Gang Is there any way with ASP that I can tell if a user session has timed out? The problem is that we have a database app where a user session may time out. After this happens the user...
2
by: Javaman59 | last post by:
I have a GUI which is monitoring a real-time device. I have several timed actions, such as periodic polling of the device, and indicators which flash for a fixed period. My first implementation...
2
by: William F. Robertson, Jr. | last post by:
Some of my users are receiving an error: Server Error in '/' Application. ---------------------------------------------------------------------------- ---- Request timed out. Description: An...
0
by: Jack Wright | last post by:
Dear All, I have a web Application "http://localhost/Web/WebForm1.aspx" that calls a WebService from "http://localhost/webserviceapp/service1.asmx"...I have set the executionTimeout to 10 secs in...
5
by: Sachin Surana | last post by:
We use HttpWebRequest to send the request at a URL. But some times the method GetResponse throws a time out exception. But when we check the IIS logs, there is no such entry. So the request never...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
4
by: Joe | last post by:
I'm hosting my web service on a Windows 2003 box which is remotely located. When trying to add a web reference to a C# project I get an error message 'There was an error downloading...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.