我正在构建一个基于Python的单人游戏,基于单词的MMORPG游戏。我是初学者,希望这是一项简单的任务。我已经编码了移动部分,其中角色从一个站点移动到另一个站点。它似乎行不通,因为Python似乎无法看到我的属性。这是错误消息:
追踪(最近一次通话): 文件" C:/ Users / lenovo / Desktop / Maelstrom / Maelstrom Move.py",第51行,in place = test.place AttributeError:'播放器'对象没有属性' place'
这是我的代码:
class Player(object):
"""The player."""
def __init__(self,name="name",inv=[],equip=[],stats=[],place=int("001"),knownplaces={}):
self.name = input("What name do you want?")
knownplaces[int("001")]="Ruby City"
knownplaces[int("002")]="Ruby Inn"
knownplaces[int("003")]="Ruby Forests"
knownplaces[int("004")]="Ruby Countryside"
knownplaces[int("005")]="Witch Hideout"
def __str__():
rep = self.movepossible
def movepossible(self,position):
#001--Ruby City
#002--Ruby Inn
#003--Ruby Forests
#004--Ruby Countryside
#005--Witch Hideout
if position==int("001"):
possible=[int("002"),int("003")]
return possible
elif position==int("002"):
possible=[int("001")]
return possible
elif position==int("003"):
possible=[int("001"),int("004")]
return possible
elif position==int("004"):
possible=[int("001"),int("003"),int("005")]
return possible
elif position==int("005"):
possible=[int("004")]
return possible
else:
return null
def move(self,position):
possiblewords=[]
print('Choose between paths:'/n)
possible = movepossible(self, position)
for m in range(0,len(possible),1):
possiblewords.append(knownplaces[possible[m]])
for n in range(0,len(possiblewords),1):
print(m+':'+possiblewords[m-1] /n)
choice=input('Make your choice...')
if choice-1 <= len(possiblewords):
self.place=possible[choice-1]
def showposition(self):
print(knownplaces[self.place])
test = Player()
while True:
place = test.place
test.move(place)
test.showposition()
答案 0 :(得分:1)
执行第place = test.place
行时,place
实例上的Player
属性尚未定义。
第一次place
属性设置在move()
方法中。即在调用place
之前尝试访问move()
将导致您观察到的错误。
您应该在self.place
类的初始值设定项中为Player
指定默认值。