如何在 Python 中运行后台计时器 [英] How to run a background timer in Python

查看:58
本文介绍了如何在 Python 中运行后台计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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

推荐答案

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

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)

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

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

这篇关于如何在 Python 中运行后台计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆