2013-03-12 60 views
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,10,10直接,它不会延迟时间。如果含糊不清,这是我的第一个问题。

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

  2. 如果我想在脚本中做其他事情,比如发送剩余的幸存者,那么在脚本运行时还是需要等待5秒钟?

感谢您的耐心:)

回答

1

为了使你的脚本等待,使用time.sleep(num_seconds)time.time()只是返回当前时间;它根本没有做任何等待。

可能有一些代码在睡觉,但有其他代码在同一时间运行,做其他事情。要做到这一点,你必须使用threads,但需要一些时间才能习惯。也许this tutorial会有用。

编辑:哦也可以做到这一点没有线程,但你必须仔细跟踪你的变量。你搞砸了你的数学。说time.time()是1000时,你的代码就运行了。然后TimeReturned为1005.假设用户输入Yes需要1秒。然后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: 
+0

好,但不会让整个脚本停止(num_seconds)?我计划让幸存者减少大约20 * 60秒的时间,这样使脚本本身停止这么久会杀死我。如果可能的话,id可以在time.sleep(20 * 60)运行时做其他事情。 – BlueLance 2013-03-12 15:08:06

+0

@ user2146277:会的。你可以不睡觉,看看我的更新。你刚刚得到了数学后退 – Claudiu 2013-03-12 17:06:19

+0

该脚本的作品奇迹,我也阅读了教程和其他一些人,并了解更多线程中发生了什么。 为了让它能够同时发送多个幸存者的浪潮,是否有可能,或者最好在线程中完成? 感谢您的全力帮助:) – BlueLance 2013-03-12 18:40:09

0

好...你没做什么,以使脚本停顿和等待。

if TimeReturned > time.time(): 

这只是检查条件是否为真,并据此进行,它并没有指示任何人等待。

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

尽管如此,在睡眠期间,没有任何执行。所以,如果你想要一些后台进程继续运行,并且只有你提到的那些人等待,你应该在自己的线程中运行脚本的那部分。

+0

不幸的是我不知道如何创建线程(不是我至少知道的)。我是Python新手和编程人员,并决定是否开始一个项目,我可以学习所需的东西,建立它,它已经工作到目前为止,但试图做这部分刚刚造成了问题。我会查找它:)谢谢:)克劳迪已经提供给我一个线程的链接,这应该有所帮助:) – BlueLance 2013-03-12 15:14:09

0

你可以用Timer做你想做的。它运行指定的秒数,然后执行您指定的功能。 A Timer只是使用第二个线程的简单方法,这就是为什么它在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) 

最后一点只是说明它是如何工作的。尽管可能有更好的方法来管理变量,但我相信我会得到一些建议。

+0

不仅仅是在定时器运行5秒后让幸存者增加5点幸存者= 15? – BlueLance 2013-03-12 15:38:34

+0

我错过了你开始减少幸存者的第一个位。无论如何,重要的部分是在改变变量之前等待,不是吗? – wrgrs 2013-03-12 15:52:04