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?
The behaviour which you're getting is due to the way
time.sleep()
works.Example:
I want you to try the following code in your console:
Did you observe what happened during the output?
This is what is happening with your code too. First it sleeps, then it updates
self.content
toself.image
andTime.cover
at the same time.Fix:
To fix the code in the example above, you can use
sys.stdout.flush()
.Disclaimer:
I haven't tried
sys.stdout.flush()
with Pygame so I can't say if it will work for you, but you can try.There seems to be a working solution on this SO question: How to wait some time in pygame?