3

I'm currently making a math game where the user has 60 seconds to answer as many questions as possible. As of now, I have everything working except for the timer which should either count down to 0 or count up to 60 then stop the game. Right now, I have the timer set to time.clock() to count up to 60 and while the timer is less than that, the game will continue to run. For some reason however, the time.clock() isn't working as I expect it to. I also tried running two while loops at the same time which didn't work either. Anyone able to help me out here? Simply looking for a way to have a timer run in the background.

Here's my code:

    score = 0
    timer = time.clock()
    lives = 3

    while timer < 60 and lives > 0:
        if score >= 25:
            x = random.randint(-100,100)
            y = random.randint(-100,100)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 20:
            x = random.randint(-75,75)
            y = random.randint(-75,75)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 15:
            x = random.randint(-50,50)
            y = random.randint(-50,50)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 10:
            x = random.randint(-25,25)
            y = random.randint(-25,25)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 5:
            x = random.randint(-10,10)
            y = random.randint(-10,10)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 0:
            x = random.randint(-5,5)
            y = random.randint(-5,5)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
    if lives == 0:
        print "Oh no! You ran out of lives! Your score was %d." % score
    elif timer == 60:
        print "Time's up! Your score is %d." % score
else:
    print "Goodbye!"
  • http://stackoverflow.com/questions/2223157/how-to-execute-a-function-asynchronously-every-60-seconds-in-python – jcfollower Sep 23 '14 at 18:50
  • you need to `time.clock()` each time you want to check. Plus, time.clock() does not do what you think it does. – fredtantini Sep 23 '14 at 18:55
  • This question has good answer: http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish – James Sapam Sep 23 '14 at 20:01

2 Answers2

7

Use time.time(), it returns the epoch time (that is, the number of seconds since January 1, 1970 UNIX Time). You can compare it to a start time to get the number of seconds:

start = time.time()
while time.time() - start < 60:
    # stuff

You can have a timer pull you out of your code at any point (even if the user is inputting info) with signals but it is a little more complicated. One way is to use the signal library:

import signal
def timeout_handler(signal, frame):
    raise Exception('Time is up!')
signal.signal(signal.SIGALRM, timeout_handler)

this defines a function that raises an exception and is called when the timeout occurs. Now you can put your while loop in a try catch block and set the timer:

signal.alarm(60)
try:
    while lives > 0
        # stuff
except:
    # print score
Roman Kovtuh
  • 561
  • 8
  • 14
Mike
  • 6,813
  • 4
  • 29
  • 50
  • So set timer = time.time() and change my while loop to: while time.time() - timer < 60 and lives > 0? –  Sep 23 '14 at 19:17
  • It worked! Thanks Mike. Another problem arose however. Now, the game won't quit if theres a question in the command line regardless of the time being > 60. The game quits only after the question is answered. Is there a way to stop the game immediately? –  Sep 23 '14 at 19:41
  • Yes, you can interrupt other processes, but it is a bit more complicated, see above. – Mike Sep 23 '14 at 19:52
1

An easiar way would be to create a variable for how long the timer should run for. Here is an example

EpicHxr
  • 36
  • 4