For example, I am trying to build an enemy class for a simple game. Each enemy that spawns has a type which affects it stats which are fields in the enemy class.
class Enemy:
#Base Stats for all enemies
name = "foo"
current_health = 4
max_health = 4
attack = 0
defense = 0
armor = 0
initiative = 0
initMod = 0
alive = True
Should each type be a subclass of enemy like so..
class goblin(Enemy):
name = goblin;
current_health = 8
max_health = 8
attack = 3
defense = 2
armor = 0
def attack(){
//goblin-specific attack
}
But this method means that I would have to build a class for each individual type (which would be 80+ classes), or is there a better way to do it? This enemy is going to be randomized, so I was thinking the types could also be put into a dictionary which uses the names of the types as keywords. Although I'm not entirely sure how I could implement that.
If you want to go the dictionary route you could do something like this with a tuple being returned by the key:
enemy_types = {"goblin": ("goblin", 8, 8, 3, 2, 0, goblin_attack)}
def goblin_attack(enemy):
do_stuff()
Though you may want to use a named_tuple
https://stackoverflow.com/a/13700868/2489837
to make sure you don't mix up the fields in the tuple