3
votes

I have created two simple sprites in PyGame and one of them is an Umbrella, the other one is a rain drop. The Raindrops are added into a sprite group called all_sprites. The Umbrella sprite has its own group called Umbrella_sprite

The raindrops are "falling" from top of the screen and if one of them touches the umbrella / collides with it.. the raindrop is supposed to be deleted. BUT instead of that specific raindrops all other are affected by this.

main file (rain.py)

#!/usr/bin/python
VERSION = "0.1"
import os, sys, raindrop
from os import path

try:
    import pygame
    from pygame.locals import *
except ImportError, err:
    print 'Could not load module %s' % (err)
    sys.exit(2)

# main variables
WIDTH, HEIGHT, FPS = 300, 300, 30

# initialize game
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Rain and Rain")

# background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((40,44,52))

# blitting
screen.blit(background,(0,0))
pygame.display.flip()

# clock for FPS settings
clock = pygame.time.Clock()


def main():
    all_sprites = pygame.sprite.Group()
    umbrella_sprite = pygame.sprite.Group()
    # a function to create new drops
    def newDrop():
        nd = raindrop.Raindrop()
        all_sprites.add(nd)

    # creating 10 rain drops
    for x in range(0,9): newDrop()

    # variable for main loop
    running = True

    # init umbrella
    umb = raindrop.Umbrella()
#    all_sprites.add(umb)
    umbrella_sprite.add(umb)

    # event loop
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        for enemy in all_sprites:
            gets_hit = pygame.sprite.spritecollideany(umb, all_sprites)
            if gets_hit:
                all_sprites.remove(enemy)

        screen.blit(background,(100,100))

        # clear
        all_sprites.clear(screen,background)
        umbrella_sprite.clear(screen,background)

        # update
        all_sprites.update()
        umbrella_sprite.update()

        # draw
        all_sprites.draw(screen)
        umbrella_sprite.draw(screen)

        # flip the table
        pygame.display.flip()
    pygame.quit()

if __name__ == '__main__':
    main()

raindrop.py ( Raindrop() & Umbrella() )

import pygame
from pygame.locals import *
from os import path
from random import randint
from rain import HEIGHT, WIDTH

img_dir = path.join(path.dirname(__file__), 'img')

class Raindrop(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.width = randint(32, 64)
        self.height = self.width + 33
        self.image = pygame.image.load(path.join(img_dir, "raindrop.png")).convert_alpha()
        self.image = pygame.transform.scale(self.image, (self.width, self.height))
        self.speedy = randint(1, 15)
        self.rect = self.image.get_rect()
        self.rect.x = randint(0, 290)
        self.rect.y = -self.height

    def reset(self):
        self.rect.y = -self.height

    def update(self):
        self.rect.y += self.speedy
        if self.rect.y >= HEIGHT:
            self.rect.y = -self.height
            self.rect.x = randint(0, 290)

class Umbrella(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.width = 50
        self.height = 50
        self.image = pygame.image.load(path.join(img_dir,"umbrella.png")).convert_alpha()
        self.image = pygame.transform.scale(self.image, (self.width, self.height))
        self.speedx = 10
        self.rect = self.image.get_rect()
        self.rect.x = (WIDTH/2) - self.width
        self.rect.y = (0.7 * HEIGHT)

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.rect.x > 0:
            self.rect.x -= self.speedx
        elif keys[pygame.K_RIGHT] and self.rect.x < (WIDTH - self.width):
            self.rect.x += self.speedx
2
Don't post your code in the question. Create a minimal reproducible example instead, which will make it more beneficial for other people with the same problem as it makes it more readable, easier to understand and more general (instead of it being presented as your problem it's being presented as a problem). - Ted Klein Bergman
I think it actually is a pretty minimal and complete example. (Py)game examples tend to be relatively big. The title should be changed to something like "spritecollideany in a for loop removes too many sprites", and the bugged for loop could be posted separately. - skrx

2 Answers

4
votes

This is your problem:

for enemy in all_sprites:
    gets_hit = pygame.sprite.spritecollideany(umb, all_sprites)
    if gets_hit:
        all_sprites.remove(enemy)

You're looping through the group, and if any sprite collides, deleting all of them.

You don't need to loop through the group - the collision functions take care of that. You just need to use the spritecollide function, which compares a sprite versus a group. That function will return a list of collisions, as well as using the DOKILL flag to delete them automatically:

        gets_hit = pygame.sprite.spritecollide(umb, all_sprites, True)
2
votes

spritecollideany checks if the sprite collides with any sprite in the group and returns this sprite, so gets_hit is a trueish value as long as the collided sprite in the group is not removed and the if gets_hit: block gets executed. That means the code in the for loop simply keeps deleting every sprite in the group that appears before the collided sprite is reached and removed. A simple fix would be to check if the hit sprite is the enemy: if enemy == gets_hit:, but the code would still be inefficient, because spritecollideany has to loop over the all_sprites group again and again inside of the for loop.

I recommend to use spritecollide instead of spritecollideany as well, since it's more efficient and just one line of code.