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

Re: [pygame] setting + modifying a variable for use across the entire application



Hi Sean,

Why not do the platform check in acolyte_settings.py itself?

#acolyte_settings.py

ANDROID = False
try:
    import android
except ImportError:
    pass
else:
    ANDROID = True
    ASSETS_DIR = './assets'

#main.py
import gameloop

def (main):
    gameloop.gameloop()


The platform will be determined once, when acolyte_settings is first imported. So main.py does not even need to import acolyte_settings unless the platform settings are need in main itself. This is modular programming.

Lenard Lindstrom

On 16/12/11 12:34 PM, Sean Wolfe wrote:
BTW guys, this is working for me. I'm using a module as an
application-level variable store. In my top-level main.py, I'm setting
variables that are referenced by modules down the line.

I had to do a couple things to make it work. One, the app-level
variables can't be already defined when imported, they have to be new
values which are set after the import by main.py. Two, any subsequent
modules which are imported have to import AFTER I'm done creating my
"app variables".

So it looks like this:

-----
main.py:

import acolyte_settings
try:
     import android
except ImportError:
     android = False
if android == True:
     acolyte_settings.ANDROID = True
     acolyte_settings.ASSETS_DIR = './assets'

import gameloop

def (main):
     gameloop.gameloop()

Now gameloop can use the acolyte_settings module to find out the
application settings. I don't have to write to an external file, or
constantly pass 'android=True' to all my classes. It's not all that
practical for multiple changes to the app-level variables, but for my
purposers, I like it!