[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Building paths & pygame.image.load documentation



Here's my second question about pygame.image.load.  Actually it's a question about specifying paths in pygame.

In my classes, I talk about the concept of a path for loading images, sounds, etc.  I suggest that students create an "images" folder and if needed, a "sounds" folder in their project folder.   I explain the difference between absolute and relative paths, and I point out the reasons why relative paths are generally easier to use for loading such assets.

I then explain that with this type of structure, that building a path to each asset is a matter of specifying the folder name (and optional subfolder name, etc.) and the file name.  I talk about how the different operating systems use different characters as the folder separator character.  But then I point out how Python solves this issue by allowing programmers to use the "/" character as the folder separator character - and how Python maps that character into the appropriate character for the operating system on which the program is running.  I give an example that I create my demo programs on a Mac, then bring them into class and run them on a Windows machine.  As long as I create paths like:

ball = pygame.image.load('images/ball.png')

This line will work on Macs and Windows (and I assume on Linux/Unix too). 

However, when I look at the documentation for pygame.image.load as an example, the documentation says:


You should use os.path.join() for compatibility.
eg. asurf = pygame.image.load(os.path.join('data', 'bla.png'))


My question is: Is there any reason to do it this way?  This requires bringing an additional package (the os package), an extra call (to os.path.join), and it would require much more of an explanation of what os.path.join does.  

I have also seen many other books/articles/other people's code where they do the same thing by building an absolute path on-the-fly.  This all seems like a great deal of overkill.

Any reason NOT to use:   

ball = pygame.image.load('images/ball.png')   

Irv