2013-03-12 64 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