0

I'm following the Alien invasion project on Python Crash Course by Eric Matthews and I have gotten to the point of loading the spaceship onto pygame, but when I do it crashes.

Here is the code that will run pygame. I've added "HERE" to the code that i recently entered to make the application crash. My suspicion I think is on the my Ship class that I created and is not loading the image properly since the input of the file location is wrong. I'll add this code below

import sys

import pygame

from settings import Settings
from ship import Ship

class AleinInvasion:
    """Overallclass to maange game assets and behavior."""

    def __init__(self):
        """Intialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width,self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self) [HERE]

        #Set background color
        self.bg_color = self.settings.bg_color

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            #watch for keyboard and moust 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.settings.bg_color)
            self.ship.blitme()[HERE]

            #make the most recenlty drawn screen visible
            pygame.display.flip()

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

Here is the code for the ship class:

import pygame

class Ship:
    """A class to manage the ship"""

    def __init__(self,ai_game):
        """ Intialize the ship and set its starting postiion."""
        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 of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

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

Thanks!

Isaiah.B
  • 21
  • 3
  • 1
    Looks like a typo in the `Ship.blitme()` function you refer to `slef.image` instead of `self.image`. If that's not it, please edit your question to include the full exception trace. – import random Jun 13 '22 at 23:52
  • 1
    @importrandom thanks for the help. This solved the issue. Can't believe I missed such a simple thing sorry about that. Recently started coding. This is my first project. – Isaiah.B Jun 14 '22 at 00:01
  • 1
    You're welcome, some editors or [code analysis tools](https://pypi.org/project/pylint/) can help find these errors. Good luck. – import random Jun 14 '22 at 00:19

0 Answers0