Search code examples
pythonpygamecollision-detectionsprite

Pygame sprites: sprite.collide 'How to not "kill" the sprite?' (or suggest a collsion detection technique)


I just have a simple question which is 'how to not "kill" a sprite'.

It probably sounds odd but let me explain.

I am trying to create a boss battle using an ufo. I already have code I used before that kill the enemy on touch using sprite.spritecollide.

for bullet in bullet_list:
    bullet_hit_list  = pygame.sprite.spritecollide(bullet,enemy_list, True)
        for i  in bullet_hit_list:
            print("bullet Collision")

Although I don't want the ufo to disappear. I want the ufo to take more than one hit before it goes down.

Does anyone know how to stop spritecollide for killing the sprite or know another technique for collision detection I could use?

Thanks for your time! :)


Solution

  • The True argument to the spritecollide function means that all Sprites that collide will be removed from the enemy_list Sprite Group. Change that to false and it won't kill the sprite.

    Reference: http://pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide