# Locating an image file in a pyGame [closed]

I am writing a simple game with pyGame.

Structure-wise, I have separated my game into several files:

• main.py # the main runner, it loads up the environment
• class_tank.py # it defines the player-controlled tank class
• class_shell.py # it defines the shells that shoot from the player's tank
• class_mob.py # it defines the enemies

The relationship of the modules above is:

• main.py imports class_tank.py, class_shell.py and class_mob.py
• all of class_tank.py, class_shell.py and class_mob.py are subclasses of pygame.Sprite.sprite.

Currently, I noticed that I am repeating myself by having the following code snippets in all four files:

current_path = os.path.dirname(os.path.realpath(__file__))
resource_path = os.path.join(current_path, 'Resources')
img_folder_path = os.path.join(resource_path, 'Images')
bg_img_path = os.path.join(img_folder_path, "xxxx.png")


xxxx.png is the image I need to load for the background (main.py), for the player (class_tank.py), for bullets (class_shell.py) and for enemies (class_mob.py).

Logically, this code snippet does belong to each of the file, but is there any elegant way to not repeat myself? The only way I can think of is to define an additional helper class.

## closed as off-topic by AJNeufeld, Toby Speight, Gerrit0, Edward, GraipherApr 4 at 8:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – AJNeufeld, Toby Speight, Gerrit0, Edward, Graipher
If this question can be reworded to fit the rules in the help center, please edit the question.

• What is the class hierarchy (if any) in your program? Which files import which other files? Without more context, we can’t give you a very good answer. – AJNeufeld Mar 31 at 23:04
• @AJNeufeld, thanks. I added more info to my post. – Yu Zhang Mar 31 at 23:11
• I want to answer your question, but as it currently stands, without actual code, it is not really a "Code Review" question. There are several ways to avoid repeating yourself. 1) Your "helper" class. 2) An Entity class that derives from pygame.sprite.Sprite, with an image load method, that your other classes derive from. 3) Dependency injection; your main module could load all images and inject them into each class. Or combinations of the above. – AJNeufeld Apr 1 at 2:23
• @AJNeufeld, thank you so much. I did not know anything about dependency injection, I will take a look. – Yu Zhang Apr 1 at 2:55

You don't necessarily need classes in Python. The helper class approach is the same direction as I would go, just with a plain module.
Basically, your resource_path and img_folder_path are constants, so I would simply create a module constants.py with the following:
import os

You can then simply import constants.py everywhere you need to know those paths.