Search code examples
pythonclasssubclasslegacy-code

Text Adventure in Python: Working with Classes and Inheritance


I'm fairly new to the idea of classes and subclasses in Python. I've found some questions/answers here on Stackoverflow related to my question, but they don't seem really answer the specifics that I am looking for. I consulted YouTube and other websites, but I'm not having much luck. Now, I might be asking the wrong questions or looking in the wrong places.So, please bear with me for a bit.

Now, I do understand how classes work in Python. That's not the problem.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

This is a relatively basic concept that I think everyone can grasp.

However, I'm having trouble understanding how subclasses work together and how to call up functions that are stored within a class. I want to add a function called "damage" to the class monster or person. I have tried making the class monster inherit the class person so that it also has its properties and can do damage to its health.

I want to create something like this:

class character:
    def __init__(self, name, health ):
        self.name = name
        self.health = health

class  monster(character):
    def __init__(self, name, health):
        character.__init__(self,name,health)
        self.health = health
        self.name = name

    def attack (self):
         self.health-1

john=character("john",22)
boo=monster("boo",20,22)
monster.attack()

By invoking this code outside of the classes, I want to use the function attack so that the health of the user is reduced.

I am still a novice and I have tried various methods.

So that you get a better idea of what I mean, here is the reference code that I am trying to use:

https://trinket.io/python/07c3a147aa

Unfortunately, it's written in legacy code and I am not familiar with all the differences between 2.7 and 3.8. I've tried to adapt and to dissect the code so that I better understand the underlying processes, but I'm not having much luck. It keeps breaking in multiple spots and I keep on getting "Attribute Errors" all over the place.

The thing that I really like about this code is that the character can enter a command like "help" or "attack" and then the function from the respective class is called up. I wanted to modify this code so that it better suits my needs like adding other functions and characters to it, but I am not really having much luck.

If someone could help fix my very flawed code and thinking, I would very appreciative.

I also thank you for taking the time out to read this.

Christopher


Solution

  • Your code has two problems besides the name mix-up you already noticed.

    def attack (self):
         self.health-1
    

    This should be:

    def attack(self):
        self.health -= 1
    

    Your original code subtracted one from the value of health and then threw away the result.

    And the second problem:

    boo=monster("boo",20,22)
    monster.attack()
    

    This should be:

    boo=monster("boo",20)
    boo.attack()
    

    If you want to call a class method you need to do so on an object of that class, not on the class itself.

    Edit: And as noticed by the commenter below, there was one parameter too many.

    Edit2: In regards to your request about the attack function, the obvious solution is:

    john.attack()
    

    (this of course would require a suitable attack function for the character class)

    But maybe you would like the monster to attack john. That could be implemented like this:

    def attack(self, target):
        target.health -= 1
    

    And then for performing the attack:

    boo.attack(john)