0

I have two classes with an image and a self.rect. When the two images touch each other, I was wanting one of the classes to disappear and the other class to gain "points". I used a for loop like this:

For class1 in class1_list:
    for class2 in class2_list:
        if class1.rect.colliderect(class2.rect):
            class1.disappear = True
            class2.points +=1

The problem is, is that if the two classes overlap each other for a while, the colliderect if statement keeps running. I want the colliderect to stop once class1 disappears.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
dogking57
  • 11
  • 4
  • Okay, so look at your code. How are you representing the fact that `class1` "disappeared"? By setting `class1.disappear = True`, yes? So - can you think of a way to write the condition that takes into account whether or not `class1` has disappeared? Perhaps something that involves checking the value of `class1.disappear`? Better yet, maybe you can think of a way to skip the `class1`s that have disappeared, before the inner loop starts. – Karl Knechtel Aug 01 '20 at 06:35
  • Thank you! This is what I was looking for. I added an if statement to check if an object in a class was still alive/visible and it worked! – dogking57 Aug 01 '20 at 06:43
  • Do you need to remember the objects at all? You might consider doing a separate loop afterwards to remove the casualties from the lists. – Karl Knechtel Aug 01 '20 at 07:11

0 Answers0