I want to have alternate characters in space invaders game using pygame. I want the game to switch ship to ship 1 if I press 1 and switch to ship 2 if I press 2 and so forth.
I have tried altering self.image in my ship.py file such that:
for event in pygame.event.get():
if event.type == pygame.K_1:
self.image = pygame.image.load('images/ship1.bmp')
self.image.get_rect()
elif event.type == pygame.K_2:
self.image == pygame.image.load('images/ship2.bmp')
self.image.get_rect()
but this returns a few errors in my main alien invasion program in regard to the ship class.
below is my working code for the static image that loads which is also the code I attempted to alter with the if and elif statements. Any advice would be greatly appreciated. If it helps I am a very nooby coder.
class Ship(Sprite):
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
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()