【问题标题】:How to run a background timer in Python如何在 Python 中运行后台计时器
【发布时间】:2014-11-18 02:19:03
【问题描述】:

我目前正在制作一个数学游戏,用户有 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!"

【问题讨论】:

标签: python time timer while-loop


【解决方案1】:

使用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(60)
try:
    while lives > 0
        # stuff
except:
    # print score

【讨论】:

  • 所以设置 timer = time.time() 并将我的 while 循环更改为:while time.time() - timer 0?
  • 成功了!谢谢迈克。然而,另一个问题出现了。现在,无论时间> 60,如果命令行中有问题,游戏都不会退出。只有回答问题后,游戏才会退出。有没有办法立即停止游戏?
  • 可以,可以打断其他进程,不过要复杂一点,见上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
相关资源
最近更新 更多