Reputation: 15
I'm trying to make a chose your own adventure game that has various "monsters" that are classes and I'm having trouble accessing the player class in the battle class.
I have tried to make the battle class a method but that didn't work.
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')
It's supposed to access the player that I made at the beginning but it says:
player.getSwordStreangth())
^
SyntaxError: invalid syntax
The player.getSwordStreangth
that fails is in the battle class.
Upvotes: 0
Views: 27
Reputation: 2764
Try:
PS('your sword strength is ' + player.getSwordStreangth())
You missed the +
.
Upvotes: 1