I decided to improve my OOP design knowledge and learn python at the same time so I started to write a simple program which will be a fight "simulator".
For now, there are only 2 character types : Orc and Human.
The CharacterFactory
class is created so that I'll be able to handle Orc
and Human
classes in a more abstract way or so.
GameState
is meant to track the state of the game. Instances will be immediately registered to the GameState
as soon as they created. And for example, if a character dies, I want to remove it from the list as well. (self.characters
).
#abtract factory or something
class CharacterFactory:
def __init__(self, character=None, game_state=None):
self.character = character
game_state.register_character(self)
def inspect(self):
print(self.character.name)
print(self.character.attack_dmg)
print(self.character.health)
print("\n")
def attack(self, target):
target.character.health = target.character.health - self.character.attack_dmg
#Observable or so
class GameState:
def __init__(self):
self.characters = []
def register_character(self, character):
self.characters.append(character)
def show_characters(self):
list(map(lambda x: x.inspect(), self.characters))
class Orc:
def __init__(self,name):
self.name = name
self.attack_dmg = 50
self.health = 100
class Human:
def __init__(self,name):
self.name = name
self.attack_dmg = 45
self.health = 105
def Main():
game_state = GameState()
orc = CharacterFactory(Orc("Karcsi"),game_state)
#orc.inspect()
#orc.attack()
human = CharacterFactory(Human("Nojbejtoo"),game_state)
#human.inspect()
#human.attack()
print("Game state:\n")
game_state.show_characters()
orc.attack(human)
game_state.show_characters()
Main()
My question is:
Is this a good design? Or maybe a complete pain in the ... to work with? Is there something that I could improve?
Of course this code is really far from being finished, but I try to find the best approach possible to design things like this so that i can deepen my knowledge in design patterns and stuff like that.
1 Answer 1
The CharacterFactory class is created so that I'll be able to handle Orc and Human classes in a more abstract way or so.
You try to stay DRY which is very good, but this idea would be better represented with inheritance.
You could create a class Character()
and let both Human
and Orc
, inherit from the super class like so:
class Character():
def __init__(self, health, attack_damage, name):
self.attack_damage = attack_damage
self.health = health
self.name = name
def attack(self, target):
target.health -= self.attack_damage
def __str__(self):
return f"Name: {self.name}\nDamage: {self.attack_damage}\nHealth: {self.health}\n"
class Human(Character):
def __init__(self, name, health=105, attack_damage=45):
super().__init__(health, attack_damage, name)
class Orc(Character):
def __init__(self, name, health=100, attack_damage=50):
super().__init__(health, attack_damage, name)
def main():
orc = Orc("Karcsi")
human = Human("Nojbejtoo")
print(orc)
print(human)
orc.attack(human)
print(human)
if __name__ == "__main__":
main()
Things I changed:
- Instead of an inspect function, I override the magic function
__str__
so you can directlyprint(human)
orprint(orc)
- The use of the
if __name__ == '__main__'
- And the
snake_case
functionsmain
instead ofMain
- In the
attack
function body, see howa = a - b
equalsa -= b
I have omitted the GameState
part, maybe someone else will pick that up.
Good luck with your game!
-
\$\begingroup\$ Okay, then I'll go with inheritance instead. Thank you for the hints :) ! \$\endgroup\$Norbert Braun– Norbert Braun2018年08月25日 14:54:55 +00:00Commented Aug 25, 2018 at 14:54
Explore related questions
See similar questions with these tags.