我正在尝试制作一个类似MMORPG的游戏,但它是单人游戏。我试图将玩家类放在一个脚本上,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__
,请执行以下操作:还有一些问题:
int("001")
,这是低效的、毫无意义的,而且是糟糕的做法李>dict
doself.knownplaces = {1: "Ruby City", 2: "Ruby Inn", ...} # etc
null
没有在python中定义,也许您的意思是None
,但是如果不返回任何其他内容,您将自动return None
李>while
循环永远不会结束。把一个break
子句放在或什么东西里李>Player
作为第一个方法参数。使用self
就像在每个python类示例中一样1{a2}{a3}{a4}{a5}李>最后,您是否注意到https://mail.python.org/pipermail/python-list/2012-January/618880.html中的整个线程都是关于您应该如何从不这样做的
相关问题 更多 >
编程相关推荐