Reputation: 1774
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!
Upvotes: 0
Views: 257
Reputation: 365747
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?
Yes. You need to store a "max health" for the player, in addition to his "current health":
if player1.getHealth() > player.getMaxHealth()
player1.setHealth(player.getMaxHealth())
As Padraic explains in a comment, you don't need getters and setters like this in Python, and it's generally considered bad style. So it would be better to write it like this:
if player1.health > player1.max_health:
player1.health = player1.max_health
You can write this a little more concisely using min
:
player1.health = min(player1.health, player1.max_health)
Also, if max health is something you can compute on the fly (maybe 10 + level
?), you can write a function for that, and call that function whenever you need it, instead of storing it as an extra attribute.
One of the neat things about Python is that you can start with an attribute, and change it to a computed value without having to turn it into a function, by using @property
:
@property
def max_health(self):
return 10 + self.level
Upvotes: 1
Reputation: 387707
Just move the responsibility to the player, e.g.:
def changeHealth (self, delta):
self.__health += delta
maxHealth = 10 if self.__level < 5 else 15
if self.__health > maxHealth:
self.__health = maxHealth
elif self.__health < 0
self.__health = 0
Upvotes: 0