0

So I recently got the PyCharm IDE for Linux mint along with the pygame module. So I am trying to put in some images for the background and player sprite by It works perfectly fine when I use a text editor and run it with the terminal. However, the PyCharm IDE gives the following error:

Traceback (most recent call last): File "/home/username/PycharmProjects/pythonProject/Pycharm/main.py", line 50, in <module> backdrop = pygame.image.load(os.path.join('images', 'stage.png')) FileNotFoundError: No such file or directory.

the background is located in : /home/username/PycharmProjects/pythonProject/Pycharm/images file is located in : /home/username/PycharmProjects/pythonProject/Pycharm sprite (which is a bunch of images) is located in : /home/username/PycharmProjects/pythonProject/Pycharm/skeleani

I am completely new to pygame and Pycharm (about 3 hrs of experience) and I decided to copy some code online and run them and study them, for practice.

code: `import pygame import sys import os

'''
Variables
'''

worldx = 960
worldy = 720
fps = 40  # frame rate
ani = 4   # animation cycles
main = True

BLUE = (25, 25, 200)
BLACK = (23, 23, 23)
WHITE = (254, 254, 254)


'''
Objects

'''

class Player(pygame.sprite.Sprite):
"""
Spawn a player
"""

def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.images = []
    for i in range(1, 14):
        img = pygame.image.load(os.path.join('skeleani', 'skele' + str(i) + '.png')).convert()
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()



'''
Setup

'''

clock = pygame.time.Clock()
pygame.init()
world = pygame.display.set_mode([worldx, world])
backdrop = pygame.image.load(os.path.join('images', 'stage.png'))
backdropbox = world.get_rect()

player = Player()   # spawn player
player.rect.x = 0   # go to x
player.rect.y = 0   # go to y
player_list = pygame.sprite.Group()
player_list.add(player)


'''
Main Loop
'''

 while main:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        try:
            sys.exit()
        finally:
            main = False

    if event.type == pygame.KEYDOWN:
        if event.key == ord('q'):
            pygame.quit()
        try:
            sys.exit()
        finally:
            main = False
world.blit(backdrop, backdropbox)
player_list.draw(world) # draw player
pygame.display.flip()
clock.tick(fps)`

source: https://opensource.com/article/17/12/game-python-add-a-player

I searched for about 30 mins for a solution but none of them work for me

  • The file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file. Try `import os` `os.chdir(os.path.dirname(os.path.abspath(__file__)))` – Rabbid76 Oct 26 '21 at 11:07
  • If you import `pathlib` from the Python standard library you can get your path relative to the current python script with with `pathlib.Path(__file__).parent / 'images' / 'stage.png'`. – Matthias Oct 26 '21 at 11:11

0 Answers0