sprite.rect.colliderect AttributeError'

Question:

In hits = pygame.sprite.spritecollide(fallschirme,bullets,False) i get the message
sprite.rect.colliderect AttributErros and ‘Group’ object has no attribute ‘rect’. The relevant code is listed below. i looked similar question but i didn’t find something simular.

 class Fallschirm(pygame.sprite.Sprite):       
        def __init__(self,x,y):
            pygame.sprite.Sprite.__init__(self)   
            self.image = pygame.image.load("Bilder/fallschirm.png").convert_alpha()
            self.image = pygame.transform.scale(self.image,(100,150))
            self.rect = self.image.get_rect()
            self.rect.x = x
            self.rect.y = y
    
        def update(self):
            self.rect.y  +=2
            if self.rect.y > hoehe:
                self.kill()  
    
     def absprung(self):    #Auslöser Fallschirm
            fallschirm = Fallschirm(self.rect.centerx, self.rect.top)
            alle_sprites.add(fallschirm)
            fallschirme.add(fallschirm)
            sound=pygame.mixer.Sound("Audio/help.mp3")
            sound.play()  
    
     hits = pygame.sprite.spritecollide(fallschirme,bullets,False)
        for hit in hits:
fallschirme = pygame.sprite.Group() 
  
Asked By: Joachim

||

Answers:

In your code fallschirme is a sprite.Group(), but you’re passing it as the first argument to spritecollide(...), which expects a sprite.

I think you should be initialising fallschirme as:

fallschirme = Fallschirm(x_position, y_position)

If that is not what you want, you’ll need to edit your question to add more context.

Answered By: import random
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.