-1

Having been here: Python class inherits object, and here What is a metaclass in Python? I'm still unsure of how to get my code working. I'll be honest those explanations were a bit complicated for me. (BTW This is all in Python 3.4).

I'm trying to make Objects for my game in the form of creatures. Example: I want an Object called Dragon, but I want Dragon to inherit the class Entity, and the class Monster.

Here's what I've got:

class Entity:
    id = 0
    xLocation= 0
    yLocation = 0
    name = ''
    description = ''
    def __init__(self, id, xLocation, yLocation, name, description):
        self.xLocation = xLocation
        self.yLocation = yLocation
        self.id = id
        self.name = name
        self.description = description


class Monster:
    life = 0
    pierce = 0
    slash = 0
    blunt = 0
    pierceReduction = 0
    slashReduction = 0
    bluntReduction = 0
    defenseBonus = 0
    score = 0
    def __init__(self, pierceReduction, bluntReduction, slashReduction, price, name, score):
        self.pierceReduction = pierceReduction
        self.bluntReduction = bluntReduction
        self.slashReduction = slashReduction
        self.price = price
        self.name = name
        self.score = score



class Structure:
    pass

Monster = Entity
Dragon = Monster

I don't know how I can give Dragon all of the values of both an entity and a Monster? Obviously setting Dragon directly to monster doesn't work.

*****I've now tried this*****:

class Structure: pass

class Dragon(Entity, Monster): pass

Dragon(10, 10, 10, 1000, 'Dragon', 1000)

The Dragon is saying it doesn't have all the parameters of both classes though?

Community
  • 1
  • 1
Lord Farqwhad
  • 31
  • 1
  • 4
  • Is a monster always a type of entity? And a dragon always a type of monster? – 101 Nov 05 '14 at 04:50
  • If you are going to use inheritance, don't forget about calling the parent class method implementations. (`__init__` in particular) – warvariuc Nov 05 '14 at 05:25
  • Dragons are always a monster and entity. Thank you very much viraptor and serkos! (My rep isn't high enough to upvote you both). P.S. warvariuc I don't know what you mean. – Lord Farqwhad Nov 06 '14 at 00:20

2 Answers2

0

You can inherit from Entity class: class Monster(Entity): in declaration. You can find detailed information in documentation

serkos
  • 306
  • 1
  • 4
0

Python can have classes inheriting from multiple parents. You can simply do:

class Dragon(Monster, Entity):
    # class details

Just make sure that the resolution order between those classes is what you expect. Search for "python mro" for more details about it.

viraptor
  • 33,322
  • 10
  • 107
  • 191