0

我试图将变量减少 5,打印新变量,然后在 X 时间后再次增加变量并打印变量。它是一款基于文本的小型游戏,您可以在其中将一些人送走,但他们在一定时间内不会回来。

import time

TimeReturned = time.time() + 5
Survivors = 10

Test = raw_input("Run Script?")
if Test == "Yes":
    print Survivors
    if TimeReturned > time.time():
        Survivors -= 5
        print Survivors
    Survivors += 5
    print Survivors

我得到的只是输出是 10、5、10,它根本不会延迟时间。这是我的第一个问题,如果含糊不清,非常抱歉。

  1. 我做错了什么导致此代码立即返回变量?

  2. 如果我想在脚本中执行其他操作,例如将剩余的幸存者送走,是否可以在此脚本运行时进行,还是需要等待 5 秒?

谢谢你的耐心:)

4

3 回答 3

1

为了让您的脚本等待,请使用time.sleep(num_seconds). time.time()只返回当前时间;它根本不会让任何东西等待。

可能有一些代码处于休眠状态,而其他代码同时运行,做其他事情。但是,要做到这一点,您必须使用线程,这需要一些时间来适应。也许本教程会有用。

编辑:哦,也可以在没有线程的情况下执行此操作,但是您必须仔细跟踪变量。你的数学有点搞砸了。假设当 time.time() 为 1000 时,您的代码运行了。然后TimeReturned是 1005。假设用户输入 1 秒钟Yes。然后if TimeReturned > time.time()检查if 1005 > 1001,这是真的。您真正想要检查的是if time.time() > TimeReturned- 当前时间是否晚于TimeReturned.

此外,您的脚本不是交互式的,因此很难看到任何进展。尝试运行此脚本:

import time

survivors = 15
survivor_return_seconds = 10.0
time_survivors_left = None

while True:
    action = raw_input("Type 'x' to make survivors leave, ENTER to see how many are left: ")

    #check if survivors returned
    if time_survivors_left is not None:
        if time.time() >= time_survivors_left + survivor_return_seconds:
            survivors += 5
            time_survivors_left = None
            print "Survivors came back!"

    if action == 'x':
        if time_survivors_left is not None:
            print "Survivors already left! Wait a bit!"
        else:
            survivors -= 5
            time_survivors_left = time.time()

    print "There are %s survivors left." % (survivors,)
    if time_survivors_left is not None:
        print "5 survivors will return in %.2fs" % (
            time_survivors_left + survivor_return_seconds - time.time())

示例输出:

Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left: x
There are 10 survivors left.
5 survivors will return in 9.99s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 9.05s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 7.66s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 6.45s
Type 'x' to make survivors leave, ENTER to see how many are left: x
Survivors already left! Wait a bit!
There are 10 survivors left.
5 survivors will return in 5.73s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 4.15s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 2.90s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 1.72s
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 10 survivors left.
5 survivors will return in 0.48s
Type 'x' to make survivors leave, ENTER to see how many are left: 
Survivors came back!
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left: 
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left: 
于 2013-03-12T14:55:18.690 回答
0

好吧...您没有做任何事情来使脚本停止并等待。

if TimeReturned > time.time():

这只是检查条件是否为真并相应地进行,它不会指示任何人等待。

您应该使用该time.sleep(secs)函数在所需的时间内停止代码的执行。

但是请注意,在睡眠期间,没有执行任何操作。所以,如果你想让一些后台进程继续运行,并且只有你提到的那些人等待,你应该在它自己的线程中运行脚本的那部分。

于 2013-03-12T14:56:45.697 回答
0

你可以用Timer. 它运行指定的秒数,然后执行您指定的函数。ATimer只是使用第二个线程的一种简单方法,这就是它在threading模块中的原因。

from threading import Timer
import time

Survivors = 10

def increase_survivors():
    global Survivors
    Survivors += 5

# start execution
print Survivors
Survivors -= 5

t = Timer(5, increase_survivors)
t.start()
for i in range(10):
    print Survivors
    time.sleep(1)

最后一点只是展示它是如何工作的。不过,管理变量的方法可能比该global语句更好 - 我相信我会得到一些建议。

于 2013-03-12T15:34:02.843 回答