0

I'm trying to check for collisions, but whenever I try and run my code I receive an
AttributeError: 'int' object has no attribute 'colliderect').

Happens in this section of the code, the line underneath the second for loop.

# Collision

for i1 in range(len(enemies)):
    for i2 in range(1, len(enemies)):  # compares list against first index of last loop
        collision = enemies[i1].rect.x.colliderect(enemies[i2].rect.x)
        if i2 != i1:
            if collision:
                if enemies[i2].rect.x > enemies[i1].rect.x:
                    enemies[i2].rect.x += 6
                if enemies[i2].rect.x < enemies[i1].rect.x:
                    enemies[i2].rect.x -= 6
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

You don't test the rect.x, you test the rect!

enemies[i1].rect.x.colliderect(enemies[i2].rect.x)

should be

enemies[i1].rect.colliderect(enemies[i2].rect)

You could also write:

for enemy1 in enemies:
    for enemy2 in enemies
        if enemy1 != enemy2:
            if enemy1.rect.colliderect(enemy2.rect)
marienbad
  • 1,461
  • 1
  • 9
  • 19