0

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()
rfkortekaas
  • 6,049
  • 2
  • 27
  • 34
Flann3l
  • 69
  • 5
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. "this returns a few errors" is not a problem specification. Asking us to visually diagnose code out of context is usually fruitless for those of us who would help, and it's useless to the archive -- which, after all, is the site's charter purpose. – Prune Jan 04 '21 at 16:42

2 Answers2

0

The instruction self.image.get_rect() does nothing at all. You must set the attribute rect. I recommend centering the new image at the center of the current image:

self.rect = self.image.get_rect(center = self.rect.center)

pygame.event.get() get all the messages and remove them from the queuepygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

See als Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?

Get the events once and use them in multiple loops or pass the list or events to functions and methods where they are handled:

class Ship(Sprite):
    def __init__(self, ai_game):
        # [...]
       
        self.ship_image_1 = pygame.image.load('images/ship1.bmp')
        self.ship_image_2 = pygame.image.load('images/ship2.bmp')

    # [...]


    def my_method(self, event_list):

        for event in event_list:
            if event.type == pygame.K_1:
                self.image = self.ship_image_1
                self.rect = self.image.get_rect(center = self.rect.center)
            elif event.type == pygame.K_2:
                self.image = self.ship_image_2
                self.rect = self.image.get_rect(center = self.rect.center)
while run:

    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False

    my_sprite.my_method(event_list)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

First load the images not in the loop.

image1 = pygame.image.load('image1')
image2 = pygame.image.load('image2')
 #replace image 1 with your image
 #in the event loop
 screen.blit(image1, (Your wish dimensions))
 #note that I took screen.blit because I made my screen in a variable called screen.
 #If you variable is display take display.blit
 if event.type == pygame.KEYDOWN:
    if pygame.key == pygame.K_RIGHT:
       image1 = image 2
Dharman
  • 30,962
  • 25
  • 85
  • 135
Revanth
  • 26
  • 1