我正在构建一个基于Python的、单人的、基于单词的MMORPG游戏。我是一个初学者,希望这是一个简单的任务。我对移动部分进行了编码,其中角色从一个站点移动到另一个站点。它似乎不起作用,因为Python似乎无法看到我的属性。这是错误消息:
Traceback (most recent call last): File "C:/Users/lenovo/Desktop/Maelstrom/Maelstrom Move.py", line 51, in place = test.place AttributeError: 'Player' object has no attribute '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()
在执行行
place = test.place
时,尚未定义Player
实例上的place
属性第一次设置
place
属性是在move()
方法中。i、 e.在调用move()
之前尝试访问place
将导致您观察到的错误应该为
Player
类的初始值设定项中的self.place
指定一个默认值相关问题 更多 >
编程相关推荐