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.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…