如何填充白色形状

How to fill shape white

描述

你好,我正在尝试让物体受到伤害,我想添加一个很酷的效果。

问题

当我键入此命令时:player.img.fill((255, 255, 255))

我明白了:

我想得到这个:

谢谢!

首先,您必须使用 pygame.mask.from_surface:

从图像中获取遮罩
player_mask = pygame.mask.from_surface(player.img)

然后您可以使用 to_surface 从蒙版创建图像。使用 set_colorkey():

使图像背景透明
mask_image = player_mask.to_surface(setcolor = (255, 255, 255))
mask_image.set_colorkey((0, 0, 0))

最小示例:

import pygame

pygame.init()
window = pygame.display.set_mode((200, 100))
clock = pygame.time.Clock()

player_image = pygame.image.load('Bird.png')
player_mask = pygame.mask.from_surface(player_image)
mask_image = player_mask.to_surface(setcolor = (255, 255, 255))
mask_image.set_colorkey((0, 0, 0))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    window.fill((0, 32, 64))
    window.blit(player_image, player_image.get_rect(center = (50, 50)))
    window.blit(mask_image, mask_image.get_rect(center = (150, 50)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()