Posted by Overlord on 02:53:00 05-15-2003
I am making a game in Borland C++, and I need some help. I am trying to find out how to let the user enter keys within a certain time span. For instance, instead of simply stating "getch(key);" , I want the user to be able to enter a key only for a span of one or two seconds.
I cant seem to find a comand or a loop that I can use to create a imited time slot for the user to hit a key, any help would be apreciated.
Posted by dxprog on 03:03:00 05-15-2003
You can inculde the windows.h file and create a timer from it to keep track of the time. Then you can use something like:
int a;
BOOL CanUserPressKey;
int timer_callback()
{
a +=1;
if (a == 10) {
a = 0;
CanUserPressKey = True;
}
}
Before you run the getch statement have an if statement seeing if CanUserPressKey is true and if it is run the getch statement and as soon as a key is pressed set CanUserPressKey to false and a to 0 and it will be about a second before the user can press the key again.
I'm not usre how to actually create the timer in a console app (seen a tutorial before), but a quick net search should help. Also I'm basing my if statement for a on the fact that there would be a 100ms interval between calls to the timer function.
I hope this helps you some.
[addsig]
Posted by Overlord on 03:11:00 05-15-2003
That's sort of what I need, but I think it would help if I explained the game a little better.
If you have played dance dance revolution, you might undertand this. In my game, there are 6 keys that the user can press: up, down, left, right, up/right diagonal, up/left diagonal. These directions are "dance moves". My prog highlights a picture of one of these 6 keys randomly. Then, the user has X seconds to press the corresponding key in order to score points. The problem is, if I simply use a getch(); , the user will have unlimited time press the corresponding key. I need it so that the user has only X seconds to press the key. If they don't, the progam should continue. I have the feeling that I am overlooking some obvious soluion....
Posted by dxprog on 04:52:00 05-15-2003
It should only take a little bit of modification of my code to do what you're wanting.
[addsig]
Posted by Overlord on 02:27:00 05-16-2003
It doesn't really work.
I just need to make a getch(); statement that only waits a certain amout of time for a key to be entered, instead of waiting forever for the user to enter a key. Is there another command besides getch(); that I can use?
Posted by KaGez on 19:52:00 05-18-2003
this may sound complicated, but may be a good idea:
Just create a loop which loops for a certain time and then returns. While inside that loop you simply getch.
#include
int function(blahblahblah)
{
time_t start;
time_t now;
now = 0;
start = time(NULL);
while(now - start
[addsig]
Posted by Smerdyakov on 20:31:00 05-18-2003
Geeeeez, I wasn't going to reply, but: Games will generally use functions to test if individual keys are currently pressed. I know Allegro had an array indexed by keycodes for this purpose. You should look for a function to do this in the library you are using.
(It should be obvious how to use this to do what you want.)