所以,我最初嘗試了這種方式,然後認爲這可能會更好。不過,我最終也遇到了同樣的問題。我想在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。那麼,理論是否正確,有沒有更好的方法來做到這一點?
我包含了else語句,m170897017給了我檢查並確保它運行,並且它始終打印「對不起,計數器不在1或2!」。所以只是爲了確保,我改變了我的第一,如果如果計數器== 0:等等等等,看起來計數器停留在0,並從不前進。我現在認爲我的問題可能在於我如何存儲我的計數,因爲它保持爲零。 – Illyduss
對不起,花了很長時間來進一步解釋,但有一個計數器的關鍵是創建一個計時器,將改變從描述打印描述description2描述。稍後我將在一天中的不同時間添加一個description3等。最後,我要添加一個重置,如counter = counter == 0,在循環遍歷各種描述之後。這應該給我在這場比賽中的時間元素。之所以花了一段時間,是因爲我現在有了tick()和我的main()線程,所以我可以使用cmd命令行。但由於某種原因,該櫃檯坐在0。 – Illyduss