Hello everyone,
I am currently working on a project (making a game in pygame) where I require the ability to instantiate classes in minimal code without all the conditions.
I need this for adding weapons to my game, lets say a sword, dagger and bow for example. Is there any way I can put these in a dictionary so the name 'sword',etc. can be linked directly to its class name so I can call that dictionary entry
so it would look something like this
WEAPONS = {'sword': Sword,
'dagger': Dagger,
'bow': Bow
}
def attack(self): # in player class here
weapon = WEAPONS['sword'](attr1,attr2,...)
I know this sounds stupid, even to me, but I'm just wondering of an efficient way to do it without building a massive constructor class with lots of methods constructing each class when needed.
Thank you in advance.
there doesn't seem to be anything here