I am working on a simple text based game and, I came across something that I do not know how to do. In my game you gain health, strength, defense, xp and levels as you progress through the game. I made it so whenever there is a battle I don't have to retype it all into the code, by making a function specifically for the battle. So when I want a battle to occur, I just type battle1() to have the battle sequence happen. The only problem with this is the stat variables change so often and in the code I have it set so if you use a potion and are attacked and your health goes above the max health for your character (which varies from level to level) it will be set to the max health you can have (for example, the player uses a potion when they have 15 health, and they're max health is 15, it would just stay at 15 health, rather than increasing to 17). Here is my code:
class player:
def __init__ (self, name, health, strength, defense):
self.__health = health
self.__strength = strength
self.__defense = defense
self.__name = name
def getName(self):
return self.__name
def getHealth(self):
return self.__health
def getStrength(self):
return self.__strength
def getDefense(self):
return self.__defense
def getPotion(self):
return self.__potion
def subHealth(self, num):
self.__health -= num
return self.__health
def setHealth(self, h):
self.__health = h
def main():
name1 = input("What would you like your name to be?")
print("Hello,", name1, "you are on a quest to save otis from the evil Dongus. You must slay him, or Otis will poop.")
player1 = player(name1, 10, 2, 1)
enemy = player("Rat", 15, 0, 0)
print("Your stats are, health:", player1.getHealth(), "strength:", player1.getStrength(), "and defense:", player1.getDefense())
print("Fight.")
attack =input("Type 1 to attack.")
if attack == "1":
enemy.subHealth(player1.getStrength()-enemy.getDefense())
**if enemy.getHealth()>15:
enemy.setHealth(15)**
print(enemy.getName()+"'s health is",enemy.getHealth())
player1.subHealth(enemy.getStrength()-player1.getDefense())
**if player1.getHealth()>10:
player1.setHealth(10)**
main()
The parts that are bolded are the parts of the code I am talking about. So, is there any way to make it so, for example, when you're level 5 your max health is set to 15, rather than ten? Keep in mind, that during the battle your enemies damage you, so you can't just do player1.getHealth(). Thanks!
healthinstead of__health, then useplayer1.healthinstead ofplayer1.getHealth()andplayer1.health = fooinstead ofplayer1.setHealth(foo).