I am currently trying to display a death screen when the player dies in my game, but rather than it just popping up I want it to gradually becoming less and less transparent until the opacity is 256. I currently have this line of code below to display the image, but I was wondering how I would go about gradually increasing the opacity over a period of few seconds.
screen.blit(pygame.image.load("Images/dead.jpg"), (0, 0))
I've tried to add a for loop and tried to use convert_alpha, however I couldn't figure out a way of doing it, and any help will be greatly appreciated.
Here is what I would do. I have explained everything in the comments:
# Allow the image to have its alpha value changed
image = pygame.image.load("Images/dead.jpg").convert()
# Set the transparency to full
image_alpha = 0
# Decide how many frames you want the image to fade in over
fade_frame_number = 60
# And the frames per second
FPS = 30
FPS_Clock = pygame.time.Clock()
while True:
window.fill((255, 255, 255)) # Fill the window with a white bg
if image_alpha < 255:
image_alpha += 255 / fade_frame_number # Change the transparency variable
image.set_alpha(image_alpha) # Set the image's alpha value
window.blit(image, (0, 0)) # Display the image
pygame.display.update() # Update the screen
FPS_Clock.tick(FPS) # Wait for the next frame
In this, window
is the display surface.