代码之家  ›  专栏  ›  技术社区  ›  Anish Shanbhag

Pygame中的透明图像

  •  2
  • Anish Shanbhag  · 技术社区  · 6 年前

    我在Pygame里有一个蓝色圆圈的精灵。我希望将此图像绘制到“褪色”屏幕上,例如半透明。但是,我不希望在它上面画一个半透明的矩形;相反,我希望修改实际图像并使其半透明。非常感谢您的帮助!

    现在我有:

    Class Circle(pygame.sprite.Sprite):
    
        self.image = self.image = pygame.image.load("circle.png")
    
    circle = Circle()
    

    最终…

    window.blit(pygame.transform.scale(circle.image, (zoom, zoom)), (100, 100))
    

    circle.png的外观:

    enter image description here

    我希望图像在透明后的外观:

    enter image description here

    我正在将图像点到窗口上,这是一个白色的背景。

    3 回复  |  直到 6 年前
        1
  •  3
  •   skrx    6 年前

    首先,您的图像/曲面需要使用每像素alpha,因此调用 convert_alpha() 方法。如果要创建新曲面(如示例中所示),也可以通过 pygame.SRCALPHA pygame.Surface

    第二步是创建另一个曲面(称为 alpha_surface 这里)填充白色和所需的alpha值(颜色元组的第四个元素)。

    最后,你必须 α-表面 在你的图像上传递 pygame.BLEND_RGBA_MULT 作为 special_flags 参数。那将使 图像的不透明部分是半透明的。

    import pygame as pg
    
    
    pg.init()
    screen = pg.display.set_mode((800, 600))
    clock = pg.time.Clock()
    BLUE = pg.Color('dodgerblue2')
    BLACK = pg.Color('black')
    
    # Load your image and use the convert_alpha method to use
    # per-pixel alpha.
    # IMAGE = pygame.image.load('circle.png').convert_alpha()
    # A surface with per-pixel alpha for demonstration purposes.
    IMAGE = pg.Surface((300, 300), pg.SRCALPHA)
    pg.draw.circle(IMAGE, BLACK, (150, 150), 150)
    pg.draw.circle(IMAGE, BLUE, (150, 150), 130)
    
    alpha_surface = pg.Surface(IMAGE.get_size(), pg.SRCALPHA)
    # Fill the surface with white and use the desired alpha value
    # here (the fourth element).
    alpha_surface.fill((255, 255, 255, 90))
    # Now blit the transparent surface onto your image and pass
    # BLEND_RGBA_MULT as the special_flags argument. 
    IMAGE.blit(alpha_surface, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
    
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
    
        screen.fill((50, 50, 50))
        pg.draw.rect(screen, (250, 120, 0), (100, 300, 200, 100))
        screen.blit(IMAGE, (150, 150))
    
        pg.display.flip()
        clock.tick(60)
    
    pg.quit()
    
        2
  •  0
  •   eyllanesc Yonghwan Shin    6 年前

    用每像素alpha创建一个新曲面。

    surf = pygame.Surface((circle_width, circle_height), pygame.SRCALPHA)
    

    使表面透明

    surf.set_alpha(128)  # alpha value
    

    把圆画到那个表面 (x=0, y=0)

    surf.blit(pygame.transform.scale(circle.image, (zoom, zoom)), (0, 0))
    

    将表面绘制到窗口

    window.blit(surf, (circle_x, circle_y))
    
        3
  •  0
  •   Gomes J. A.    6 年前

    作为 Surface.set_alpha() 文件上说,你可以用“齐次阿尔法”或者每像素阿尔法,但不能两者兼而有之,我相信这是你想要的。它可能与彩色键的透明度有关,但我不确定(我还没有测试过)。如果使用,任何非rgba(没有活动alpha通道)像素格式都可能工作。 set_colorkey() set_alpha() 在闪电之前。

    因此,代码可能看起来像:

    class Circle(pygame.sprite.Sprite):
        def __init__(self)
            self.image = pygame.image.load("circle.png")
            # get the surface's top-left pixel color and use it as colorkey
            colorkey = self.image.get_at((0, 0))
            self.image.set_colorkey(colorkey)
    

    在代码中的某个时刻(在呈现之前),您可能希望通过调用以下命令来设置透明度:

    circle.image.set_alpha(some_int_val)
    

    然后你就可以按计划缩放和闪烁它。