本文介绍了pygame:平滑的图片分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗? :

我试图制作一个在用户屏幕上流畅显示的标题:

I try to make a title that appear smoothly on the user screen :

def image(name,x,y,u):
   screen.blit(name,(x,y))
   if u = 1
      pygame.display.update()

Window == 'main'
While windows = 'main':
   image(background,0,0)
   image(title, 640, 120)
   pygame.display.update()

但是发短信突然突然消失了,而不是我想要的...

But texte appaers suddently and not as I would like...

推荐答案

您要使用透明性,而pygame使用三种不同类型:

You want to use transparency, and pygame uses three different types:

每个像素的Alpha不同,因为它们存储每个像素的透明度值.这样可以实现最精确的透明效果,但速度也最慢.每个像素的Alpha不能与表面Alpha和色键混合.

Per pixel alphas are different because they store a transparency value for every pixel. This allows for the most precise transparency effects, but it also the slowest. Per pixel alphas cannot be mixed with surface alpha and colorkeys.

因此,让我们使用colorkey创建一个透明的Surface并为其添加一些文本,然后使用表面alpha通过调用 set_alpha Surface:

So let's use colorkey to create a transparent Surface and blit some text to it, and then use surface alpha to create the fade in effect by calling set_alpha and the Surface:

import pygame

def main():
    screen = pygame.display.set_mode((300, 100))
    FONT = pygame.font.SysFont(None, 64)
    text = FONT.render('Hello World', False, pygame.Color('darkorange'))
    surf = pygame.Surface(text.get_rect().size)
    surf.set_colorkey((1,1,1))
    surf.fill((1,1,1))
    surf.blit(text, (0, 0))
    clock = pygame.time.Clock()
    alpha = 0
    while True:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                return

        alpha = (alpha + 1) % 256
        surf.set_alpha(alpha)

        screen.fill(pygame.Color('dodgerblue'))
        screen.blit(surf, (20, 20))
        clock.tick(120)
        print(alpha)
        pygame.display.update()

if __name__ == '__main__':
    pygame.init()
    main()

这篇关于pygame:平滑的图片分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:03