I have a working Alien Invasion game which is from the Python Crash Course book. However, I am applying modifications. I am trying to replace the value of bg_color
to represent a background image stored in my project folder instead of a colour i.e
self.bg_color = pygame.image.load('images/space.bmp')
Currently the AlienInvasion class imports the background colour from a class called settings stored in a sep file called settings.py
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's static settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
AlienInvasion is the main game class which sets up the game.
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
"""create a settings object using the imported settings class"""
self.settings = Settings()
"""initialise the game display using the settings saved within the settings file"""
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
""" assign the imported ship attributes to a new ship instance - "Self" in the call references to a call of AlienInvasion as a class """
self.ship = Ship(self)
""" run the game in full screen mode """
pygame.display.set_caption("Alien Invasion")
# Set the background color.
self.bg_color = (230, 230, 230)
Update_screen updates the screen continually as the game progresses and handles the background colour when the game is run
def _update_screen(self):
"""Update the images on the screen and flip to the new screen"""
# Redraw the screen during each pass through the loop. Use the settings file here
self.screen.fill(self.settings.bg_color)
I think my issue is to do with .fill() applying only to colour values (230, 230, 230). But I essentially want the screen to update with the background image rather than a static colour. Can somebody please assist? Is there another function I should be using instead of .fill()?
Change the background in Settings
:
class Settings:
def __init__(self):
# [...]
self.bg_color = pygame.image.load('images/space.bmp')
Get the background from the Settings
object in the constructor of AlienInvasion
. If the background image is a different size than the display, I recommend using pygame.transform.smoothscale()
to scale the background to the correct size:
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
# [...]
self.settings = Settings()
# [...]
self.bg_color = pygame.transform.smoothscale(self.settings.bg_color,
self.screen.get_size())
blit
the background rather rather than fill
the display surface:
class AlienInvasion:
# [...]
def _update_screen(self):
self.screen.blit(self.settings.bg_color, (0, 0))
blit
draw one image onto another. In this case it draw the background image (Surface) on the Surface which is associated to the display.
Note, that the name bg_color
is misleading. If you are using a background image, you should rename it (e.g. bg_image
or bg_surface
).