def __init__(self, level_state): self.level = level_state self.level.map.load_graphics() self.map = self.level.map self.gfx_manager = GfxManager() pygame.time.set_timer(E_UPDATE_SPRITE_FRAMES, 200) pygame.time.set_timer(E_UPDATE_SPRITE_POSITIONS, 50) self.ui = TouhouUI(self.level) self.ui.generate_menus() self.event_catalog = {} self.assign_event_handler(QUIT, self.quit) self.assign_event_handler( E_UPDATE_SPRITE_FRAMES, self.map.frame_update) # For animated sprites self.assign_event_handler(E_UPDATE_SPRITE_POSITIONS, self.map.update_objects) # Movement self.assign_event_handler(E_USER_INPUT, self.ui_events) self.assign_event_handler(E_OBJECT_SIGNAL, self.object_events) self.assign_event_handler(KEYDOWN, self.ui.key_down) self.assign_event_handler(KEYUP, self.ui.key_up) self.assign_event_handler(MOUSEBUTTONDOWN, self.ui.update_mouse) self.assign_event_handler(MOUSEBUTTONUP, self.ui.update_mouse) self.assign_event_handler(MOUSEMOTION, self.ui.update_mouse)
def __init__(self, level_state): self.level = level_state self.level.map.load_graphics() self.map = self.level.map self.gfx_manager = GfxManager() pygame.time.set_timer(E_UPDATE_SPRITE_FRAMES,200) pygame.time.set_timer(E_UPDATE_SPRITE_POSITIONS,50) self.ui = TouhouUI(self.level) self.ui.generate_menus() self.event_catalog = {} self.assign_event_handler(QUIT, self.quit) self.assign_event_handler(E_UPDATE_SPRITE_FRAMES,self.map.frame_update) # For animated sprites self.assign_event_handler(E_UPDATE_SPRITE_POSITIONS,self.map.update_objects) # Movement self.assign_event_handler(E_USER_INPUT,self.ui_events) self.assign_event_handler(E_OBJECT_SIGNAL,self.object_events) self.assign_event_handler(KEYDOWN, self.ui.key_down) self.assign_event_handler(KEYUP, self.ui.key_up) self.assign_event_handler(MOUSEBUTTONDOWN, self.ui.update_mouse) self.assign_event_handler(MOUSEBUTTONUP, self.ui.update_mouse) self.assign_event_handler(MOUSEMOTION, self.ui.update_mouse)
class TouhouPlay: SCROLL_SPEED = 5 def __init__(self, level_state): self.level = level_state self.level.map.load_graphics() self.map = self.level.map self.gfx_manager = GfxManager() pygame.time.set_timer(E_UPDATE_SPRITE_FRAMES,200) pygame.time.set_timer(E_UPDATE_SPRITE_POSITIONS,50) self.ui = TouhouUI(self.level) self.ui.generate_menus() self.event_catalog = {} self.assign_event_handler(QUIT, self.quit) self.assign_event_handler(E_UPDATE_SPRITE_FRAMES,self.map.frame_update) # For animated sprites self.assign_event_handler(E_UPDATE_SPRITE_POSITIONS,self.map.update_objects) # Movement self.assign_event_handler(E_USER_INPUT,self.ui_events) self.assign_event_handler(E_OBJECT_SIGNAL,self.object_events) self.assign_event_handler(KEYDOWN, self.ui.key_down) self.assign_event_handler(KEYUP, self.ui.key_up) self.assign_event_handler(MOUSEBUTTONDOWN, self.ui.update_mouse) self.assign_event_handler(MOUSEBUTTONUP, self.ui.update_mouse) self.assign_event_handler(MOUSEMOTION, self.ui.update_mouse) def start(self): self.running = True def quit(self, e): self.running = False # Assign a function to the event. Can't be overwritten. # e: event name, handler: function name def assign_event_handler(self, event_type, handler): if event_type not in self.event_catalog: self.event_catalog[event_type] = handler else: raise OverwriteError(event_type, self.event_catalog[event_type]) def process(self): event_list = pygame.event.get() self.ui.update((self.gfx_manager.x,self.gfx_manager.y)) self.process_ui() self.gfx_manager.register_draw(self.map.draw_ground()) self.gfx_manager.register_draw(self.ui.ui.draw_under()) self.gfx_manager.register_draw(self.map.draw_sprites()) self.gfx_manager.register_draw(self.ui.ui.draw()) for e in event_list: try: f = self.event_catalog[e.type] f(e) except KeyError: pass #print e, "event not registered" self.scroll_map() # Respond to changes in UI interface def process_ui(self): data = self.ui.data if data.mode == I_BROWSE: pass elif data.mode == I_MOVE: if data.dest: path = astar.path(self.map, data.selected.pos, data.dest) x,y = self.ui.data.selected.pos self.map.grid[x][y].move_path(path) elif data.mode == I_ATTACK: pass else: print "Unexpected Mode" def _attack(self, attacker, defender): damage = self.level.creatures[attacker].attack self.level.creatures[defender].change_hp(-damage) print attacker, "attacks", defender, "for", damage print defender, "has", self.level.creatures[defender].hp, "hp" if self.level.creatures[defender].hp <= 0: print defender, "died." self.level.kill_creature(defender) self.ui.unselect() def ui_events(self, e): ui_command = e.subtype if ui_command == MOVETO: self.move_character(e) elif ui_command == ENDTURN: self.level.end_turn() elif ui_command == ATTACK: try: attacker = e.attacker defender = self.level.get_object(e.target) self.ui.set_selected_object(e.target) self._attack(attacker, defender) except: print "Invalid Attack Command" def object_events(self, e): if e.subtype == OBJECTEVENT: self.map.update_obj_pos(e.obj) if not e.obj.moving: self.ui.set_browse() def move_character(self, e): pos = self.map.obj_list[e.name] path = astar.path(self.map, pos, e.dest) x,y = pos self.map.grid[x][y].move_path(path) def scroll_map(self): if self.ui.keybuffer[K_UP]: self.gfx_manager.shift((0,-self.SCROLL_SPEED)) elif self.ui.keybuffer[K_DOWN]: self.gfx_manager.shift((0,self.SCROLL_SPEED)) if self.ui.keybuffer[K_LEFT]: self.gfx_manager.shift((self.SCROLL_SPEED,0)) elif self.ui.keybuffer[K_RIGHT]: self.gfx_manager.shift((-self.SCROLL_SPEED,0))
class TouhouPlay: SCROLL_SPEED = 5 def __init__(self, level_state): self.level = level_state self.level.map.load_graphics() self.map = self.level.map self.gfx_manager = GfxManager() pygame.time.set_timer(E_UPDATE_SPRITE_FRAMES, 200) pygame.time.set_timer(E_UPDATE_SPRITE_POSITIONS, 50) self.ui = TouhouUI(self.level) self.ui.generate_menus() self.event_catalog = {} self.assign_event_handler(QUIT, self.quit) self.assign_event_handler( E_UPDATE_SPRITE_FRAMES, self.map.frame_update) # For animated sprites self.assign_event_handler(E_UPDATE_SPRITE_POSITIONS, self.map.update_objects) # Movement self.assign_event_handler(E_USER_INPUT, self.ui_events) self.assign_event_handler(E_OBJECT_SIGNAL, self.object_events) self.assign_event_handler(KEYDOWN, self.ui.key_down) self.assign_event_handler(KEYUP, self.ui.key_up) self.assign_event_handler(MOUSEBUTTONDOWN, self.ui.update_mouse) self.assign_event_handler(MOUSEBUTTONUP, self.ui.update_mouse) self.assign_event_handler(MOUSEMOTION, self.ui.update_mouse) def start(self): self.running = True def quit(self, e): self.running = False # Assign a function to the event. Can't be overwritten. # e: event name, handler: function name def assign_event_handler(self, event_type, handler): if event_type not in self.event_catalog: self.event_catalog[event_type] = handler else: raise OverwriteError(event_type, self.event_catalog[event_type]) def process(self): event_list = pygame.event.get() self.ui.update((self.gfx_manager.x, self.gfx_manager.y)) self.process_ui() self.gfx_manager.register_draw(self.map.draw_ground()) self.gfx_manager.register_draw(self.ui.ui.draw_under()) self.gfx_manager.register_draw(self.map.draw_sprites()) self.gfx_manager.register_draw(self.ui.ui.draw()) for e in event_list: try: f = self.event_catalog[e.type] f(e) except KeyError: pass #print e, "event not registered" self.scroll_map() # Respond to changes in UI interface def process_ui(self): data = self.ui.data if data.mode == I_BROWSE: pass elif data.mode == I_MOVE: if data.dest: path = astar.path(self.map, data.selected.pos, data.dest) x, y = self.ui.data.selected.pos self.map.grid[x][y].move_path(path) elif data.mode == I_ATTACK: pass else: print "Unexpected Mode" def _attack(self, attacker, defender): damage = self.level.creatures[attacker].attack self.level.creatures[defender].change_hp(-damage) print attacker, "attacks", defender, "for", damage print defender, "has", self.level.creatures[defender].hp, "hp" if self.level.creatures[defender].hp <= 0: print defender, "died." self.level.kill_creature(defender) self.ui.unselect() def ui_events(self, e): ui_command = e.subtype if ui_command == MOVETO: self.move_character(e) elif ui_command == ENDTURN: self.level.end_turn() elif ui_command == ATTACK: try: attacker = e.attacker defender = self.level.get_object(e.target) self.ui.set_selected_object(e.target) self._attack(attacker, defender) except: print "Invalid Attack Command" def object_events(self, e): if e.subtype == OBJECTEVENT: self.map.update_obj_pos(e.obj) if not e.obj.moving: self.ui.set_browse() def move_character(self, e): pos = self.map.obj_list[e.name] path = astar.path(self.map, pos, e.dest) x, y = pos self.map.grid[x][y].move_path(path) def scroll_map(self): if self.ui.keybuffer[K_UP]: self.gfx_manager.shift((0, -self.SCROLL_SPEED)) elif self.ui.keybuffer[K_DOWN]: self.gfx_manager.shift((0, self.SCROLL_SPEED)) if self.ui.keybuffer[K_LEFT]: self.gfx_manager.shift((self.SCROLL_SPEED, 0)) elif self.ui.keybuffer[K_RIGHT]: self.gfx_manager.shift((-self.SCROLL_SPEED, 0))