如何在Python中运行后台计时器

时间:2014-09-23 18:44:48

标签: python time timer while-loop

我目前正在制作数学游戏,用户有60秒的时间来回答尽可能多的问题。截至目前,除了计时器之外我还能正常工作,计时器应该倒数到0或计数到60然后停止游戏。现在,我将计时器设置为time.clock()以计数到60,而当计时器小于该计时器时,游戏将继续运行。但是出于某种原因,time.clock()并不像我期望的那样工作。我也尝试同时运行两个while循环,但这两个循环都没有用。有人能帮帮我吗?只是想找一种在后台运行计时器的方法。

这是我的代码:

    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!"

1 个答案:

答案 0 :(得分:2)

使用time.time(),它返回纪元时间(即自1970年1月1日以来的秒数UNIX Time)。您可以将其与开始时间进行比较以获得秒数:

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

您可以让计时器在任何时候(即使用户输入信息)将信号从您的代码中拉出来,但信号稍微复杂一些。一种方法是使用信号库:

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

这定义了一个引发异常的函数,并在超时发生时调用。现在,您可以将while循环放在try catch块中并设置计时器:

signal.alarm.timeout(60)
try:
    while lives > 0
        # stuff
except:
    # print score