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


Python GameState.addObject方法代码示例

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


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

示例1: GameModel

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

#.........这里部分代码省略.........
                else:
                    attributes[key] = db_attributes[key]
        return attributes
    
    def isIDUsed(self, ID):
        if self.game_state.hasObject(ID):
            return True
        for namespace in self.agents:
            if ID in self.agents[namespace]:
                return True
        return False
    
    def createUniqueID(self, ID):
        if self.isIDUsed(ID):
            id_number = 1
            while self.isIDUsed(ID + "_" + str(id_number)):
                id_number += 1
                if id_number > self.MAX_ID_NUMBER:
                    raise ValueError(
                        "Number exceeds MAX_ID_NUMBER:" + 
                        str(self.MAX_ID_NUMBER)
                    )
            
            ID = ID + "_" + str(id_number)
        return ID
    
    def moveObject(self, object_id, new_map):
        """Moves the object to a new map, or in a container
        @param object_id: ID of the object
        @type object_id: str 
        @param new_map: ID of the new map, or None
        @type object_id: str """
        game_object = self.deleteObject(object_id)
        self.game_state.addObject(object_id, new_map, game_object)

    def deleteObject(self, object_id):
        """Removes an object from the game
        @param object_id: ID of the object
        @type object_id: str """
        if self.agents["All"].has_key(object_id):
            del self.agents["All"][object_id]
        else:
            del self.items[object_id]
        return self.game_state.deleteObject(object_id)
        
    def save(self, path, filename):
        """Writes the saver to a file.
           @type filename: string
           @param filename: the name of the file to write to
           @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()       
开发者ID:parpg,项目名称:parpg,代码行数:69,代码来源:gamemodel.py

示例2: GameModel

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

#.........这里部分代码省略.........
        return items

    def createContainerObject(self, attributes):
        """Create an object that can be stored in 
        an container and return it
        @param attributes: Dictionary of all object attributes
        @type attributes: Dictionary
        @return: The created object """
        # create the extra data
        extra = {}
        extra['controller'] = self
        attributes = self.checkAttributes(attributes)
        
        info = {}
        info.update(attributes)
        info.update(extra)
        ID = info.pop("id") if info.has_key("id") else info.pop("ID")
        if not info.has_key("item_type"):
            info["item_type"] = info["type"]
        ID = self.createUniqueID(ID)
        if info.has_key("attributes"):
            attributes = info["attributes"]
            if "Container" in attributes:
                info["actions"]["Open"] = ""
                if info.has_key("Items"):
                    inventory_objs = info["Items"]
                    info["items"] = self.createContainerItems(inventory_objs)
                
                new_item = CarryableContainer(ID = ID, **info) 
            else:
                new_item = CarryableItem(ID = ID, **info) 
        else:
            new_item = CarryableItem(ID = ID, **info) 
        self.game_state.addObject(None, new_item)
        return new_item
      
    def createInventoryObject(self, container, attributes):
        """Create an inventory object and place it into a container
           @type container: base.Container
           @param container: Container where the item is on
           @type attributes: Dictionary
           @param attributes: Dictionary of all object attributes
           @return: None"""
        index = attributes.pop("index") if attributes.has_key("index") else None
        slot = attributes.pop("slot") if attributes.has_key("slot") else None
        obj = self.createContainerObject(attributes)        
        #obj = createObject(attributes, extra)
        if slot:
            container.moveItemToSlot(obj, slot)
        else:
            container.placeItem(obj, index)
    
    def deleteObject(self, object_id):
        """Removes an object from the game
        @param object_id: ID of the object
        @type object_id: str """
        del self.agents["All"][object_id]
        self.game_state.deleteObject(object_id)
        
    def save(self, path, filename):
        """Writes the saver to a file.
           @type filename: string
           @param filename: the name of the file to write to
           @return: None"""
        fname = '/'.join([path, filename])
        try:
开发者ID:mgeorgehansen,项目名称:PARPG_Technomage,代码行数:70,代码来源:gamemodel.py


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