Instantiate entity attributes in __init__ rather than in the class definition
This commit is contained in:
parent
61969c46e6
commit
a6cd075b8c
|
@ -9,11 +9,13 @@ class Item(Entity):
|
||||||
A class for items
|
A class for items
|
||||||
"""
|
"""
|
||||||
held: bool
|
held: bool
|
||||||
held_by: Optional["Player"]
|
held_by: Optional[Player]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, held: bool = False, held_by: Optional[Player] = None,
|
||||||
|
*args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.held = False
|
self.held = held
|
||||||
|
self.held_by = held_by
|
||||||
|
|
||||||
def drop(self, y: int, x: int) -> None:
|
def drop(self, y: int, x: int) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -40,8 +42,11 @@ class Heart(Item):
|
||||||
"""
|
"""
|
||||||
A heart item to return health to the player
|
A heart item to return health to the player
|
||||||
"""
|
"""
|
||||||
name: str = "heart"
|
healing: int
|
||||||
healing: int = 5
|
|
||||||
|
def __init__(self, healing: int = 5, *args, **kwargs):
|
||||||
|
super().__init__(name="heart", *args, **kwargs)
|
||||||
|
self.healing = healing
|
||||||
|
|
||||||
def hold(self, player: "Player") -> None:
|
def hold(self, player: "Player") -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -53,15 +58,16 @@ class Heart(Item):
|
||||||
|
|
||||||
class Bomb(Item):
|
class Bomb(Item):
|
||||||
"""
|
"""
|
||||||
A bomb item intended to deal damage to ennemies at long range
|
A bomb item intended to deal damage to enemies at long range
|
||||||
"""
|
"""
|
||||||
name: str = "bomb"
|
|
||||||
damage: int = 5
|
damage: int = 5
|
||||||
exploding: bool
|
exploding: bool
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, damage: int = 5, exploding: bool = False,
|
||||||
super().__init__(*args, **kwargs)
|
*args, **kwargs):
|
||||||
self.exploding = False
|
super().__init__(name="bomb", *args, **kwargs)
|
||||||
|
self.damage = damage
|
||||||
|
self.exploding = exploding
|
||||||
|
|
||||||
def drop(self, x: int, y: int) -> None:
|
def drop(self, x: int, y: int) -> None:
|
||||||
super().drop(x, y)
|
super().drop(x, y)
|
||||||
|
|
|
@ -6,10 +6,22 @@ from ..interfaces import FightingEntity, Map
|
||||||
|
|
||||||
class Monster(FightingEntity):
|
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:
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__()
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def act(self, m: Map) -> None:
|
def act(self, m: Map) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -44,41 +56,37 @@ class Beaver(Monster):
|
||||||
"""
|
"""
|
||||||
A beaver monster
|
A beaver monster
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self, strength: int = 2, maxhealth: int = 20,
|
||||||
super().__init__()
|
*args, **kwargs) -> None:
|
||||||
name = "beaver"
|
super().__init__(name="beaver", strength=strength,
|
||||||
maxhealth = 30
|
maxhealth=maxhealth, *args, **kwargs)
|
||||||
strength = 2
|
|
||||||
|
|
||||||
|
|
||||||
class Hedgehog(Monster):
|
class Hedgehog(Monster):
|
||||||
"""
|
"""
|
||||||
A really mean hedgehog monster
|
A really mean hedgehog monster
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self, strength: int = 3, maxhealth: int = 10,
|
||||||
super().__init__()
|
*args, **kwargs) -> None:
|
||||||
name = "hedgehog"
|
super().__init__(name="hedgehog", strength=strength,
|
||||||
maxhealth = 10
|
maxhealth=maxhealth, *args, **kwargs)
|
||||||
strength = 3
|
|
||||||
|
|
||||||
|
|
||||||
class Rabbit(Monster):
|
class Rabbit(Monster):
|
||||||
"""
|
"""
|
||||||
A rabbit monster
|
A rabbit monster
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self, strength: int = 1, maxhealth: int = 15,
|
||||||
super().__init__()
|
*args, **kwargs) -> None:
|
||||||
name = "rabbit"
|
super().__init__(name="rabbit", strength=strength,
|
||||||
maxhealth = 15
|
maxhealth=maxhealth, *args, **kwargs)
|
||||||
strength = 1
|
|
||||||
|
|
||||||
|
|
||||||
class TeddyBear(Monster):
|
class TeddyBear(Monster):
|
||||||
"""
|
"""
|
||||||
A cute teddybear monster
|
A cute teddybear monster
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self, strength: int = 0, maxhealth: int = 50,
|
||||||
super().__init__()
|
*args, **kwargs) -> None:
|
||||||
name = "teddy_bear"
|
super().__init__(name="teddy_bear", strength=strength,
|
||||||
maxhealth = 50
|
maxhealth=maxhealth, *args, **kwargs)
|
||||||
strength = 0
|
|
||||||
|
|
|
@ -8,22 +8,23 @@ class Player(FightingEntity):
|
||||||
"""
|
"""
|
||||||
The class of the player
|
The class of the player
|
||||||
"""
|
"""
|
||||||
name = "player"
|
|
||||||
maxhealth: int = 20
|
|
||||||
strength: int = 5
|
|
||||||
intelligence: int = 1
|
|
||||||
charisma: int = 1
|
|
||||||
dexterity: int = 1
|
|
||||||
constitution: int = 1
|
|
||||||
level: int = 1
|
|
||||||
current_xp: int = 0
|
current_xp: int = 0
|
||||||
max_xp: int = 10
|
max_xp: int = 10
|
||||||
inventory: list
|
inventory: list
|
||||||
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, maxhealth: int = 20, strength: int = 5,
|
||||||
super().__init__()
|
intelligence: int = 1, charisma: int = 1, dexterity: int = 1,
|
||||||
|
constitution: int = 1, level: int = 1, current_xp: int = 0,
|
||||||
|
max_xp: int = 10, *args, **kwargs) -> None:
|
||||||
|
super().__init__(name="player", maxhealth=maxhealth, strength=strength,
|
||||||
|
intelligence=intelligence, charisma=charisma,
|
||||||
|
dexterity=dexterity, constitution=constitution,
|
||||||
|
level=level, *args, **kwargs)
|
||||||
|
self.current_xp = current_xp
|
||||||
|
self.max_xp = max_xp
|
||||||
self.inventory = list()
|
self.inventory = list()
|
||||||
|
self.paths = dict()
|
||||||
|
|
||||||
def move(self, y: int, x: int) -> None:
|
def move(self, y: int, x: int) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
from random import choice, randint
|
from random import choice, randint
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from dungeonbattle.display.texturepack import TexturePack
|
from dungeonbattle.display.texturepack import TexturePack
|
||||||
|
|
||||||
|
@ -194,9 +194,13 @@ class Entity:
|
||||||
name: str
|
name: str
|
||||||
map: Map
|
map: Map
|
||||||
|
|
||||||
def __init__(self):
|
# noinspection PyShadowingBuiltins
|
||||||
self.y = 0
|
def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
|
||||||
self.x = 0
|
map: Optional[Map] = None):
|
||||||
|
self.y = y
|
||||||
|
self.x = x
|
||||||
|
self.name = name
|
||||||
|
self.map = map
|
||||||
|
|
||||||
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
||||||
-> bool:
|
-> bool:
|
||||||
|
@ -318,16 +322,19 @@ class FightingEntity(Entity):
|
||||||
constitution: int
|
constitution: int
|
||||||
level: int
|
level: int
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
|
||||||
super().__init__()
|
strength: int = 0, intelligence: int = 0, charisma: int = 0,
|
||||||
self.health = self.maxhealth
|
dexterity: int = 0, constitution: int = 0, level: int = 0,
|
||||||
self.health = 0
|
*args, **kwargs) -> None:
|
||||||
self.strength = 0
|
super().__init__(*args, **kwargs)
|
||||||
self.intelligence = 0
|
self.maxhealth = maxhealth
|
||||||
self.charisma = 0
|
self.health = maxhealth if health is None else health
|
||||||
self.dexterity = 0
|
self.strength = strength
|
||||||
self.constitution = 0
|
self.intelligence = intelligence
|
||||||
self.level = 1
|
self.charisma = charisma
|
||||||
|
self.dexterity = dexterity
|
||||||
|
self.constitution = constitution
|
||||||
|
self.level = level
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dead(self) -> bool:
|
def dead(self) -> bool:
|
||||||
|
|
Loading…
Reference in New Issue