I'm trying to build a game in Pygame where if a player moves onto a red square, the player loses. When this happens, I want to display a picture of an explosion where the player lost until the user presses any key on the keyboard. When the user presses a key, I'll call the function new_game() to start a new game. The issue is that my code seems to skip over the line where I blit the explosion, and instead just starts a new game right away.
I've tried using something like this, but I'm not sure what to put in the while loop (I want it to wait until there is a keypress):
while event != KEYDOWN:
# Not sure what to put here
If I put time.sleep() in the while loop, the whole program seems to freeze and no image is blitted.
Here's me loading the image into Pygame:
explosionpic = pygame.image.load('C:/Users/rohan/Desktop/explosion.png')
And here's where I call it/determine if a player has lost (program seems to skip over the screen.blit line because I don't even see the image at all):
if get_color(p1.x + p1_velocity_x, p1.y + p1_velocity_y) == red: # If player lands on a red box
screen.blit(explosionpic, (p1.x, p1.y))
# Bunch of other code goes here, like changing the score, etc.
new_game()
It's supposed to display the image, then when the user presses a key, call the new_game() function.
I'd appreciate any help.
pygame.display.update()and some way to delay the call tonew_game()until the key is pressed. Do you have it? If so, please add it to the question - Valentino