0

Screenshot of Error Text

Error Text:

Traceback (most recent call last):
  File "C:\Users\Sean Takei\Desktop\python_work\alien_invasion\alien_invasion3.py", line 40, in <module>
    ai= AlienInvasion()
  File "C:\Users\Sean Takei\Desktop\python_work\alien_invasion\alien_invasion3.py", line 21, in _init__
    self.ship= Ship(self)
  File "C:\Users\Sean Takei\Desktop\python_work\alien_invasion\shiptest.py", line 23, in _init__
    self.image = pygame.image.load('images/ship.bmp' )
pygame.error: Couldn't open images/ship.bmp

First post here, Im new to python and been going through the Python Crash Course book and been stuck on chapter12 "Drawing the Ship to the Screen" part. At first I have the ship.bmp in images folder in the alien_invasion folder, which is where the this code below is running. Now I have the images both in images folder and also the alien_invasion folder and it is showing this error. Please tell me what I'm doing wrong

alien_invasion3.py

import sys

import pygame 

from settings import Settings 
from shiptest 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.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)

    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.settings.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()   

Ship.py

import pygame

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

    def __init__(self, ai_game):
    """Initialize the ship and set its staring 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 the ship at its current location."""
        self.screen.blit(self.image, self.rect) 

settings.py

class Settings:
"""A class to store all settings for Alien Invasion."""

    def __init__(self):
    """Initialize the game's settings."""
    #Screen settings
        self.screen_width= 1200
        self.screen_height= 800
        self.bg_color= (0, 0, 255)          
Kingsley
  • 14,398
  • 5
  • 31
  • 53
Kentypop
  • 51
  • 7
  • 1
    Can you post the actual error you got as well please. Sometimes image loading problems can be solved by passing the full path of the image. You can try doing that. –  Jan 20 '20 at 09:07
  • @hippozhipos Thank you! I have put a link for the picture, if you have the time please take a look. I cant just attach it to the post since my account is still new – Kentypop Jan 20 '20 at 11:06
  • 1
    In the future, please add the text from your error message here instead of a picture, it will likely get you better and faster results. That being said, if you provide the full path for your image this should work. See the following post about getting the full path in there (it is for sound but the process of getting the full path is the same): https://stackoverflow.com/questions/47729260/how-to-make-the-path-to-songs-in-pygame-relative – Hoog Jan 20 '20 at 13:48
  • @Hoog got it! Thank you for this, will make better post in the future. It was very helpful, appreciate it – Kentypop Jan 22 '20 at 23:19

1 Answers1

0

Use the absolute path. For example:

self.image = pygame.image.load("F:/python_projects/AlienInvasion/images/ship.bmp")

I don’t know why it doesn’t work with a relative path.

BSMP
  • 4,596
  • 8
  • 33
  • 44