def loadImages(prefix, postfix = '.png', dirStr = '.',): ''' Load a sequence of images that represent an animation. ''' l = [] i=0 foundAny = False #automatically determine the amount of zero padding in the current sequence nDigits = 1 for j in range(maxDigits, 0, -1): if os.path.exists(os.path.join(dirStr,prefix + ("%.*d" % (j, 0)) + postfix)): nDigits = j while True: itemStr = (prefix + ("%.*d" % (nDigits, i)) + postfix) fullPath = os.path.join(dirStr, itemStr) if os.path.exists(fullPath): l.append(pygame.image.load(fullPath)) foundAny = True else: if not foundAny: io.tserr('ANIMATION ERROR: first item %s in animation sequence not found' % itemStr) break i += 1 return l
def quitHandler(engineReference, eventHandler): '''quitHandler will be called at the end of the program as well as the handler for a pygame.QUIT event. It sound only run once.''' if quitHandler.quitting: return quitHandler.quitting = True menulock.acquire() io.tsprint('MAIN: quit handler started.') engineReference.stopEngine() io.tsprint("MAIN: stopping sound and music...") SoundHandler.stopAll() SoundHandler.stopMusic() io.tsprint("MAIN: done") pygame.quit() if profiling: io.tsprint("MAIN: Stopping profiler and getting data....") fout = open(quitHandler.profilingDest, 'a+') l = yappi.get_stats(yappi.SORTTYPE_TTOTAL, yappi.SORTORDER_DESCENDING, yappi.SHOW_ALL) yappi.stop() if fout: for s in l: fout.write(s+'\n') fout.close() io.tsprint("MAIN: Profiling data written.") else: io.tserr("MAIN ERROR: Error opening file for profiling data") io.tsprint("MAIN: Exiting!")
def startLevel(): #create player and add to engine __builtin__.currentPlayer = reimu.PlayerClass(eventHandler.getControlState()) #eventHandler.startThread() currentEngine.setPlayer(currentPlayer) currentEngine.startEngineThread() if not hasattr(level, 'runLevel'): io.tserr("MAIN ERROR:, The level module that has been loaded does does not have a runLevel attribute.") #:If the engine is stopped before the level script runs to completion (e.g. by the #event handler handling a quit event) the engine will be stopped. Any call to any of the #methods that constitute the level-script interface to the engine will return a LevelOverException #which will halt execution of the level try: level.runLevel() except engine.LevelOverException: pass
def mainMethod(): options, args = getopt.getopt(sys.argv[1:], "qp:w:h:c:", ['help']) global COLORDEPTH, screenW, screenH, profiling if len(args) > 0: io.tserr("Unknown arguments:") io.tserr(args) io.tserr(usage) exit() for o, a in options: if o == '-q': io.tsprint.quietMode = True elif o == '-p': profiling = True import yappi io.tsprint('MAIN: Starting YAPPI profiler') yappi.start() quitHandler.profilingDest = a elif o == '-w': screenW = int(a) elif o == '-h': screenH = int(a) elif o== '--help': print usage exit() elif o=='-c': COLORDEPTH = int(a) WINSIZE=(screenW,screenH) __builtin__.level = __import__('level') pygame.init() print 'winsize', print WINSIZE screen = pygame.display.set_mode(WINSIZE,pygame.DOUBLEBUF,COLORDEPTH) pygame.display.set_caption("NANHOU PROJECT v0.1.0b") eventHandler = events.EventHandler() __builtin__.currentEngine = engine.Engine(screen,eventHandler.getControlState()) eventHandler.setQuitHandler(quitHandler, (currentEngine, eventHandler)) eventHandler.startThread() #test menu import menu m = menu.Menu(screen) while not quitHandler.quitting: menulock.acquire() m.handlekeys(eventHandler.getControlState()) m.display() pygame.display.update() menulock.release() quitHandler(currentEngine, eventHandler)
def runEngine(self): ''' This is the method that represents the thread of the engine running. It should not be called directly outside of startEngineThread. startEngineThread will start the engine in another thread. ''' while not self.done: while pygame.time.get_ticks() - self.lastCycle < Engine.frameLenMillis and not self.done: time.sleep(self.idleSleepInterval) self.lastCycle = pygame.time.get_ticks() if self.done: return if self.controlState.pausePressed(): if self.paused: self.paused = False io.tserr("ENGINE ERROR: Engine broke out of pause loop while paused!?") else: self.paused = True #draw screen and wait for unpause self.screen.fill(pygame.Color('black')) self.screen.blit(pauseScreen, (0, 0)) pygame.display.update() #wait for the pause key to be released before polling for its press #to prevent the pause state from changing twice with one press self.lock.acquire() while self.controlState.pausePressed(): time.sleep(0.001) while not self.controlState.pausePressed() and not self.done: time.sleep(self.pauseSleepInterval) while self.controlState.pausePressed(): time.sleep(0.001) self.lock.release() self.paused = False #rather than trying to be smart about timing information while the engine is paused, #for now we can just flush the FPS information on leaving pause to maintain accuracy self.fpsStartFrames = self.frameCount self.fpsStartTicks = pygame.time.get_ticks() if self.done: return try: traceback.extract_tb(self.engineLoop()) except Exception: traceback.print_tb(sys.exc_info()[2]) io.tserr("ENGINE: exception caught:") io.tserr(sys.exc_info()[:]) return