[英]Why does Python not connect to my class attribute?
我正在尝试制作类似于 MMORPG 的游戏,但它是单人游戏。 我试图将 Player 类放在一个脚本 MaelstromPlayer 上,它的子类 MaelstromMove 使用另一个脚本连接到它,因为我很快就会添加 MaelstromBattle 和 MaelstromInv 类。
问题是我无法访问 MaelstromPlayer 类属性,更不用说调整了。 我想知道是什么导致了这个问题以及如何解决它,或者即使这是可能的。
如果您想知道,我从这个网站https://mail.python.org/pipermail/python-list/2012-January/618880.html获得了“跨多个文件分发类”的方法。
这是我的 MaelstromPlayer 代码,我存储父类的文件。
class Player(object):
def __init__(self):
self.place = int("001")
self.name = input("What name do you want?")
self.knownplaces={}
self.knownplaces[int("001")]="Ruby City"
self.knownplaces[int("002")]="Ruby Inn"
self.knownplaces[int("003")]="Ruby Forests"
self.knownplaces[int("004")]="Ruby Countryside"
self.knownplaces[int("005")]="Witch Hideout"
self.mode="moving"
def __str__(self):
rep = self.movepossible
这是我的 MaelstromMove 代码,我在其中存储 PlayerMove 的子类。
import sys
sys.path.append('F:\Maelstrom\Python\MaelstromPlayer.py')
from MaelstromPlayer import Player
class PlayerMoving(Player):
def __init__(Player):
print('Welcome')
def movepossible(Player,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(Player,position):
if Player.mode=="moving":
position=int(position)
possiblewords=[]
print('Choose between paths:')
possible = Player.movepossible(position)
for m in range(0,len(possible),1):
possiblewords.append(Places.knownplaces[possible[m]])
for n in range(0,len(possiblewords),1):
print(str(n+1)+':'+str(possiblewords[n]))
choice=input('Make your choice...')
#choice=int(choice)
if choice == '':
choice = int('9999999999')
if int(choice) <= len(possiblewords):
Player.place=possible[int(choice)-int('1')]
print("\n")
print("\n")
def showposition(Player):
print('You are at '+Player.knownplaces[int(Player.place)])
test = Player()
while True:
place = test.place
test.move(place)
test.showposition()
sys.path 方法来自: http : //www.daveoncode.com/2017/03/07/how-to-solve-python-modulenotfound-no-module-named-import-error/ 。
请帮忙,最好有代码示例,谢谢。
您的问题是您从不调用PlayerMoving
类(用于底部的测试),只调用Player
类。 同样在PlayerMoving
类中,您覆盖了Player
类的__init__
方法。 要确保Player.__init__
仍然被调用,请执行以下操作:
class PlayerMoving(Player):
def __init__(self):
print('Welcome')
super().__init__()
一些额外的问题:
int("001")
,它效率低下,毫无意义,只是不好的做法。dict
做self.knownplaces = {1: "Ruby City", 2: "Ruby Inn", ...} # etc
null
没有在 python 中定义,也许你的意思是None
但如果你不返回其他任何东西,你会自动return None
。while
循环永远不会结束。 在里面放一个break
子句。Player
作为第一个方法参数。 使用self
就像你会在每个 python 类示例中找到的那样1 2 3 4 5 。最后,您是否注意到来自https://mail.python.org/pipermail/python-list/2012-January/618880.html 的整个线程是关于您永远不应该这样做的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.