1

If a collision occurs, is there a way to get both objects?

For example:

allSprite = pygame.sprite.Group()
Bullets = pygame.sprite.Group()
Enemies = pygame.sprite.Group()

bullet = Bullet()
enemy = Enemy()
Bullets.add(bullet)
Enemies.add(enemy)

hits = pygame.sprite.collide(Enemies,Bullets)
for hit in hits:
    hit.damage()

This only gets the Enemy object, but I want both objects because I need the bullet damage, too.

skrx
  • 19,980
  • 5
  • 34
  • 48
Knight
  • 23
  • 6

1 Answers1

1

You can use the groupcollide function for this: http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.groupcollide

You will get back a dictionary of Enemies (keys) and for each Enemy, a list of the Bullets that hit it (values). Then you can do something like this:

hits = pygame.sprite.groupcollide(Enemies, Bullets, False, True)
for enemy in hits:
    for bullet in hits[enemy]:
        enemy.damage(bullet.damage)
Chris
  • 898
  • 1
  • 5
  • 8