Skip to content

Fix asset loading to use script-relative paths for portability#2

Open
saifeemustafaq wants to merge 1 commit intosoftchris:mainfrom
saifeemustafaq:main
Open

Fix asset loading to use script-relative paths for portability#2
saifeemustafaq wants to merge 1 commit intosoftchris:mainfrom
saifeemustafaq:main

Conversation

@saifeemustafaq
Copy link
Copy Markdown

Fix asset loading to use script-relative paths for portability

  • Added a helper function get_asset_path() to construct asset paths relative to the script's location.
  • Updated all asset loading (images, sounds, etc.) to use this function.
  • This ensures the game works correctly regardless of the current working directory when running the script.

Problem

Previously, all asset files (images, sounds, etc.) were loaded using relative paths like "assets/player.png". This approach only works if the script is run from the directory containing the assets folder. If the script is run from a different working directory, asset loading fails with a FileNotFoundError.

Solution

This PR introduces a helper function, get_asset_path(), which constructs asset paths relative to the location of the script (refactored.py). All asset loading calls have been updated to use this function. This ensures that assets are always loaded correctly, regardless of the current working directory from which the script is executed.

Why this is important

  • Portability: The game can now be run from any directory, not just the project root.
  • Robustness: Prevents runtime errors related to missing asset files due to incorrect working directories.
  • Maintainability: Centralizes asset path logic, making future changes easier and reducing code duplication.

Additional Notes

  • The get_asset_path() function is documented to clarify its purpose.
  • All asset loading (images, sounds, etc.) has been updated to use this function.
  • No changes were made to the actual asset files or their locations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant