I'm making a litle game with pygame where a ship doges asteroids and 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, after 1 second anticolision will return back to False. The idea is to avoid the explosion animation and the life loss while there is already an animation going on. I think the logic is correct but I can't seem to make it work. I always get the first print, but not the second. The weird thing is that it works correctly in vsc debugger, but that's the only way I can reach that second print and the change of the value back to normal. Why?
current_time = 0
while running :
#[...]
current_time = pygame.time.get_ticks()
#[...]
if self.player.estado == self.player.Estado.volando:
#comprobar colisión
colisiones = pygame.sprite.spritecollide(self.player, self.asteroides, True, pygame.sprite.collide_circle)
anticolision = False
for colision in colisiones:
if colision and not anticolision:
anticolision_time = pygame.time.get_ticks()
anticolision = True
print("anticolision true")
expl = Explosion(colision.rect.center)
self.explosionSound.play()
self.all_sprites.add(expl)
self.vidas -= 1
espera = (anticolision_time - current_time)/ 1000
if espera > 1:
print("anticolision false")
anticolision = False
else:
pass
if self.vidas == 0:
self.gameOver()
running= False