0

I am trying to create a game based on Eric Matiz's book "learning python" and created the Ship module, there is an error in this module that I cannot solve

Here is the code itself:

import pygame


class Ship():

    def __init__(self, screen):
        """Init ship and sets its starting position"""
        self.screen = screen

        # load image ship and gets rectangle.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # every new ship is starting in bottom edge screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

    def bliteme(self):
        """Draws the ship at the current position."""
        self.screen.blit(self.image, self.rect)

I tried to do it through the "os" module, but nothing worked Error:

self.image = pygame.image.loading('images/ship.bmp')
FileNotFoundError error: There is no such file or directory.

The file is located in the "images" directory with the name "ship.bmp", the path is specified correctly, but still gives an error

I tried to find an answer on the internet, but mostly such errors remain without a working answer

  • 1
    Hello Artem and welcome to Stack Overflow. Traceback would help to troubleshoot your problem. – ex4 Feb 13 '22 at 14:38
  • 1
    Welcome to Stack Overflow. Please read [ask]. What do you mean by "install"? What do you mean by "it gives an error"? What is "it"? What do you mean by "nothing worked"? **What** did you "try to do through the 'os' module", and **how**? *How do you try to use the code*? When you try doing that, *exactly what happens*, and *how is that different* from what is supposed to happen? If there is an error message, [copy and paste it, starting with the line that says `Traceback (most recent call last):`, and format it like code.](https://meta.stackoverflow.com/questions/359146) – Karl Knechtel Feb 13 '22 at 14:38
  • @ex4 I tried, but it shows the line "self.image = pygame.image.load('images/ship.bmp')" and gives the error "self.image = pygame.image.load('images/ship.bmp') FileNotFoundError: No such file or directory." – Artem Shevlyakov Feb 13 '22 at 14:39
  • And please remember to *ask a question*, don't just say that there is a problem. Use a word like "why" or "how", and a `?`, and make sure it is clear *what you need us to tell you*. – Karl Knechtel Feb 13 '22 at 14:39
  • If error is "FileNotFound", then probably the problem is that the file does not exist, at least in the place you are looking it for. I'd start by providing absolute path (like /home/user/projects/game/images/ship.bmp) and see if that works. – ex4 Feb 13 '22 at 14:43
  • Okay, so, `images/ship.bmp` is a relative path, yes? What do you expect the path to be relative *to*? Why? Do you know what a *current working directory* is? When you were "trying to do it through the 'os' module", did you see something that helps you check the current working directory? Did you try using it? Did you get the result you expected? – Karl Knechtel Feb 13 '22 at 14:48
  • @KarlKnechtel. The person who answered below has already helped me. Thank you for trying to answer my request – Artem Shevlyakov Feb 13 '22 at 14:51
  • If that answers the question, then it is a simple duplicate - please see for example https://stackoverflow.com/questions/918154/relative-paths-in-python, and many others. – Karl Knechtel Feb 13 '22 at 14:59

1 Answers1

0

It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. The image file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script.
Put the following at the beginning of your code to set the working directory to the same as the script's directory:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174