2014-12-30 18 views
-1

所以,我最初嘗試了這種方式,然後認爲這可能會更好。不過,我最終也遇到了同樣的問題。我想在Python中創建一個文本的比賽,我有一類名爲室:在Python中使用計時器返回Json的不同部分

class Room(): 
def __init__(self, id=0, name="A Room", description="An empty room.", description2="A dark and empty room.", neighbors={}): 
    self.id = id 
    self.name = name 
    self.neighbors = neighbors 
    if tick == 1: 
      self.description = description 
     elif tick == 2: 
      self.description = description2 
    else: 
     None 

這個類是從一個JSON文件看起來拉動信息,如:

{ 
"name": "Courtyard", 
"description": "You are standing in a large open air courtyard.", 
"description2": "It is too dark to see.", 
"neighbors": {"w":2} 

}

我也有一個計時器在測試文件,它看起來像設置:

import time 
import sys 

counter = 0 

def tick(count): 
    time_start = time.time() 
    seconds = 0 
    counter = 0 

    while True: 
     time.sleep(1) 
     seconds = int(time.time() - time_start) - counter * 5 
     if seconds >= 5: 
      counter += 1 
      seconds = 0 
    return count 

最後我叫蒂姆呃在我的遊戲主文件,像這樣的底部:

start() 
main(player) 
tick(counter) 

什麼我希望的是,我的房類的說明部分更改爲描述或內容描述2根據存儲在我的定時器計數,但它似乎並沒有像我的計時器工作正常,或者至少我的Room類沒有交換描述。目前,我得到這個回溯:

Traceback (most recent call last): 
    File "game.py", line 80, in <module> 
    main(player) 
    File "game.py", line 77, in main 
    runCMD(input[0], input[1], player) 
    File "game.py", line 64, in runCMD 
    commands[cmd](player, args) 
    File "/home/illyduss/Urth/Commands/commands.py", line 20, in look 
    print (player.loc.description) 
AttributeError: Room instance has no attribute 'description' 

我也試過在我的房間類做這樣的事情:

class Room(): 
def __init__(self, id=0, name="A Room", description="An empty room.", description2="A dark and empty room.", neighbors={}): 
    self.id = id 
    self.name = name 
    self.neighbors = neighbors 
    if tick >= 2: 
      self.description = description 
     else: 
      self.description = description2 

哪隻給了我的描述,從不提供內容描述2,反之亦然,如果我把它改成:

if tick <= 2: 
      self.description = description 
     else: 
      self.description = description2 

我覺得我的AttributeError的是因爲計數器開始在東西比2更高,所以我的if語句是從來沒有在1或2,返回None,這是不是一個DESCRIPT然後它拋出AttributeError。這對我來說沒有意義,因爲我的計數器從0開始,但是我知道我是一個n00b。那麼,理論是否正確,有沒有更好的方法來做到這一點?

回答

0

tick函數。 Your if -statements

if tick == 1: 
    self.description = description 
elif tick == 2: 
    self.description = description2 

從不觸發。


變化tickcounter

if counter == 1: 
    self.description = description 
elif counter == 2: 
    self.description = description2 

並修復tick功能:

def tick(count): 
    global counter # use the global counter variable, not a local one. 
    ... 

這仍然無法工作,雖然,因爲tick不斷增加counter值。一旦其值通過2,您的if-陳述將再次失敗。我無法幫助你解決這個問題,因爲我沒有首先得到一個櫃檯。

+0

我包含了else語句,m170897017給了我檢查並確保它運行,並且它始終打印「對不起,計數器不在1或2!」。所以只是爲了確保,我改變了我的第一,如果如果計數器== 0:等等等等,看起來計數器停留在0,並從不前進。我現在認爲我的問題可能在於我如何存儲我的計數,因爲它保持爲零。 – Illyduss

+0

對不起,花了很長時間來進一步解釋,但有一個計數器的關鍵是創建一個計時器,將改變從描述打印描述description2描述。稍後我將在一天中的不同時間添加一個description3等。最後,我要添加一個重置,如counter = counter == 0,在循環遍歷各種描述之後。這應該給我在這場比賽中的時間元素。之所以花了一段時間,是因爲我現在有了tick()和我的main()線程,所以我可以使用cmd命令行。但由於某種原因,該櫃檯坐在0。 – Illyduss

0

首先,您必須返回counter而不是count中的tick函數。看來你不使用count變量,它通過了tick函數。刪除它。 像這樣:

import time 
import sys 

counter = 0 
# delete variable count 
def tick(): 
    time_start = time.time() 
    seconds = 0 
    counter = 0 

    while True: 
     time.sleep(1) 
     seconds = int(time.time() - time_start) - counter * 5 
     if seconds >= 5: 
      counter += 1 
      seconds = 0 
    return counter 

其次,tick是一個函數而不是一個int對象,因此它永遠不會等於1這意味着self.descriptionRoom類將永遠不會到達。更改Room類到此:

class Room(): 
    def __init__(self, id=0, name="A Room", description="An empty room.", description2="A dark and empty room.", neighbors={}): 
     self.id = id 
     self.name = name 
     self.neighbors = neighbors 
     # use return value of tick function to compare 
     if tick() == 1: 
      self.description = description 
     elif tick() == 2: 
      self.description = description2 
     else: 
      self.description = "Sorry, counter is not 1 or 2!!" 

希望這可以幫助。 :)

+0

這一個創建了我以前有過的一個不同的bug。在我的終端中啓動遊戲後,它不再打印我的提示或採取命令。爲了讓你更好地瞭解我在說什麼,它仍然會啓動程序,但是一旦我達到tick()會激活的點,在我的getRoom階段,它只是一個空行而不是顯示我的命令提示符>>。 – Illyduss

相關問題