Pydude
Pydude

Reputation: 145

How to fill shape white

Description

Hi, I am trying to make objects to take damage and I want to add a cool effect.

Problem

When I type this command: player.img.fill((255, 255, 255))

I get this:

enter image description here

and I want to get this:

enter image description here

Thanks!

Upvotes: 1

Views: 223

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

First you have to get the mask from the image with pygame.mask.from_surface:

player_mask = pygame.mask.from_surface(player.img)

Then you can use to_surface to create an image from the mask. Make the background of the image transparent with set_colorkey():

mask_image = player_mask.to_surface(setcolor = (255, 255, 255))
mask_image.set_colorkey((0, 0, 0))

Minimal example:

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()

Upvotes: 2

Related Questions