I need to put a time limit on a game, I think it's something like time.sleep() but have forgotten how it works. The game needs two minutes to be able to finish the game, but I have forgotten the formula. Can anybody enlighten me?

Thanks for any help.:)


One way is to use time.time() to read a timestamp, as in tstart=time.time() . This returns a floating point number of seconds since some unimportant starting point.

Then in your game you periodically check to see if time.time() is greater than tstart+120.0 , and quit if that's true.

commented: Very helpful and easy to understand comments. +1

The problem with time.sleep(sec) is that your whole program sleeps unless you thread the background action. So BearofNH's use of time.time() is more appropriate.

commented: Good post, reminded me of how time.sleep() worked. +1


Good work BearofNH and Bumsfeld. First Bumsfeld, you're absolutely right in saying that time.sleep() will shut down the program/game it's just that it's been so long since I've used it that I've forgotten what it actually does...but I should've looked at it's very name (time.sleep()) it's so self explanatory (duh!):$ .

To Bear, I havn't used time.time() before so am unfamiliar with it's useage, but it looks easy enough to work with...further to this, I was thinking if one were not to use up all the time alloted say 110 seconds and one had ten seconds left over one could add this on to the next game and not have 120 seconds but 130 seconds, it would all have to be working under a large while statement. Just a thought...

Thanks for your input guys:).