class ApplicationTemplate(object): # settings that can be accessed by other objects. SETTINGS = settings() ''' default __init__() function - sets the window dimensions and creates a BasicScreen ''' def __init__(self, WIN_HEIGHT_DEFAULT, WIN_WIDTH_DEFAULT): # save window height and width that we started out with. if WIN_HEIGHT_DEFAULT is not None: self.SETTINGS.WINDOW_HEIGHT_DEFAULT = WIN_HEIGHT_DEFAULT if WIN_WIDTH_DEFAULT is not None: self.SETTINGS.WIN_WIDTH_DEFAULT = WIN_WIDTH_DEFAULT # always initialize with 'BasicScreen' config. self.current_screen = BasicScreen(self) ''' startCurrentScreen() will start the currently configured Screen. ''' def startCurrentScreen(self): # register 'current_screen' handlers. self.window.push_handlers("on_key_press", self.current_screen.on_key_press) self.window.push_handlers("on_draw", self.current_screen.on_draw) self.window.push_handlers("on_resize", self.current_screen.on_resize) # schedule the 'current_screen.update()' method to be called each 'update_interval' pyglet.clock.schedule_interval(self.current_screen.update, self.update_interval) # call start of the current scene / screen - will create additional handlers. self.current_screen.start() ''' run 'clearCurrentScreen' in case you want to switch between Screens ''' def clearCurrentScreen(self): # undo most of the stuff that had been done in 'startCurrentScreen' pyglet.clock.unschedule(self.current_screen.update) self.current_screen.clear() self.window.remove_handlers("on_key_press", self.current_screen.on_key_press) self.window.remove_handlers("on_draw", self.current_screen.on_draw) self.window.remove_handlers("on_resize", self.current_screen.on_resize) ''' sets the current screen by passing the current Application to it ''' def set_current_screen(self, current_screen): self.current_screen = current_screen(self) ''' default key press function ''' def on_key_press(self, symbol, modifiers): pass ''' default setup_window() function which builds a pyglet window this function should not depend on any parameters that are being set in 'setup()'! ''' def setup_window(self): # setup the pyglet window. self.window = pyglet.window.Window(width=self.SETTINGS.WINDOW_WIDTH_DEFAULT, height=self.SETTINGS.WINDOW_HEIGHT_DEFAULT, resizable=True) # try to load our icon (if present) try: self.window.set_icon(pyglet.resource.image('icon.png')) except: pass ''' setup() function for all custom stuff that your application will need / do ''' def setup(self): pass ''' run function invoked by the user - starts everything in the right order ''' def run(self): # setup window first, because some of the objects in 'setup()' need a window. self.setup_window() self.setup() self.startCurrentScreen() pyglet.app.run()