class Ins_Normal():
def getInstructions(self, *args):
return "GetInstructions_Normal"
class Ins_Agressive():
def getInstructions(self, *args):
return "GetInstructions_Agressive"
class Enemies(object):
def __init__(self, behaivor = 'Ins_Normal', mode = True, *args):
self.behaivor = behaivor
self.mode = mode
def getInstructions(self, *args):
#create instance based on behaivor
try:
ins = globals()[self.behaivor]() # same like ins = Ins_Agressive()
except KeyError as e:
raise NotImplementedError(e)
# return getInstructions() init value behaivor
return ins.getInstructions()
def xyz(self, *args):
# if True return worldspace position (eg)
if self.mode:
return "xyz"
return 'com_xyz'
def CreateEnemy(enemy = 0, behaivor = 'Ins_Normal', mode =True):
# wrapper function to collect Enemies
# create dict, so no hardcoded variabels, scalable....
data = dict((_id, Enemies(behaivor, mode)) for _id in range(enemy))
return data
# create groups of enemies
enemies_com_normal = CreateEnemy(3, 'Ins_Normal') #com
enemies_user_normal = CreateEnemy(3, 'Ins_Normal', True) #interactive
enemies_com_agressive = CreateEnemy(5, 'Ins_Agressive', True) #interactive
print enemies_com_normal
# we get dict of Enemy instances with id(int) as key
#>>> {0: <Enemies object at 0x7f2d8cfe5b10>, 1: <Enemies object at 0x7f2d8cfe5b50>, 2: <Enemies object at 0x7f2d8cfe5b90>}
#lets print some infos of the agents
print enemies_com_normal[0].xyz()
#>>>xyz
print enemies_com_normal[0].getInstructions()
#>>>GetInstructions_Normal
print enemies_com_agressive[2].getInstructions()
#>>>GetInstructions_Agressive
enemies_com_hgd = CreateEnemy(5, 'Ins_HGD', True) #interactive
print enemies_com_hgd[0].getInstructions()
#Traceback (most recent call last):
# File "python", line 56, in <module>
# File "python", line 21, in getInstructions
#NotImplementedError: 'Ins_HGD' <<<<< no Instruction Implemented for Ins_HGD
Enemiesが定義されているコードスニペットをお願いしますか?多分問題があるかもしれません。 – Alex