Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

python - Pygame - sprite collision with sprite group

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...