pygame:平滑的图片分配 [英] Pygame : smooth picture aparition

查看:199
本文介绍了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:

pygame支持三种类型的透明度:色键,表面Alpha和像素Alpha.表面Alpha可以与色键混合,但是具有每像素Alpha的图像不能使用其他模式. Colorkey透明性使单个颜色值透明.与色键匹配的任何像素都不会绘制.表面Alpha值是一个可更改整个图像透明度的单个值. 255的表面alpha是不透明的,0的值是完全透明的.

There are three types of transparency supported in pygame: colorkeys, surface alphas, and pixel alphas. Surface alphas can be mixed with colorkeys, but an image with per pixel alphas cannot use the other modes. Colorkey transparency makes a single color value transparent. Any pixels matching the colorkey will not be drawn. The surface alpha value is a single value that changes the transparency for the entire image. A surface alpha of 255 is opaque, and a value of 0 is completely transparent.

每个像素的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通过调用

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:平滑的图片分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆