如何访问班级中的班级?

时间:2019-04-24 20:33:27

标签: 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 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.getSwordStreangth())
    ^
SyntaxError: invalid syntax

失败的player.getSwordStreangth在战斗舱中。

1 个答案:

答案 0 :(得分:1)

尝试:

PS('your sword strength is ' + player.getSwordStreangth())

您错过了+