Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use pygame set_alpha() on a picture

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
like image 523
Ben Schwabe Avatar asked Sep 04 '12 00:09

Ben Schwabe


2 Answers

[1] You don't want to load the image every iteration. Because creating a new surface is a slow operation. [2] Your loop draws 225 times, then afterward the final iteration, waits 2000ms.

You want:

image = pygame.image.load("logo.png")

for i in range (225):
    background.fill((0,0,0))    
    image.set_alpha(i)
    screen.blit(image,(0,0))
    pygame.display.flip()
    pygame.time.delay(20)

To fade in and out, you need to keep looping until the player clicks/hits a button. Like this:

import pygame
from pygame.locals import *

# ...

def title_loop():
    # title screen main loop    
    image = pygame.image.load("logo.png")
    done = False
    alpha = 0
    alpha_vel = 1

    # fade alpha in-out while waiting    
    while not done:        
        # get key input
        for event in pygame.event.get():
            if event.type == QUIT:
                done = true
            if event.type == KEYDOWN:
                if event.key = K_ESCAPE:
                    done = true

        # draw
        if alpha >= 255 or alpha <= 0:
            alpha_vel *= -1
        alpha += alpha_vel

        background.fill((0,0,0))
        image.set_alpha(i)
        screen.blit(image,(0,0))
        pygame.display.flip()

        pygame.time.delay(20)
like image 23
ninMonkey Avatar answered Sep 20 '22 12:09

ninMonkey


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.

image = pygame.image.load("logo.png")
image = image.convert()

or

image = pygame.image.load("logo.png").convert()

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.

import pygame, sys
pygame.init()
window=pygame.display.set_mode((1500, 800))
background=pygame.Surface((window.get_rect().width, window.get_rect().height))
background.fill((0, 0, 0))
image=pygame.image.load('InsertImageHere.png')
image=image.convert()
image2=pygame.image.load('InsertImage2Here.png')
image2=image2.convert_alpha()
rect=image.get_rect()
rect2=image2.get_rect()
rect2.left=rect.width+1
i=1
while True:
    for event in pygame.event.get():
        if event.type==12:
            pygame.quit()
            sys.exit()
    image.set_alpha(i)
    image2.set_alpha(i)
    window.fill((255, 255, 255))
    window.blit(background, background.get_rect())
    window.blit(image, rect)
    window.blit(image2, rect2)
    pygame.time.delay(20)
    i+=1
    if i==255:
        i=1
    pygame.display.update()

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.

like image 59
ThisIsAQuestion Avatar answered Sep 18 '22 12:09

ThisIsAQuestion