Time delay in python/pygame without disrupting the game? -


I was writing the code for the introduction of a game that I was making, introducing here one of the images with a timeline The problem is delaying the chain in between 4 seconds. The problem is that using the time.sleep method also gets worse with the main loop and thus the program is "hung" for this period. Any suggestions please? [Have identification and TWD sound items]

  a = 0 for events in true: pygame.event.get (): if event.type == QUIT: pygame.quit () sys . Exit () Intro.stop () TWD.stop () if a & lt; = 3: screen.blit (pygame.image.load (images [a]). Convert (), (0,0)) a = a + 1 if a & gt; 1: time.sleep (4) Intro.play () if any == 4: Intro.stop () TWD.play () pygame.display.update ()  

You can add some arguments that advance only a if 4 seconds have passed, you can you can use the module and a starting point last_time_ms can get, every time we loop, we are searching for new current time and the time and last_time_ms Find the differences between them. If it is more than 4000 ms, increment a .

I used milliseconds because I think it is more convenient than seconds.

  import time = 0 last_time = int (round (time.time () * 1000)) is true: diff_time_ms = int (round (time.time () * 1000)) - last_time_ms (Diff_time_ms> = 4000): a + = 1 last_time_ms = int (round (time.time () * 1000)) for events occurring in pygame.event.get (): if event.type == QUIT: Pygame.quit () Sys.exit () Intro.stop () TWD.stop () if & lt; = 3: screen.blit (pygame.image.load (picture [a]). Convert (), (0,0)) Intro.play () if a == 4: Intro.stop () TWD.play () Pygame.display.update ()  

Comments