0

I am pretty new in python and having a hard time with classes.

Here is the guide:

  • Add a Boss class which inherits all of the features of the Character class.
  • Add the following method to the Boss class: spawn(), generates a Minion named "Minion" with HP and attack equal to 1/4 (rounded down) of the Boss's max. The Minion should be the same level as the Boss but have 0 exp and 0 defense.

So how to generates a Minion named "Minion" with HP and attack equal to 1/4 (rounded down) of the Boss's max?

if I try this line of code, and give each of the variable its difference method, in that case where "spawn" method should go?

class Boss(Character):
    def __init__(self, name=Minion, lvl=1, hp=100, at=10, df=1, exp=None):
        self.name = name
        self.lvl = lvl
        self.hp = hp
        self.at = at
        self.df = df

    def spawn(self):
pppery
  • 3,731
  • 22
  • 33
  • 46
  • 1
    `return Character(name="minion", at=0,....)` that's all and it goes under `spawn`. This creates a new character with the specified stats. I don't know if you need a `Boss.__init__` and I **know** the Boss init shouldn't take Minion as a default name. – JL Peyret Feb 20 '20 at 20:13
  • 1
    I'm not sure if that's true @JLPeyret as it seems like `Minion` is its own class. Can you share a stub of the Character class as well? Perhaps the Minion class too so folks know what the initialization will look like. – LeKhan9 Feb 20 '20 at 20:14
  • 1
    if what's true? – JL Peyret Feb 20 '20 at 20:15
  • 1
    I believe Minion would be its own class (also a Character). It wouldn't make sense for a Boss to generate a raw Character object that it is inheriting from, makes more sense for Boss to generate a Minion object. – LeKhan9 Feb 20 '20 at 20:18
  • 2
    You should call `super().__init__(...)` – Tomerikoo Feb 20 '20 at 20:21
  • why do you need a Minion class? does it have any characteristics or methods that are not described by its name, hp, at, df, attributes that are presumably present in the Character class? – JL Peyret Feb 20 '20 at 20:26
  • thank guys! I think @JLPeyret is right –  Feb 20 '20 at 20:32

2 Answers2

1

The Guide says Boss is just like Character except for the addition of spawn, so you shouldn't be defining __init__ (Character already has one) and should be writing the body of spawn.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

thank you for reaching out for help about this. There are actually quite a lot of tutorials on classes and how they work. I'd suggest finding a blog, book, or video and just work through it.

However for your particular issue I'd suggest setting up your minion as it's own class. Then setting up your boss with a spawn method that creates minions. You'll need the super constructor for this to work.

The idea behind this is that your class "hierarchy" will look like this:

         Object
           | 
        Character
        |
      Boss

What this shows us is that all of your classes will be children of Object. Boss is a child of Character. It is common to hear this relationship described as a "is a". i.e. Boss is a Character.

Moving forward.

Let's imagine your character class looks like this:

class Character(Object):
    def __init__(self, name="Placeholder",
                 level=1, hp=10, attack=10, 
                 defense=10, experience=0):
        self.name = name
        self.level = level
        self.hp = hp
        self.attack = attack
        self.defense = defense
        self.experience = experience

Spawning a minion from you boss is rather simple. Try constructing a new character with the relevant stats. This might look something like the following:

class Boss(Character):
    '''
     Defines the boss class.
     Which by default has the following values:

       Name: "Boss"
       level: 100
       hp: 10000
       attack: 1000
       defense: 1000
       experience: 10000 
    '''
    def __init__(self, name="Boss", level=100,
                 hp=10000, attack=1000, defense=1000,
                 defense=1000, experience=10000):
        super(Boss, self).__init__(
            name=name, level=level,
            hp=hp, attack=attack,
            defense=defense, experience=experience
        )

    def spawn(self):
        '''
        Spawns a single minion
        '''
        return Character(name="Minion", level=self.level,
                         hp=round(self.hp / 4),
                         attack=round(self.attack / 4),
                         defense=0, experience=0)

There are tons of other ways to accomplish this using other paradigms, but this is a good starting point for OOP. If you'd like a little help with types and even spawning different kinds of minions I'd suggest researching enums in python.

EDIT: Example including Minion subclass

This would change your inheritance tree like so:

         Object
           | 
        Character
        |      |
      Boss    Minion

Which, again. Means both your Boss and Minion are children of Character and thus by extension are children of Object. You can describe this as: "Boss and Minion are Characters".

Edited code below:

class Character(Object):
    def __init__(self, name="Placeholder",
                 level=1, hp=10, attack=10, 
                 defense=10, experience=0):
        self.name = name
        self.level = level
        self.hp = hp
        self.attack = attack
        self.defense = defense
        self.experience = experience

class Minion(Character):
    '''
    TODO: define any special attributes,
          abilities, etc of the minion
    '''
    pass

class Boss(Character):
    '''
     Defines the boss class.
     Which by default has the following values:

       Name: "Boss"
       level: 100
       hp: 10000
       attack: 1000
       defense: 1000
       experience: 10000 
    '''
    def __init__(self, name="Boss", level=100,
                 hp=10000, attack=1000, defense=1000,
                 defense=1000, experience=10000):
        super(Boss, self).__init__(
            name=name, level=level,
            hp=hp, attack=attack,
            defense=defense, experience=experience
        )

    def spawn(self):
        '''
        Spawns a single minion
        '''
        return Minion(name="Minion", level=self.level,
                         hp=round(self.hp / 4),
                         attack=round(self.attack / 4),
                         defense=0, experience=0)

Taylor Cochran
  • 439
  • 4
  • 16
  • Thank you for your effort, I appreciate it. Going back to out question , the thing is it doesn't finish with Boss class, but also has its different "Minion" class also inherits Character class. –  Feb 20 '20 at 20:48
  • whole assignment is pretty complicated, I'm trying to figure it out, thanks you all for your help –  Feb 20 '20 at 20:49
  • @MohammedAlimbetov I'm not understanding your first comment fully. Are you saying the assignment wants you to build out a Minion class and a Boss class? – Taylor Cochran Feb 20 '20 at 22:37
  • @TaylorCochran Yes. so its different minion class which also needs to be created. Would you mind leave you contact method(email, facebook) so I can send you whole assignment, if you wanna take a look? I am really struggling with it –  Feb 21 '20 at 02:24
  • 1
    @MohammedAlimbetov if that's the case, you can just have `class Minion(Character): pass` and then `return Minion(...)` in `spawn`. now you specifically get a Minion. – JL Peyret Feb 21 '20 at 02:59
  • @MohammedAlimbetov I've edited the post with an example of JL's suggestion – Taylor Cochran Feb 21 '20 at 14:49