How to use pygame set_alpha () in the picture

I use pygame and python for the project I am creating, and I am creating a screen saver when the first game opens. I have a .png that I want to show for the screensaver, and decided to put it out and get out of black. the best way I found for this was: blitting an image using the alpha set. I made this code, but it works very slowly (the program freezes for 30 seconds) and does not give alpha. Displays only the image on the screen. 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 
+1
source share
3 answers

Another problem you might run into (besides what the monkey said) is that you may need to use surface.convert() , which converts the image into a form in which alpha can be resized. You can do one of the following:

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

or

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

I found that although surface.convert_alpha() should do almost the same thing, it usually doesn't work. Try checking out this test code.

 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 tests, image 1 disappeared properly, but image 2 remained dark all the time. You must try it for yourself; Your computer may work differently.

If surface.convert_alpha() works for you, you should use it, otherwise do what I said earlier. This should solve your problem.

It should also be noted that I used pygame.time.delay(20) , and not 2000, as before. 2000 will be too long if you increase alpha in the ranks of one.

+5
source

[1] You do not want to upload an image at each iteration. Because creating a new surface is slow work. [2] Your loop draws 225 times, then after the last iteration it waits 2000 ms.

Do you want to:

 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 continue the cycle until the player presses the button / presses the 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) 
+4
source

PygameNerd your example is close, but actually it does not work.

Image.convert () will fade correctly, but it does not support alpha channel. Try on a non-black background and it shows. The .convert_alpha () image will not fade, but the alpha channel is working fine.

I am surprised that pygame does not support this out of the box, but anyway. Here is the answer: http://www.nerdparadise.com/tech/python/pygame/blitopacity/

A bit complicated, but works great. Transparent background and attenuation in just one package.

+2
source

Source: https://habr.com/ru/post/927666/


All Articles