def save(): """Save the game and exit.""" ctx=Context.getContext() s=savefile.SaveFile(SAVE_FILE) s.save({'ctx':ctx}) pygame.display.quit() sys.exit(0)
def player_pre(): ctx=Context.getContext() ctx.message_buffer.is_read=True now=time.time() ctx.pc.perception.calculateFOV() logger.warn('Elapsed: '+str(time.time()-now)) ctx.worldview.draw()
def player_post(): ctx=Context.getContext() if not len(ctx.enemies): ctx.message_buffer.addMessage("All oni have been slain and you emerged victorious!") victory_handler=input_handling.InputHandler() victory_handler.addFunction(reallyQuit, K_RETURN) victory_handler.addFunction(reallyQuit, K_SPACE) ctx.screen_manager.current.handlers.push(victory_handler)
def load(): """Loads the game if available.""" s=savefile.SaveFile(SAVE_FILE) ret=s.load() if not ret: return ret Context.ctx=ret['ctx'] return Context.getContext()
def moveBlocked(self,tile): ctx=Context.getContext() if(tile.actor==ctx.pc): ctx.message_buffer.addMessage("An oni hits you. You die! [Enter or Space to quit]") death_handler=InputHandler() death_handler.addFunction(ctx.quit, K_RETURN) death_handler.addFunction(ctx.quit, K_SPACE) ctx.screen_manager.current.handlers.push(death_handler)
def moveBlocked(self,tile): ctx=Context.getContext() if(tile.actor and isinstance(tile.actor,Oni)): ctx.message_buffer.addMessage("You hit the oni and slay it.") ctx.enemies.remove(tile.actor) tile.actor=None else: ctx.message_buffer.addMessage("Thud! You run into a wall.")
def __init__(self,viewable_area,center=None): super(GridView, self).__init__() logger.debug(str(viewable_area)) self.viewport=globals.screen.subsurface(viewable_area) self._sprites=[] (self.x,self.y)=(self.viewport.get_width()/2,self.viewport.get_height()/2) logger.debug(str(self.x)+' '+str(self.y)) self.ctx = Context.getContext() if center: self.center(center)
def tryQuit(): """Quit without saving.""" ctx=Context.getContext() ctx.message_buffer.addMessage("Commit seppuku? [y/N]") quit_handler=input_handling.InputHandler() quit_handler.addFunction(dontQuit, K_RETURN) quit_handler.addFunction(dontQuit, K_SPACE) quit_handler.addFunction(dontQuit, K_n) quit_handler.addFunction(reallyQuit, K_y) ctx.screen_manager.current.handlers.push(quit_handler)
def player_phase(): ctx=Context.getContext() event=pygame.event.wait() logger.debug(event) ret=ctx.screen_manager.current.handlers.handle(event) logger.debug(ret) if ret is not None: ctx.message_buffer.flush() ctx.worldview.draw() return True return False
def act(self): ctx=Context.getContext() choice=ctx.random.randint(0,5) if(choice==0): self.moveN() elif(choice==1): self.moveS() elif(choice==2): self.moveNW() elif(choice==3): self.moveE() elif(choice==4): self.moveW() elif(choice==5): self.moveSE()
def init(): #setup logger #logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.INFO) ctx=Context.getContext() #get random if not ctx.random: ctx.random = Random() #get PC -- important for functions below if not ctx.pc: ctx.pc=PC() #setup event handler pygame.event.set_allowed(None) pygame.event.set_allowed([KEYDOWN]) handler=input_handling.InputHandler() handler.addFunction(ctx.pc.moveSE, K_j) handler.addFunction(ctx.pc.moveNW, K_k) handler.addFunction(ctx.pc.moveN, K_y) handler.addFunction(ctx.pc.moveW, K_u) handler.addFunction(ctx.pc.moveE, K_b) handler.addFunction(ctx.pc.moveS, K_n) handler.addFunction(ctx.pc.idle, K_PERIOD) handler.addFunction(lookMode, K_l) handler.addFunction(tryQuit, K_q, (KMOD_CTRL,)) handler.addFunction(save, K_s, (KMOD_SHIFT,)) #setup screen manager ctx.screen_manager=ui.ScreenManager(ui.Screen(handler)) if not ctx.message_buffer: ctx.message_buffer=messages.MessageBuffer(ctx.screen_manager.current) pygame.display.set_caption('Ronin') pygame.display.update()
def dontQuit(): ctx=Context.getContext() ctx.message_buffer.is_read=True ctx.message_buffer.flush() ctx.screen_manager.current.handlers.pop()
def enemies_phase(): ctx=Context.getContext() for enemy in ctx.enemies: #enemy.perception.calculateFOV() enemy.act() return True
#setup screen manager ctx.screen_manager=ui.ScreenManager(ui.Screen(handler)) if not ctx.message_buffer: ctx.message_buffer=messages.MessageBuffer(ctx.screen_manager.current) pygame.display.set_caption('Ronin') pygame.display.update() if __name__ == '__main__': lie.init('ronin.conf') ctx=load() init() if ctx == None: ctx=Context.getContext() ctx.quit=reallyQuit #populate world now=time.time() logger.info("Level generation seed: "+str(now)) generator=CellularAutomata(Random(now), Floor, Wall) while True: ctx.world=generator.generateLevel(51,25) pockets=generator.getPockets(ctx.world) if len(pockets[-1])>400: for pocket in pockets[0:-1]: for tile in pocket: tile.terrain=Wall() break logger.info("trying again...") done_tiles=[]
def moveToTile(self,dst_tile): ret=super(PC,self).moveToTile(dst_tile) ctx=Context.getContext() if ret: ctx.worldview.center(ctx.worldview[dst_tile.loc].rect) return ret
def moveToLocation(self, loc): ctx=Context.getContext() dst_tile=ctx.world[loc] return self.moveToTile(dst_tile)