#๐Ÿ”’ File referencing for Parent/Child Classes.

12 messages ยท Page 1 of 1 (latest)

plush mirage
#

Hey there!
I have a parent Class (MiniGame) and some number of Child classes
My folder structure looks something like this

Assets
|----Graphics
  |-Images
    |--"Background_image.jpg
|----Sounds
  |- Music
    |--background_music.mp3
  |-Effects
    |--sfx.mp3
games
|----Memory_games
  |-Memory_Simon.py  (CHILD CLASS FILE)
|----Mini_Game.py    (PARENT CLASS FILE)
|----OptionMenu.py
main.py

Given that my Parent class MiniGame is located within Mini_Game.py (Projectdir/games/Mini_game.py) and my children classes will be found in (Projectdir/games/game category/Actual_gamefile), how might I reference the assets folder (and its subdirectories) within my ParentClass so that I might just use the filename within the children class?

unkempt pondBOT
#

@plush mirage

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

plush mirage
#

As of right Now I have been attempting to define the assets folder within the parent and create two methods for images and sounds.

class MiniGame:
    def __init__(self, screen):
        self.screen = screen
        self.clock = pygame.time.Clock()
        self.running = False
        self.game_over = False
        # Set the assets directory
        self.assets_dir = os.path.join(os.path.dirname(__file__), "..", "Assets")

    def load_image(self, filename):
        # Construct the path to the image file
        image_path = os.path.join(self.assets_dir, "Graphics", filename)
        # Load and return the image
        return pygame.image.load(image_path)

But when I attempt to call the function within a child class I get the path relative to the child.

#

Is there a way to do this without using an absolute path? I am sure using Abs path will cause problems for me down the line if I ever go to packaging the script as an executable.

junior tartan
#

Well that's the thing, __file__ is able to identify the location of the current module wherever it happens to be located.
I don't know for sure if it works properly if you plan to wrap this in pyinstaller or nuitka or whatnot, but you should rest assured as long as the relative paths between this file and others in the project stay the same.

#

On the other hand, I would recommend switching to pathlib:

from pathlib import Path

this_file = Path(__file__)
directory = this_file.parent
assets_dir = directory.parent / "Assets"

image_path = assets_dir / "Graphics" / filename
plush mirage
#

I guess I should look at why pathlib is recommended, But I seem to be having a type error when using this schema with pygame.mixer.sound(sound_path)


Error :  TypeError: Unrecognized argument (type WindowsPath)

# Parent class method definition
    def load_sound(self, filename):
        # Construct the path to the sound file
        sound_path = self.assets_dir / "Sounds" / "Music" / filename
        # Load and return the sound
        return pygame.mixer.Sound(sound_path)  # Line where I get an error.

# Child class Usage
super().__init__(screen) 
self.load_sound("background_music1.jpg") <--
#

I hope that formatting makes sense.

plush mirage
junior tartan
#

Pathlib is just nicer to work with, but yeah, in those cases where a package expects a string path, just str() convert it and you're set.

unkempt pondBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.