Instantiate entity attributes in __init__ rather than in the class definition

This commit is contained in:
Yohann D'ANELLO
2020-11-18 14:54:21 +01:00
parent 61969c46e6
commit a6cd075b8c
4 changed files with 82 additions and 60 deletions

View File

@ -6,11 +6,23 @@ from ..interfaces import FightingEntity, Map
class Monster(FightingEntity):
"""
The class for all monsters in the dungeon
The class for all monsters in the dungeon.
A monster must override this class, and the parameters are given
in the __init__ function.
An example of the specification of a monster that has a strength of 4
and 20 max HP:
class MyMonster(Monster):
def __init__(self, strength: int = 4, maxhealth: int = 20,
*args, **kwargs) -> None:
super().__init__(name="my_monster", strength=strength,
maxhealth=maxhealth, *args, **kwargs)
With that way, attributes can be overwritten when the entity got created.
"""
def __init__(self) -> None:
super().__init__()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def act(self, m: Map) -> None:
"""
By default, a monster will move randomly where it is possible
@ -44,41 +56,37 @@ class Beaver(Monster):
"""
A beaver monster
"""
def __init__(self) -> None:
super().__init__()
name = "beaver"
maxhealth = 30
strength = 2
def __init__(self, strength: int = 2, maxhealth: int = 20,
*args, **kwargs) -> None:
super().__init__(name="beaver", strength=strength,
maxhealth=maxhealth, *args, **kwargs)
class Hedgehog(Monster):
"""
A really mean hedgehog monster
"""
def __init__(self) -> None:
super().__init__()
name = "hedgehog"
maxhealth = 10
strength = 3
def __init__(self, strength: int = 3, maxhealth: int = 10,
*args, **kwargs) -> None:
super().__init__(name="hedgehog", strength=strength,
maxhealth=maxhealth, *args, **kwargs)
class Rabbit(Monster):
"""
A rabbit monster
"""
def __init__(self) -> None:
super().__init__()
name = "rabbit"
maxhealth = 15
strength = 1
def __init__(self, strength: int = 1, maxhealth: int = 15,
*args, **kwargs) -> None:
super().__init__(name="rabbit", strength=strength,
maxhealth=maxhealth, *args, **kwargs)
class TeddyBear(Monster):
"""
A cute teddybear monster
"""
def __init__(self) -> None:
super().__init__()
name = "teddy_bear"
maxhealth = 50
strength = 0
def __init__(self, strength: int = 0, maxhealth: int = 50,
*args, **kwargs) -> None:
super().__init__(name="teddy_bear", strength=strength,
maxhealth=maxhealth, *args, **kwargs)