Search code examples
pythonpygamecollision-detection

How to know which sprite has collided with a bullet in its group?


Im making a game where a player shoots bullets at incoming boulders, which destroys them. It all functions well and good, but it seems a bit plain when the boulder's destroyed. I want to display an explosion image where the boulder was destroyed. To do this, I need to know which exact boulder has been destroyed in my boulder group.

Relevant code

pygame.sprite.groupcollide(boulder_grp, bullet_grp, True, True)

The boulder_grp contains the boulders that spawns, and the bullet_grp contains the bullet that the player shoots. I want to know which boulder in the grp was destroyed, so I can blit an image at that exact spot. How do I go about doing this? Thank you.


Solution

  • pygame.sprite.groupcollide returns a dictionary with the sprites.

    collide_dict = pygame.sprite.groupcollide(boulder_grp, bullet_grp, True, True)
    for boulder in collide_dict:
        print(boulder.rect)