def handleFPS(self): '''Update logic for determining FPS.''' #There are more accurate ways to handle that, but we don't need to worry about that now. if (pygame.time.get_ticks() - self.fpsStartTicks)/1000.0 >= 1.0 / self.fpsFrequency: self.fps = (self.frameCount - self.fpsStartFrames) * 1.0 / (pygame.time.get_ticks() - self.fpsStartTicks) * 1000 self.fpsStartFrames = self.frameCount self.fpsStartTicks = pygame.time.get_ticks() io.tsprint('ENGINE: fps: %d' % self.fps)
def startThread(self): #in case the thread is being restarted #at some point self.quit = False t = threading.Thread(target=self.eventCycle, args=()) t.daemon = True t.start() #thread.start_new_thread(self.eventCycle, ()) io.tsprint( 'EVENT HANDLER: thread started.')
def sleepFrames(self, frames): '''Anything calling this method will block for a given number of engine frames and receive a LevelOverException if the engine is stopped before the time is up..''' end = self.frameCount + frames while self.frameCount < end: #this way the level will end promptly even if the engine #quits while the leve is waiting for a long time if self.done: io.tsprint ('ENGINE: engine stopped while level script is sleeping.') io.tsprint ('ENGINE: Sending exception to level...') raise LevelOverException() else: time.sleep(self.idleSleepInterval)
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 handleKeyPress(self, e): ''' Handle key press event queue. ''' if e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE : self.keypause = True elif e.key == pygame.K_UP: self.keyup = True elif e.key == pygame.K_DOWN: self.keydown = True elif e.key == pygame.K_LEFT: self.keyleft = True elif e.key == pygame.K_RIGHT: self.keyright = True elif e.key == pygame.K_z: self.keyshot = True elif e.key == pygame.K_x: self.keybomb = True elif e.key == pygame.K_LSHIFT: self.keyslow = True else: io.tsprint ('CONTROL STATE: unrecognized keypress %d' % e.key) elif e.type == pygame.KEYUP: if e.key == pygame.K_ESCAPE : self.keypause = False elif e.key == pygame.K_UP: self.keyup = False elif e.key == pygame.K_DOWN: self.keydown = False elif e.key == pygame.K_LEFT: self.keyleft = False elif e.key == pygame.K_RIGHT: self.keyright = False elif e.key == pygame.K_z: self.keyshot = True elif e.key == pygame.K_x: self.keybomb = True elif e.key == pygame.KMOD_LSHIFT: self.keyslow = True
def stopEngine(self): '''This will let the engine finish its current frame and then stop the engine.''' io.tsprint('ENGINE: stopping engine...') skipLock = self.paused if not skipLock: self.lock.acquire() if self.done: io.tsprint('ENGINE: Engine is already stopped.') else: self.done = True io.tsprint('ENGINE: stopped') if not skipLock: self.lock.release()
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 addEntities(self, entity, eType): '''append the entities in the entity list of type eType''' checkType(eType) io.tsprint( 'ENGINE: %d %s added' % (len(entity), typeStr[eType])) self.eList[eType].extend(entity)
def addEntity(self, entity, eType): '''append the entity of type eType''' checkType(eType) io.tsprint('ENGINE: %s added', typeStr[eType]) self.eList[eType].append(entity)
def stopThread(self): io.tsprint( 'EVENT HANDLER: stopping thread...') self.lock.acquire() self.quit = True io.tsprint( 'EVENT HANDLER: thread stopped') self.lock.release()