I am currently producing software on Pygame.
I would like to ask your opinion on image transmission processing.
I would like to fade out the image with the window created in Pygame.
Is there any good way?
I look forward to your kind cooperation.
(You can use a well-known library like Pillow.)
Fading out with set_alpha is easy.
By the way, fade-out such as text is specifying BLEND_RGBA_MULT in special_flags, so I put it in the sample code as well.
import pygame aspg
# Changing the Todo Pathname
img_path=r "img\test.png"
defmain(img_path):
clock = pg.time.clock()
screen=pg.display.set_mode(640,480))
# image initialization
img_orig=pg.image.load(img_path).convert()
colorkey=img_orig.get_at(0,0))#Treatment to make upper left transparent
img_orig.set_colorkey(colorkey,pg.RLEACCEL)#Treatment to make upper left transparent
img_surf=img_orig.copy()
img_alpha=pg.Surface(img_surf.get_size(), pg.SRCALPHA)
# text initialization
font=pg.font.SysFont(None,64)
blue=pg.Color('royalblue')
txt_orig = font.render(u'Transparent.', True, blue)
txt_surf = txt_orig.copy()
txt_alpha=pg.Surface(txt_surf.get_size(), pg.SRCALPHA)
alpha = 0
isAdding = True
while True:
for event inputg.event.get():
if event.type==pg.QUIT:
return
screen.fill ((30, 30, 30))
# image drawing
img_alpha.fill(255,255,255,alpha),special_flags=pg.BLEND_RGBA_MULT)
img_surf=img_orig.copy()
img_surf.set_alpha(alpha)#transparent treatment
img_surf.blit(img_alpha,(0,0))
screen.blit(img_surf,(10,50))
# text drawing
txt_alpha.fill(255,255,255,255-alpha))
txt_surf = txt_orig.copy()
txt_surf.blit(txt_alpha,(0,0), special_flags=pg.BLEND_RGBA_MULT)
screen.blit(txt_surf,(10,10))
pg.display.flip()
clock.tick(30)
# transparency update
if isAdding:
alpha + = 5
else:
alpha - = 5
if alpha<=0 or alpha>=255:
isAdding = not isAdding
alpha = max (min(alpha,255), 0)
if__name__=='__main__':
pg.init()
main(img_path)
pg.quit()
© 2024 OneMinuteCode. All rights reserved.