0

I'm new to programming, and this error keeps popping up no matter what. I tried changing the file to something else, or moving all the things altogether. They are in the same folder. Here's the code:

import pygame
import os
import time
import random
pygame.font.init()
pygame.init()

WIDTH, HEIGHT = 750, 750
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Shooter Tutorial")

# Sounds
bulletSound = pygame.mixer.Sound("bullet.wav")
hitSound = pygame.mixer.Sound("hit.wav")

music = pygame.mixer.music.load("music.wav")
pygame.mixer.music.play(-1) # -1 will make the song loop

# Load images
RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))import pygame
import os
import time
import random
pygame.font.init()
pygame.init()

WIDTH, HEIGHT = 750, 750
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Shooter Tutorial")

# Sounds
bulletSound = pygame.mixer.Sound("bullet.wav")
hitSound = pygame.mixer.Sound("hit.wav")

music = pygame.mixer.music.load("music.wav")
pygame.mixer.music.play(-1) # -1 will make the song loop

# Load images
RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))

The trouble come when I run it, and they say :

Traceback (most recent call last):
 File "/Users/abhivyaktbhati/Desktop/PYTHON NOW/assets/Draft with sounds.py", line 20, in <module>
   RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
FileNotFoundError: No such file or directory.

How to fix this? PS. This is on a mac.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Why do you repeat the question? [Pygame Error : FileNotFoundError: No such file or directory](https://stackoverflow.com/questions/68020415/pygame-error-filenotfounderror-no-such-file-or-directory) – Rabbid76 Jun 17 '21 at 15:30
  • The paths need to be relative to the current working directory. The working directory is possibly different form the directory of the source files. See [Could not open resource file: pygame.error: Couldn't open sprite/test_bg.jpg](https://stackoverflow.com/questions/58177145/could-not-open-resource-file-pygame-error-couldnt-open-sprite-test-bg-jpg/58178276#58178276) – Rabbid76 Jun 17 '21 at 15:31
  • Put `import os` `os.chdir(os.path.dirname(os.path.abspath(__file__))` at the begin of your code. – Rabbid76 Jun 17 '21 at 15:32

1 Answers1

0

The file path has to be relative to the current working directory. The working directory is possibly different form the directory of the source files.

One solution is to change the working directory. The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path). Put the following code at the begin of your application:

import os

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