0

I'm fairly new to coding and currently I am making a game with pygame. The game is a ship that has to dodge asteroids.I am trying to avoid having multiple collisions at the same time, there is also an explosion animation and an explosion sound. I wanted to use a boolean as an anticolision device, what I mean is anticolision will be always false except if there is a colision, then it should be True a not allow for any collisions to happen for 1 second. I think the logic is correct but I can't seem to make it work, mainly because I have some trouble understanding how to manage events with time on PyGame. I also wanted to say sorry, because I know there are a lot of good answers and a lot of questions around this topic but I can't trully understand the time factor. Thanks in advance!

            if self.player.estado == self.player.Estado.volando:
                anticolision = False
                colisiones = pygame.sprite.spritecollide(self.player, self.asteroides, True, pygame.sprite.collide_circle)
                for colision in colisiones:
                    start_ticks = pygame.time.get_ticks()    
                                     
                    if colision and not anticolision:
                        anticolision = True                       
                        expl = Explosion(colision.rect.center)
                        self.explosionSound.play()
                        self.all_sprites.add(expl)                  
                        self.vidas -= 1
                        segundos = (pygame.time.get_ticks()-start_ticks)/1000
                        if segundos > 0.5:
                            anticolision = False                        
                    else:
                        pass

                    if self.vidas == 0:     
                        self.gameOver()
                        running= False
jjjosete
  • 13
  • 4
  • Why do you set `start_ticks = pygame.time.get_ticks()` in the loop? – Rabbid76 Jul 23 '21 at 18:14
  • I did it to try to mark the begging of the first collision, I've also tried setting `start_ticks` outside the for loop but didn't work either. Where do you think I should set it? – jjjosete Jul 23 '21 at 19:02

0 Answers0