You can always assign long path to variable and later use this variable.
import os
BASE = '/Users/(My name...)/Documents/PythonProjects/Games/Game'
self.image = pygame.image.load( os.path.join(BASE, 'graphics/test/rock.png') ).convert_alpha()
Or you can create function which will get relative path and it will return full path
BASE = '/Users/(My name...)/Documents/PythonProjects/Games/Game'
def get_image(path)
return pygame.image.load( os.path.join(BASE, path) ).convert_alpha()
self.image = get_image('graphics/test/rock.png')
Second version can also keep information in some global dictionary and check if you already loaded image.
BASE = '/Users/(My name...)/Documents/PythonProjects/Games/Game'
resources = {}
def get_image(path)
if path not in resources:
resources[path] = pygame.image.load( os.path.join(BASE, path) ).convert_alpha()
return resources[path]
self.image = get_image('graphics/test/rock.png')
To make it even more useful you can generate BASE
using __file__
and it should work even if you move all to different folder
BASE = os.path.dirname( os.path.dirname(os.path.abspath(__file__)) )
Because you keep main.py
in subfolder so it may need double dirname
to move parent folder (../
)
BASE = os.path.dirname( os.path.dirname(os.path.abspath(__file__)) )