I am using pygame and python for a project I am building, and I am building a splashscreen for when the game first opens. I have a .png that I want to show for the splashscreen, and decided to fade it in and out from black. the best way I found to do this was by blitting the image with a set alpha. I made this code, but it runs really slowly (the program hangs for 30 seconds) and doesn’t give an alpha. Only displays the picture onscreen. What am i doing wrong?
screen = pygame.display.set_mode([1066,600])
#Drawable surface
background = pygame.Surface(screen.get_size())
#Used for converting color maps
background = background.convert()
#Splashscreen
#image fades in
for i in range (225):
background.fill((0,0,0))
image = pygame.image.load("logo.png")
image.set_alpha(i)
logoimage = screen.blit(image,(0,0))
pygame.display.flip()
pygame.time.delay(2000)
#image fades out
#goes on to display main menu
Another problem that you might be having (besides what monkey said) is that you might need to use
surface.convert()which converts the image into a form where the alpha can be changed. You can do either of the following.or
I have found that, although
surface.convert_alpha()should do pretty much the same thing, it doesn’t usually work. Try this test code to check.In my testings, image 1 faded in properly, but image 2 stayed dark the whole time. You should try it for yourself; your computer might work differently.
If
surface.convert_alpha()does work for you, you should use it, otherwise, do what I said before. This should solve your problem.You should also note that I used
pygame.time.delay(20)rather than 2000 like you had before. 2000 would be a bit too long if you are increasing the alpha in incraments of one.