当前位置: 首页>>代码示例>>Python>>正文


Python GameState.restoreFromState方法代码示例

本文整理汇总了Python中gamestate.GameState.restoreFromState方法的典型用法代码示例。如果您正苦于以下问题:Python GameState.restoreFromState方法的具体用法?Python GameState.restoreFromState怎么用?Python GameState.restoreFromState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gamestate.GameState的用法示例。


在下文中一共展示了GameState.restoreFromState方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GameModel

# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import restoreFromState [as 别名]

#.........这里部分代码省略.........
           @return: None"""
        fname = '/'.join([path, filename])
        try:
            save_file = open(fname, 'w')
        except(IOError):
            sys.stderr.write("Error: Can't create save game: " + fname + "\n")
            return

        save_state = {}
        save_state["Agents"] = self.agents
        save_state["Items"] = self.items
        save_state["GameState"] = self.game_state.getStateForSaving()
        
        yaml.dump(save_state, save_file)
        
        save_file.close()       

    def load(self, path, filename):
        """Loads a saver from a file.
           @type filename: string
           @param filename: the name of the file (including path) to load from
           @return: None"""
        fname = os.path.join(path, filename)

        try:
            load_file = open(fname, 'r')
        except(IOError):
            sys.stderr.write("Error: Can't find save game file '" + fname + "'\n")
            return        
        self.deleteMaps()
        self.clearAgents()
        
        save_state = yaml.load(load_file)
        self.game_state.restoreFromState(save_state["GameState"])
        maps = save_state["Agents"]
        for map_name in maps:
            for agent_name in maps[map_name]:
                agent = {agent_name:maps[map_name][agent_name]}
                self.addAgent(map_name, agent)
        self.items = save_state["Items"]               
      
        load_file.close()             
         
    def teleport(self, agent, position):
        """Called when a an agent is moved instantly to a new position. 
        The setting of position may wan to be created as its own method down 
        the road.
        @type position: String Tuple
        @param position: X,Y coordinates passed from engine.changeMap
        @return: fife.Location"""
        logging.debug(position)
        coord = fife.DoublePoint3D(float(position[0]), float(position[1]), 0)
        location = fife.Location(self.active_map.agent_layer)
        location.setMapCoordinates(coord)
        agent.teleport(location)         
               
    def getObjectAtCoords(self, coords):
        """Get the object which is at the given coords
        @type coords: fife.Screenpoint
        @param coords: Coordinates where to check for an object
        @rtype: fife.Object
        @return: An object or None"""
        instances = self.active_map.cameras[
                                            self.active_map.my_cam_id].\
            getMatchingInstances(coords, self.active_map.agent_layer)
        # no object returns an empty tuple
开发者ID:parpg,项目名称:parpg,代码行数:70,代码来源:gamemodel.py

示例2: GameModel

# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import restoreFromState [as 别名]

#.........这里部分代码省略.........
            if agent == "PlayerCharacter":
                agent_obj = self.game_state.player_character
            else:
                agent_obj = self.game_state.getObjectById(agent, map_name)
            if agent_obj:
                agent_inst = self.game_state.maps[map_name].\
                                    agent_layer.getInstance(agent)
                agent_dict.update(agent_obj.getStateForSaving())
                agent_dict["Rotation"] = agent_inst.getRotation()
                agent_dict["MapName"] = map_name
            agents_dict[agent] = agent_dict
        save_state["Agents"]["All"] = agents_dict
        save_state["GameState"] = self.game_state.getStateForSaving()
        yaml.dump(save_state, save_file)
        
        save_file.close()       

    def load(self, path, filename):
        """Loads a saver from a file.
           @type filename: string
           @param filename: the name of the file (including path) to load from
           @return: None"""
        fname = '/'.join([path, filename])

        try:
            load_file = open(fname, 'r')
        except(IOError):
            sys.stderr.write("Error: Can't find save game file\n")
            return        
        self.deleteMaps()
        self.clearAgents()
        
        save_state = yaml.load(load_file)
        self.game_state.restoreFromState(save_state["GameState"])
        maps = save_state["Agents"]
        for map_name in maps:
            for agent_name in maps[map_name]:
                agent = {agent_name:maps[map_name][agent_name]}
                self.addAgent(map_name, agent)
                
        # Load the current map
        if self.game_state.current_map_name:
            self.loadMap(self.game_state.current_map_name)         
        load_file.close()
        

        # Recreate all the behaviours. These can't be saved because FIFE
        # objects cannot be pickled
        
        self.placeAgents()
        self.placePC()
      
        # In most maps we'll create the PlayerCharacter Instance internally. 
        # In these cases we need a target position
         
    def teleport(self, agent, position):
        """Called when a an agent is moved instantly to a new position. 
        The setting of position may wan to be created as its own method down the road.
        @type position: String Tuple
        @param position: X,Y coordinates passed from engine.changeMap
        @return: fife.Location"""
        print position
        coord = fife.DoublePoint3D(float(position[0]), float(position[1]), 0)
        location = fife.Location(self.active_map.agent_layer)
        location.setMapCoordinates(coord)
        agent.teleport(location)         
开发者ID:mgeorgehansen,项目名称:PARPG_Technomage,代码行数:70,代码来源:gamemodel.py


注:本文中的gamestate.GameState.restoreFromState方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。