简体   繁体   中英

Why won't colorkey or setalpha work on my code in pygame?

I am creating a game in pygame and I am trying to get the images to have their white backgrounds transparent, I did my research and tried set_colorkey and convert_alpha. Could someone tell me why this isn't working.

class player(pygame.sprite.Sprite):
def __init__(self,player):
    super().__init__()

    if player == 1:
        self.image = pygame.image.load('Megaman.png')
        self.image.set_colorkey(white)

    elif player == 2:
        self.image = pygame.image.load('Megaman2.png')
        self.image.convert_alpha()

The images are of Megaman with a white background. They are png images. The color white is

white = 255,255,255)

and this is the image of megaman: image of megaman

I would suggest trying to make a .png file of your pictures without a white background, as this would allow you to use convert_alpha() . I have found more success with that then setcolorkey

If you wish to remove the white backround of Megaman2.png , I suggest you don't use convert_alpha , as it does not remove color.

EDIT: Did you write your code as white = 255,255,255) ? That would cause a problem as you forgot a parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM