所以,我最初尝试的方式不同,然后认为这可能会更好。但是我最终遇到了同样的问题。我正在尝试用Python创建一个文本游戏,我有一个名为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
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)
我希望的是我的Room类的描述部分根据我的计时器中存储的计数更改为描述或描述2,但它似乎不是我的计时器工作正常或至少我的房间class没有交换描述。目前我得到这个追溯:
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'
我也试过在我的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
if tick >= 2:
self.description = description
else:
self.description = description2
其中只给了我描述并且从未提供过description2,反之亦然,如果我将其更改为:
if tick <= 2:
self.description = description
else:
self.description = description2
我认为我的AttributeError是因为计数器从高于2的值开始,因此我的if语句永远不会在1或2并且返回None,这不是描述然后它会抛出AttributeError。这对我没有意义,因为我的计数器从0开始,但我知道我是一个n00b。那么,这个理论是否正确,有更好的方法吗?
答案 0 :(得分:0)
tick
是功能。您的if
- 陈述
if tick == 1:
self.description = description
elif tick == 2:
self.description = description2
永远不会触发。
将tick
更改为counter
:
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
- 语句将再次失败。我无法帮助你解决这个问题,因为我一开始就没有办法解决这个问题。
答案 1 :(得分: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.description
类中的Room
。将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!!"
希望这会有所帮助。 :)