def _remove_component(self, component): if component.__class__ not in self._components: pass if type(self._components[component.__class__]) is list: if component in self._components[component.__class__]: self._components[component.__class__].remove(component) else: if isinstance(component, IDrawable): self._components[IDrawable] = None else: del self._components[component.__class__] if isinstance(component, IResource): try: component.unload() except: errorutils.handle_exception() if self.started: Game.instance().scene.game_object_remove_component(self, component) component.game_object = None try: component.finish() except: errorutils.handle_exception()
def play(self): """ Start the game loop. This function only return when the quit method is called. :return: """ self._running = True while self._running: try: Time.instance().update() self._handle_event() # just for safety if self.scene is not None: self.scene.update() self.scene.draw() pygame.display.flip() self.surface.fill(self.scene.background_color) if self._next_scene is not None: if self.scene is not None: self.scene.finish() self._next_scene.start() self._current_scene = self._next_scene self._next_scene = None except: errorutils.handle_exception() try: self.quit() except: errorutils.handle_exception()
def _add_component(self, component): if component.is_unique: if component.__class__ in self._components: raise Exception("Try to add a duplicate component marked as unique. " + str(component.__class__)) if isinstance(component, IDrawable): self._components[IDrawable] = component else: self._components[component.__class__] = component else: self._components.setdefault(component.__class__, []) if component not in self._components[component.__class__]: self._components[component.__class__].append(component) if isinstance(component, IResource): try: component.load() except: errorutils.handle_exception() if self.started: Game.instance().scene.game_object_add_component(self, component) component.game_object = self try: component.start() except: errorutils.handle_exception()
def finish(self): try: if self._current_scene is not None: self._current_scene.finish() pygame.quit() except: errorutils.handle_exception()
def _update_list_game_object(self): for game_object in self._included: self._game_objects[game_object.id] = game_object if game_object.get_component(IDrawable) is not None: comp = game_object.get_component(IDrawable) self._game_objects_drawable[comp.layer, comp.order_in_layer, game_object.id] = game_object if game_object.get_component(IDrawer) is not None: if game_object not in self._game_objects_drawer: self._game_objects_drawer.append(game_object) try: game_object.start() except: errorutils.handle_exception() for game_object in self._removed: if game_object.id not in self._game_objects: pass del self._game_objects[game_object.id] comp = game_object.get_component(IDrawable) if game_object.get_component(IDrawable) is not None: if (comp.layer, comp.order_in_layer, game_object.id) in self._game_objects_drawable: del self._game_objects_drawable[comp.layer, comp.order_in_layer, game_object.id] if game_object.get_component(IDrawer) is not None: if game_object in self._game_objects_drawer: self._game_objects_drawer.remove(game_object) try: game_object.finish() except: errorutils.handle_exception() self._included = [] self._removed = []
def update(self): if not self.is_updating: pass for component in self._components.values(): try: component.update() except: errorutils.handle_exception() self._update_component_list()
def draw(self): if not self.is_drawing: pass for drawer in self._game_objects_drawer: if not drawer.is_drawing: continue try: drawer.draw() except: errorutils.handle_exception()
def update(self): Physics.instance().update() if not self.is_updating: pass index = 0 self._update_list_game_object() for game_object in self._game_objects.values(): if not game_object.is_updating: continue try: game_object.update() except: errorutils.handle_exception() index += 1
def start(self): try: pygame.init() self.set_configuration() except: errorutils.handle_exception()