-1

I'm going through a basic pygame tutorial for Space Invaders and I ended up getting stuck embarrassingly early. Whenever I try to import my files from an asset folder (and yes it's located in the same directory as my code) I end up with a filenotfound error. Here's my code...

import pygame
import os
import time
import random



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"))
YELLOW_SPACE_SHIP = pygame.image.load(os.path.join('assets', "pixel_ship_yellow.png")) #player ship

RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
GREEN_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_green.png"))
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))
YELLOW_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_yellow.png"))

BG = RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "background-black.png"))

I've tried doing CURRENT_PATH = os.path.dirname("assets") as well...

CURRENT_PATH = os.path.dirname("assets")
RED_SPACE_SHIP = pygame.image.load(os.path.join(CURRENT_PATH, 'pixel_ship_red_small.png'))

and all it really gives me is a slightly longer loading time with the same results.

  • Is the error coming up for all the files or just one of them? The error is quite clear, check if the file names actually match. It usually is something stupid like a underscore actually being a hyphen, or some letters being capitalized.... – mandulaj Jan 06 '21 at 04:31
  • the error's coming up for all of them and the title names, and I tried swaping out hyphens for underscores and vice-versa. I've even tried removing .png at the end only to get the same results. – penguindrummer Jan 06 '21 at 06:39

1 Answers1

0

Can you please check if the file sizes are not zero bytes. Here's the code that i tried to demonstrate the same. i have used 2 images: smiley.png (142461 bytes) and an empty image - pixel_ship_red_small.png (0 bytes)

import pygame
import os

print('size of the file: ', os.stat(os.path.join("assets", 'smiley.png')).st_size, 'bytes')
smiley = pygame.image.load(os.path.join("assets", 'smiley.png'))
print('dimensions: ', smiley.get_size())

# print('size of the file: ', os.stat(os.path.join("assets", 'pixel_ship_red_small.png')).st_size, 'bytes')
# RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", 'pixel_ship_red_small.png'))
# print('dimensions: ', RED_SPACE_SHIP.get_size())

Output:

pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
size of the file:  142461 bytes
dimensions:  (530, 532)

if you use the empty image, it will throw below error:

import pygame
import os

RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", 'pixel_ship_red_small.png'))

output:

pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\Python\temp.py", line 11, in <module>
RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", 'pixel_ship_red_small.png'))
FileNotFoundError: No such file or directory.
mukund ghode
  • 252
  • 1
  • 7