From a6cd075b8c743d768ad05027d5030da1840787fa Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 18 Nov 2020 14:54:21 +0100 Subject: [PATCH] Instantiate entity attributes in __init__ rather than in the class definition --- dungeonbattle/entities/items.py | 26 ++++++++------ dungeonbattle/entities/monsters.py | 56 +++++++++++++++++------------- dungeonbattle/entities/player.py | 21 +++++------ dungeonbattle/interfaces.py | 39 ++++++++++++--------- 4 files changed, 82 insertions(+), 60 deletions(-) diff --git a/dungeonbattle/entities/items.py b/dungeonbattle/entities/items.py index f09e657..dbfd455 100644 --- a/dungeonbattle/entities/items.py +++ b/dungeonbattle/entities/items.py @@ -9,11 +9,13 @@ class Item(Entity): A class for items """ 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) - self.held = False + self.held = held + self.held_by = held_by def drop(self, y: int, x: int) -> None: """ @@ -40,8 +42,11 @@ class Heart(Item): """ A heart item to return health to the player """ - name: str = "heart" - healing: int = 5 + healing: int + + def __init__(self, healing: int = 5, *args, **kwargs): + super().__init__(name="heart", *args, **kwargs) + self.healing = healing def hold(self, player: "Player") -> None: """ @@ -53,15 +58,16 @@ class Heart(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 exploding: bool - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.exploding = False + def __init__(self, damage: int = 5, exploding: bool = False, + *args, **kwargs): + super().__init__(name="bomb", *args, **kwargs) + self.damage = damage + self.exploding = exploding def drop(self, x: int, y: int) -> None: super().drop(x, y) diff --git a/dungeonbattle/entities/monsters.py b/dungeonbattle/entities/monsters.py index f006ed3..1f04372 100644 --- a/dungeonbattle/entities/monsters.py +++ b/dungeonbattle/entities/monsters.py @@ -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) diff --git a/dungeonbattle/entities/player.py b/dungeonbattle/entities/player.py index 0c2883b..6abe397 100644 --- a/dungeonbattle/entities/player.py +++ b/dungeonbattle/entities/player.py @@ -8,22 +8,23 @@ class Player(FightingEntity): """ 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 max_xp: int = 10 inventory: list paths: Dict[Tuple[int, int], Tuple[int, int]] - def __init__(self): - super().__init__() + def __init__(self, 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, + 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.paths = dict() def move(self, y: int, x: int) -> None: """ diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 00751e4..ae0185d 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -2,7 +2,7 @@ from enum import Enum, auto from math import sqrt from random import choice, randint -from typing import List +from typing import List, Optional from dungeonbattle.display.texturepack import TexturePack @@ -141,7 +141,7 @@ class Map: """ self.width = d["width"] self.height = d["height"] - self.start_y = d["start_y"] + self.start_y = d["start_y"] self.start_x = d["start_x"] self.currentx = d["currentx"] self.currenty = d["currenty"] @@ -192,11 +192,15 @@ class Entity: y: int x: int name: str - map: Map + map: Map - def __init__(self): - self.y = 0 - self.x = 0 + # noinspection PyShadowingBuiltins + def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None, + 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)\ -> bool: @@ -318,16 +322,19 @@ class FightingEntity(Entity): constitution: int level: int - def __init__(self): - super().__init__() - self.health = self.maxhealth - self.health = 0 - self.strength = 0 - self.intelligence = 0 - self.charisma = 0 - self.dexterity = 0 - self.constitution = 0 - self.level = 1 + def __init__(self, maxhealth: int = 0, health: Optional[int] = None, + strength: int = 0, intelligence: int = 0, charisma: int = 0, + dexterity: int = 0, constitution: int = 0, level: int = 0, + *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + self.maxhealth = maxhealth + self.health = maxhealth if health is None else health + self.strength = strength + self.intelligence = intelligence + self.charisma = charisma + self.dexterity = dexterity + self.constitution = constitution + self.level = level @property def dead(self) -> bool: