def __setupContainer(self, container): self.container = container self.container.setVSync(True) self.inputListener = InputListener() self.inputListener.associateInputProvider( InputProvider(self.container.getInput()), self.container.getInput(), DEFAULT_INPUT_MAP)
class GameManager: ''' Manages everything in a game instance. ''' def __init__(self, name, cfgPath, xSize, ySize): ''' @xSize - the intended horizontal video size @ySize - the intended vertical video size @cfgPath - path to a the config file to use ''' self.name = name self.currentWorld = None self.currentWorldIndex = 0 self.gameStartTime = None self.gameStarted = False self.config = GameConfigParser.parse(cfgPath) self.levelOrder = ArrayDeque([ #~ 'Factory', 'Ninja Rope Test 1', # ... ]) self.levelPaths = { 'Ninja Rope Test 1' : LEVEL_DIR + 'ninja_test_1.tmx', #~ 'Factory' : LEVEL_DIR + 'Factory_1_R1024x768_T32x32.tmx', # ... } self.checkLevelOrderComposition() def checkLevelOrderComposition(self): # this just checks that all entries in self.levelOrder actually # correspond to entries in levelPaths, and that all of the # level paths point to existing tmx files. for name in self.levelOrder: if name not in self.levelPaths: raise ValueError('%s not found in levelPaths!' % name) path = self.levelPaths[name] if not os.path.exists(path): raise ValueError( 'Level path doesn\'t exist : %s' % name) def loadWorld(self, name): ''' Loads and returns the named world as a WorldSimulator. ''' print 'Loading World', name, 'at', self.levelPaths[name] return TMXParser.parseLevelToSimulator( self.levelPaths[name], FileInputStream(self.levelPaths[name]), TILE_SETS_PATH, self.config) def __setupContainer(self, container): self.container = container self.container.setVSync(True) self.inputListener = InputListener() self.inputListener.associateInputProvider( InputProvider(self.container.getInput()), self.container.getInput(), DEFAULT_INPUT_MAP) def init(self, container): ''' Overriden superclass method. Sets up sound, user input and graphics systems, then loads the initial world. ''' self.__setupContainer(container) if not self.currentWorld: self.currentWorld = self.loadWorld( self.levelOrder[0]) assert self.currentWorld, 'GameManager.currentWorld is None' self.gameStartTime = time.time() self.gameStarted = True print 'Game started at', self.gameStartTime print 'Current Level :' def render(self, container, graphics): ''' Overriden superclass method. Renders the current level starting at the top left of the screen. ''' self.currentWorld.render(graphics) def update(self, container, delta): self.currentWorld.update()