Hi I am currently trying to create space invasion from chapter 12 in Python Crash Course. However I am stuck when trying to add the ship image to the game. Here is the code for Alien Invasion:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
#Overall class to manage game assests 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 Code:
import pygame
class Ship:
#A class to manage the ship
def __init__(self, ai_game):
#Initialize the ship and set its starting 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.scren.bilt(self.image, self.rect)
When trying to run it i get this error:
Traceback (most recent call last):
File "c:/Users/rico3/OneDrive/Desktop/Python Scripts/Alien Invasion/Alien_Invaders.py", line 39, in <module>
ai=AlienInvasion()
File "c:/Users/rico3/OneDrive/Desktop/Python Scripts/Alien Invasion/Alien_Invaders.py", line 19, in __init__
self.Ship=Ship(self)
File "c:\Users\rico3\OneDrive\Desktop\Python Scripts\Alien Invasion\ship.py", line 12, in __init__
self.image=pygame.image.load('images/ship.bmp')
pygame.error: Couldn't open images/ship.bmp
The images folder is in the same folder as the Alien Invasion file yet it still cant seem to find the ship file.

