0

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!

7
  • 1
    not an answer but you don't need those get and set methods, you can access attributes directly. Commented Aug 10, 2014 at 17:02
  • @Robbie: Just use the variables directly. Call it health instead of __health, then use player1.health instead of player1.getHealth() and player1.health = foo instead of player1.setHealth(foo). Commented Aug 10, 2014 at 17:10
  • @Robbie: Also, even if you do need private variables (which you don't in this case), you don't want to use double underscores. Double underscores are only for methods that you need to hide from subclasses (so they can define a method with the same name without accidentally overriding yours). You may want to read PEP 8, the Pythons style guide, which explains all of this and more. Commented Aug 10, 2014 at 17:11
  • Ok makes sense, sorry I accidentally deleted my comment.. Commented Aug 10, 2014 at 17:13
  • By the way, I'm not sure why someone downvoted you, because I think this is a reasonable question (being a novice doesn't make you stupid or lazy, it just makes you a novice). But you could definitely improve it by stripping out all of the irrelevant code (see MCVE) and giving a more specific idea of what part of this you're stuck on. Commented Aug 10, 2014 at 19:17

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

13 Comments

Ok, almost all of that makes sense. Doesn't the min() function choose the smallest of the two values? What happens when the values are the same?
It returns the min value. It doesn't matter if there is more than one minimum.
@Robbie: That's really the kind of thing you can test for yourself more quickly than you can ask. Just open your interpreter and type print(min(2, 2)) and see what you get.
@Abarnert I did try it out, I just didn't understand why it worked.
Also I am getting an error as follows: "Basics/Game.py", line 100, in battle1 enemy.Health = min(enemy.Health(), enemy.enemymaxHealth()) TypeError: 'int' object is not callable". Any ideas?
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.