0

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

jjjosete
  • 13
  • 4

2 Answers2

0

I would add print statements above your if test, so see what the value of espera is.

The solution is to make it: (anticolision_time - current_time )/ 1000

to make it work, because the anticolision_time is going to be larger :) Best of luck!

>>> import time
>>> current = time.time()
>>>
>>>
>>>
>>> anti_time = time.time()
>>>
>>> current - anti_time
-5.687999963760376

as you can see, your variable current_time is a bit misleading, you need to rename it to start_time to make it clearer

  • I still have the same problem, works perfectly fine while debugging, but it doesn't works while running. I made the print(espera) above the If statement and although while debugging works fine while running only prints "0.0" – jjjosete Jul 24 '21 at 14:07
0

In your implementation current_time is always equal anticolision_time.

The return value of pygame.time.get_ticks() only changes when you delay for some time with pygame.time.delay. On some systems you'll also need to handle the events (see pygame.event.pump).

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I cant't use time.delay because it freezes the whole game for that second. I'll check the event.pump doc ! – jjjosete Jul 24 '21 at 14:20
  • @jjjosete You shouldn't do that at all. Use the application loop! YOu have working examples here [Animation doubles duration every time it appears in pygame](https://stackoverflow.com/questions/68508824/animation-doubles-duration-every-time-it-appears-in-pygame) and here [How can I show explosion image when collision happens?](https://stackoverflow.com/questions/64305426/how-can-i-show-explosion-image-when-collision-happensv). Why do you step back to the first ill desinge? – Rabbid76 Jul 24 '21 at 14:25
  • The code on the animation one didn't work either, I'm trying to fix it but I'm struggling!, Thanks for the help, anyway!! – jjjosete Jul 24 '21 at 15:11
  • @jjjosete But you can find a nice example here: [How can I show explosion image when collision happens?](https://stackoverflow.com/questions/64305426/how-can-i-show-explosion-image-when-collision-happens) – Rabbid76 Jul 24 '21 at 15:14