I am writing a simple game with pyGame.
Structure-wise, I have separated my game into several files:
# the main runner, it loads up the environment
# it defines the player-controlled tank class
# it defines the shells that shoot from the player's tank
# it defines the enemies
The relationship of the modules above is:
- all of
class_mob.pyare subclasses of
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