-1

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!

ToxicGLaDOS
  • 441
  • 5
  • 16
  • 4
    so what is the question..? – Anshul Goyal Sep 14 '14 at 05:58
  • The instances will have different ID's, `id(instance)` ... ... or, you could look at this answer: http://stackoverflow.com/a/8628132/351031. – g.d.d.c Sep 14 '14 at 06:00
  • 1
    I think you've simplified the code in your question too much. What are you doing with your `Enemy` instances after you create them? How are you storing them? These are essential details to making any sort of attempt at answering your question. – Blckknght Sep 14 '14 at 06:03

3 Answers3

2

The easiest method for 'differentiating between instances of a class' is to use IDs (e.g. each Enemy has a unique id that distinguishes it from the others). Because you have these Enemies in a dictionary (dict), their id could be their dict key.

It might help me to see how your hit detection works for a more complete answer.

Hope this helps.

owns
  • 305
  • 2
  • 7
0

Presumably you are iterating through a list of Enemy objects. When you detect a "hit", then you should immediately have the instance for the Enemy which was hit. If you have implemented this algorithm carefully enough, you shouldn't need any other way to identify that particular Enemy instance.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Your error is that instead of using Enemy.…, which refers to the Enemy class (which indeed does not have any number attribute), your code should refer to an Enemy instance (which does have a number attribute as per your constructor).

Similarly, in your Game.main() method, you should do something with the Enemy instances that you create (like saving them somewhere): they are the objects that have a number attribute, so they are the objects that your processing code should handle.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • What exactly would I have to put in for it to refer to an instance of Enemy the way things are now as you see them? – ToxicGLaDOS Sep 14 '14 at 06:37
  • I added a comment about `Game.main()` having to do maybe save the instances that you create. If this does not help you enough, then we definitely need more information; for instance, what is the context of your "dictionary updating code"? basically, it should be given an *instance*. With more information about your code and intent I can guide you more. – Eric O. Lebigot Sep 14 '14 at 06:40
  • I could give the entire code to you personally if you REALLY wanted to help me out (it's only 314 lines) but aside from that I can tell you that in the main loop there is a variable of self.hashMap which is a dictionary that is populated by that second block of code there for each "thing" bullets, enemies ect... and if "Bullet" and "Enemy 0" show up in the same value Enemy 0 needs to die. – ToxicGLaDOS Sep 14 '14 at 06:52
  • The second block of code that you give is embedded in what? Again, and I agree with Blckknght, we need to know what you do with the `Enemy` instances that you create (in `Game.main()`). You should update your question with this information, if it is to be useful to other people. Also, the context of your second block (the one that uses the `Enemy` class instead of using instances) should be explained (do you have a loop on some objects?…). – Eric O. Lebigot Sep 14 '14 at 08:26