I'm working with multiple instances of a class called Enemy. I'm calling them from my main method in the Game class, so it looks like this.
class Game(object):
def main(self, screen):
if condition is true:
Enemy(arguments)
elif condition is true:
Enemy(arguments)
class Enemy(pygame.sprite.Sprite):
def __init__(self, location, kind, number, *groups):
self.kind = kind
self.number = str(number)
# Stuff that determines where and which kind
each time Enemy is called it spawns an enemy.
later on I have hit detection that needs to know which enemy is hit so it updates a dictionary. But to update that dictionary for Enemy (and other classes like it) I'll need to access each instance of Enemy and know which one I'm accessing because each have different variables about their location and such at any given time and they are all updating separately and correctly as far as I can tell. Any more information you need I'll be happy to give.
Edit: Looking at it now I do think I simplified too much so I'm going to dump a piece of the dictionary updating code so you can look at it. Note I am a relative novice in coding and more so to classes so this may not all be very well done.
class Game(object):
def main(self, screen):
while 1:
self.hashMap[(topLeftx / acc, topLefty / acc)] = ["Enemy " + Enemy.number]
topLeftx = Enemy.rect[0]
topLefty = Enemy.rect[1]
width = Enemy.rect[2]
height = Enemy.rect[3]
acc = 10
if width > acc:
x = math.floor(width / acc)
for a in range(1, int(x) + 1):
if ((topLeftx / acc) + a, (topLefty / acc)) in self.hashMap.keys():
self.hashMap[((topLeftx / acc) + a, (topLefty / acc))].append("Enemy" + Enemy.number)
else:
self.hashMap[((topLeftx / acc) + a, (topLefty / acc))] = ["Enemy" + Enemy.number]
if height > acc:
y = math.floor(height / acc)
for b in range(1, int(y) + 1):
if ((topLeftx / acc), (topLefty / acc) + b) in self.hashMap.keys():
self.hashMap[((topLeftx / acc), (topLefty / acc) + b)].append("Enemy" + Enemy.number)
else:
self.hashMap[((topLeftx / acc), (topLefty / acc) + b)] = ["Enemy" + Enemy.number]
This is what I tried to do for Enemy but it failed with
AttributeError: type object 'Enemy' has no attribute 'number'
even though it is defined in enemy. The 'number' is my take at doing ID's but it didn't work out. Any tweaks that'd make this work or something entirely new?
Re-edit - I forgot that there is a loop in the main method. All hit detection should be in the main method also. I don't have the calls for Enemy stored as instances, how should I implement that exactly? Thank you for any assistance!