Search code examples
pythontimepygame

How to use time.sleep in pygame?


So i am trying to display an image, wait 1 second and then display another image. I'm making a matching game, so in my code I'm trying to say that if two images don't match, I want to change the image to just a preset generic one. So my code for that would look something like this:

if True:
    self.content = self.image
    time.sleep(1)
    self.content = Tile.cover

self.content is a variable for what is being displayed, self.image is the image that displays, and then Tile.cover is the generic image that covers the other one. Yet whenever I do this the code skips the first line, and just sets the image to Tile.cover, why?


Solution

  • In Pygame, you have to use pygame.time.wait() instead of python's time.sleep().

    It takes time in milliseconds:

    pygame.time.wait(1000)
    

    The reason for not using time.sleep is because it will block pygame's event loop and so pygame will not be able to process other events.