1

A helicopter is shot at. If he is hit, he falls. At the same time, a parachute releases from the helicopter. If the parachute is shot at and also hit by a bullet, the image of the parachute should be replaced with another image.
The jump with the parachute is done by the function absprung. The parachute is created with the Fallschirm class. The shelling is queried via collide.
It doesn't work, the picture is not changed.

def absprung(self):    #Auslöser Fallschirm
    fallschirm = Fallschirm(self.rect.centerx, self.rect.top)
    alle_sprites.add(fallschirm)
    fallschirme.add(fallschirm)

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
        self.beschuss = False

    def update(self):      
        if self.beschuss == True:
            self.image = pygame.image.load("Bilder/fadenkreuz.png").convert_alpha()
            self.image = pygame.transform.scale(self.image,(100,150))      
        self.rect.y  +=2
        if self.rect.y > hoehe:
            self.kill()  


hits = pygame.sprite.groupcollide(fallschirme,bullets,False,True)      
for hit in hits:        
     fallschirme.beschuss = True 
3
  • Are you sure that you call pygame.display.update() or pygame.display.flip()` every frame? Commented Nov 26, 2022 at 10:08
  • yes, when the helicopter ist damaged by a bullet Commented Nov 26, 2022 at 13:41
  • but fallschirme.beschuss = True that will not accept in the updat function . when i insert there print (self.beschuss) i get False as result. Commented Nov 26, 2022 at 13:58

1 Answer 1

2

beschuss is an attribute of a single Falschirm object, but not an attribute of fallschirme. pygame.sprite.groupcollide returns a dictionary of objects that were hit. You must set the attribute to these objects.

hits = pygame.sprite.groupcollide(fallschirme, bullets,False,True)      
for fallschirm in hits:    
    fallschirm.beschuss = True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.