I am making this two player simple game.
I just need to make each of the player press a key any time not alternating from one another.

Do I still use getch() for a solution? I have tried but the user still presses his/her assigned keys after the other presses his/her assigned keys as well. I wanted it to be able to function even though they press keys together at the same time.

I hope you get my question.

Thank you very much.

Simultaneous processing can be done with threading, but that's a can of worms that I don't recommend opening unless you absolutely must. A way to fake it in a single threaded application would be to specify a distance threshold between the two keypresses that denotes "at the same time", and any two keypresses inside that distance would be treated as simultaneous.

Obviously this means calculating the time between each player's keypress, which isn't terribly difficult. However, you'll need to rely on a nonstandard timing method because C's standard timing granularity is limited to seconds. You could probably say that "simultaneous" is within 1ms.

What compiler and OS are you using?

clock() returns double and and number of ticks and you should divide it with ticks_per_second so as to get the time. thanks

you should divide it with ticks_per_second so as to get the time

That give you seconds since the program started, not the current time. one tick = one millisecond.

Edited 4 Years Ago by Ancient Dragon

time() gives you the current time, but in seconds. If you want to test for two keys within a certain number of milliseconds all you need is the value returned by clock(), then subtract the two values, you don't care what the current time is.

This article has been dead for over six months. Start a new discussion instead.