Search code examples
pythonpygamespritetransparencypygame-surface

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


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 like this

ship.py

import pygame

class Ship:
    """ A class to manage the ship. """
    
def __init__(self, ai_game):
    """ Initialize the ship and the starting position. """
    self.screen = ai_game.screen
    self.screen_rect = ai_game.screen.get_rect()

    # Load the ship image and get its rect.
    self.image = pygame.image.load('images/ship.bmp')
    self.rect = self.image.get_rect()

    # Start each new ship at the bottom center of the screen.
    self.rect.midbottom = self.screen_rect.midbottom

def blitme(self):
    """ Draw ship at its current location. """
    self.screen.blit(self.image, self.rect)

alieninvasion.py

import sys
import pygame
from ship import Ship


class AlienInvasion:
"""Overall class to manage game assets and behavior."""

def __init__(self):
    """Initialize the game, and create game resources."""
    pygame.init()
    self.screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")

    # Set background colour
    self.bg_color = (0, 0, 255)
    self.ship = Ship(self)
    
def run_game(self):
"""Start the main loop for the game."""
while True:
    # Watch for keyboard and mouse events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
    # Redraw the screen during each pass through the loop.
    self.screen.fill(self.bg_color)
    self.ship.blitme()

    # Make the most recently drawn screen visible.
    pygame.display.flip()


if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()

I tried the answers from this discussion but I couldn't fix it.

I couldn't understand how to use image.convert_alpha() and image.set_colorkey() and using them in ship.py did not show any changes for me.

Note: ship.py is the class to make changes on ship while alieninvasion.py is the main file.


Solution

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