[PyGame] Importing a .png image - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] Importing a .png image (/thread-21117.html) |
Importing a .png image - Myang123 - Sep-15-2019 I am trying to create a class called Ship in a separate .py file and I am trying to import its associated .png image (ship.png) into my Pygame file and every time I try to import the image or the class I receive this error message: The code for the separate .py file for the ship class is this:class Ship(): def __init__(self, screen): self.screen = screen self.image = pygame.image.load('ship.png') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom def blitme(self): self.screen.blit(self.image, self.rect)The source code for the file I am trying to import the ship image and class into is this: Please note that there is another class called settings but I am sure that is not the cause of the problem since the program ran fine before with it in it. import sys import pygame from alien_invasion_settings import Settings from ship import Ship def run_game(): # init the game and create a screen object pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invansion") ship = Ship(screen) # main loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(ai_settings.bg_color) ship.blitme() pygame.display.flip() run_game()Any help would be greatly appreciated and thank you so much for your time. * All the files are saved in the exact same folder and the image is in a .png format. RE: Importing a .png image - Larz60+ - Sep-15-2019 You need to install pygame From command line, not in python interpreter: pip install pygame RE: Importing a .png image - buran - Sep-15-2019 It looks you didn't import pygame in ship.py RE: Importing a .png image - SheeppOSU - Sep-15-2019 If that's all the code, you should keep it all in one file to more easily edit everything. Though if you plan to make many different classes, you can put them all in a different files and import them into a main file like you are doing with the Ship class. It just depends on how big the classes are and how many. Having 6 classes in one file could be disorganized and make it hard to find certain code. With just one class though, it can stay in the same file as main. |