hey ,i'm having an issue with the code of a game i'm making, can someone please help me out
Posted by Hzbshh1162@reddit | learnprogramming | View on Reddit | 10 comments
here is the error i'm getting: "The image is in the same format as the one used previously in the program (which I got from someone else). Pygame 2.6.1 (SDL 2.28.4, Python 3.13.2) Hello from the pygame community. https://www.pygame.org/contribute.html 2025-03-20 10:04:55.447 Python[85995:7409126] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:. Traceback (most recent call last): File "/Users/brad/Desktop/Pyanozore copie/game.py", line 225, in
Big_Combination9890@reddit
If you post code or console output, put it in a codeblock, because your post is unreadable
If you post stacktraces, post the COMPLETE stacktrace
Hzbshh1162@reddit (OP)
ok so i did what you said, here is where i'm at, i have not different error:
FileNotFoundError: No file 'data/images/data/images/tiles/decor/0.png' found in working directory '/Users/brad/Desktop/ninja_game 3'.
despite it in fact being there
ConfidentCollege5653@reddit
data/images/data/images
Are you sure?
Hzbshh1162@reddit (OP)
here is the part of the code ,can you please check it out and tell me what exactly is wrong?
import os
import pygame
BASE_IMG_PATH = 'data/images/'
def load_image(path):
path = BASE_IMG_PATH + path
if not os.path.exists(path):
raise FileNotFoundError(f"not file in: {path}")
full_path = os.path.join(BASE_IMG_PATH, path) # Correctly join paths
img = pygame.image.load(full_path).convert()
return img
def load_images(path):
images = []
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
images.append(load_image(os.path.join(path, img_name)))
return images
class Animation:
def __init__(self, images, img_dur=5, loop=True):
self.images = images
self.loop = loop
self.img_duration = img_dur
self.done = False
self.frame = 0
def copy(self):
return Animation(self.images, self.img_duration, self.loop)
def update(self):
if self.loop:
self.frame = (self.frame + 1) % (self.img_duration * len(self.images))
else:
self.frame = min(self.frame + 1, self.img_duration * len(self.images) - 1)
if self.frame >= self.img_duration * len(self.images) - 1:
self.done = True
def img(self):
return self.images[int(self.frame / self.img_duration)]
Hzbshh1162@reddit (OP)
and here is the error message:
Game().run()
\~\~\~\~\^\^
File "/Users/brad/Desktop/ninja_game 3/game.py", line 29, in __init__
'decor': load_images('tiles/decor'),
\~\~\~\~\~\~\~\~\~\~\~\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^
File "/Users/brad/Desktop/ninja_game 3/scripts/utils.py", line 19, in load_images
images.append(load_image(os.path.join(path, img_name)))
\~\~\~\~\~\~\~\~\~\~\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^
File "/Users/brad/Desktop/ninja_game 3/scripts/utils.py", line 13, in load_image
img = pygame.image.load(full_path).convert()
\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\^\^\^\^\^\^\^\^\^\^\^
FileNotFoundError: No file 'data/images/data/images/tiles/decor/0.png' found in working directory '/Users/brad/Desktop/ninja_game 3'.
Big_Combination9890@reddit
Again, use code blocks when you post code or stacktraces.
Also the error you get is pretty clear. The file isn't where the application is looking for it.
'data/images/data/images/tiles/decor/0.png'
Look at the error. I highly doubt that there is a valid reason to have another
data/images
directory structure under a structure of the same name. You are building your paths wrong.Sorry, but this is not the kind of error where you should immediately jump to reddit and post a question. Learn to analyse the error messages you get.
ConfidentCollege5653@reddit
Sorry for the formatting, I'm on my phone
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
You add the base image path here, then when you call load_image you add it a second time.
Hzbshh1162@reddit (OP)
File "/Users/brad/Desktop/ninja_game 3/scripts/utils.py", line 10, in load_image
raise FileNotFoundError(f"not file in: {path}")
FileNotFoundError: not file in: tiles/decor/0.png
i now get this error, what would be the solution here?
ConfidentCollege5653@reddit
I don't know what you changed, but you've overcorrected and now you're not adding the base path at all.
You need to use a debugger or a bunch of print statements to trace through the code and see where it's going wrong
TopNotchNerds@reddit
I mean, looks like you have file path problems like the image is not there or is not called that or something.
do something like this in your utils.py file before you get to that line 8
import os
path = BASE_IMG_PATH + path
if not os.path.exists(path):
raise FileNotFoundError(f"not file in: {path}")
if you exit with this code before you get to the line 8 on your utils.py you gotta fix your file paths