Python 如何访问类中的类?
我正在尝试制作一个你自己选择的冒险游戏,里面有各种各样的“怪物”,它们都是职业,我在战斗职业中很难访问玩家职业 我试着让战斗级成为一种方法,但没有成功Python 如何访问类中的类?,python,class,Python,Class,我正在尝试制作一个你自己选择的冒险游戏,里面有各种各样的“怪物”,它们都是职业,我在战斗职业中很难访问玩家职业 我试着让战斗级成为一种方法,但没有成功 def PS(Input): for x in Input: sys.stdout.write(x) sys.stdout.flush() time.sleep(.05) sys.stdout.write('\n') #this is just making
def PS(Input):
for x in Input:
sys.stdout.write(x)
sys.stdout.flush()
time.sleep(.05)
sys.stdout.write('\n')
#this is just making one letter appear at a time
class Battle:
def __init__(self, HP, S, player):
#making the giant for the battle
giant = Giant(HP, S)
gh = giant.getGiantHealth()
#keeping the battle from stoping after one round
while int(gh) > 0:
#allowing the player to chose if they want to battle
PS('do you fight: ')
F1=input()
if F1 == 'yes':
time.sleep(.5)
#telling the player how strong the giant is
PS('the monster has ' + giant.getGiantHealth() + ' health and ' + giant.getGiantStreangth() + ' strength')
time.sleep(.5)
#the code that doesn't work and is supposed to tell the player how strong there sword is
PS('your sword strength is ' player.getSwordStreangth())
else:
x = ('x')
class Player:
def __init__(self, surviveChance, health, bowPower, swordStrength, bowUnlock):
self.surviveChance = surviveChance
self.health = health
self.bowPower = bowPower
self.swordStrength = swordStrength
self.bowUnlock = bowUnlock
def getSurviveChance(self):
return str(self.surviveChance)
def setSurviveChance(self,x):
self.surviveChance = x
def getBowPower(self):
return str(self.bowPower)
def setBowPower(self, y):
self.BowPower = y
def getSwordStrength(self):
return str(self.swordStrength)
def setSwordStrength(self, z):
self.swordStrength = z
def getBowUnlock(self):
return (self.bowUnlock)
def setBowUnlock(self, a):
self.bowUnlock = a
player = Player(50, 100, 10, 10, 'true')
class Giant:
def __init__(self, giantHealth, giantStreangth):
self.giantHealth = giantHealth
self.giantStreangth = giantStreangth
def getGiantHealth(self):
return str(self.giantHealth)
def removeGiantHealth(self, x):
self.giantHealth = self.giantHealth - x
print('giant health lowered')
def getGiantStreangth(self):
return str(self.giantStreangth)
def setGiantStreangth(self, y):
self.giantStreangth = y
print('giant streangth altered')
它应该访问我在开始时制作的播放器
但它说:
player.get剑术长度()
^
SyntaxError:无效语法
失败的玩家。获取剑术长度在战斗类中。试试:
PS('your sword strength is ' + player.getSwordStreangth())
你错过了+
感谢它如此有效,我花了好几个小时在上面,却没有注意到。我休息了几周,然后又发了一次,谢谢