How to convert the background color of image to match the color of Pygame window?

前端 未结 1 462
予麋鹿
予麋鹿 2021-01-23 12:22

What I need to do is match the color of the image background with the color of Pygame windows. But the background of image and pygame window didn\'t match. It looks something li

1条回答
  •  伪装坚强ぢ
    2021-01-23 12:52

    You don't need to change the background color of the image to the background color of the window, but make the background of the image transparent.


    Set the transparent colorkey by pygame.Surface.set_colorkey:

    Set the current color key for the Surface. When blitting this Surface onto a destination, any pixels that have the same color as the colorkey will be transparent.

    Note, all the background has to have exactly the same color. In your case the background color seems to be gray (230, 230, 230):

    self.image = pygame.image.load('images/ship.bmp').convert()
    self.image.set_colorkey((230, 230, 230))
    

    Another option wound be to create a new image (you need to draw it in a painting application) with per pixel alpha (e.g. PNG) and a transparent background:

    self.image = pygame.image.load('images/ship.png').convert_alpha()
    

    0 讨论(0)
提交回复
热议问题