本文整理汇总了Python中gamestate.GameState.getStateForSaving方法的典型用法代码示例。如果您正苦于以下问题:Python GameState.getStateForSaving方法的具体用法?Python GameState.getStateForSaving怎么用?Python GameState.getStateForSaving使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gamestate.GameState
的用法示例。
在下文中一共展示了GameState.getStateForSaving方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GameModel
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import getStateForSaving [as 别名]
#.........这里部分代码省略.........
"""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()
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):
示例2: GameModel
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import getStateForSaving [as 别名]
#.........这里部分代码省略.........
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:
save_file = open(fname, 'w')
except(IOError):
sys.stderr.write("Error: Can't create save game: " + fname + "\n")
return
save_state = {}
save_state["Agents"] = {}
for map_name in self.agents:
if map_name == self.ALL_AGENTS_KEY:
continue
agents_dict = {}
for agent in self.agents[map_name]:
agent_obj = self.game_state.getObjectById(agent, map_name)
agent_inst = self.game_state.maps[map_name].\
agent_layer.getInstance(agent)
agent_dict = self.agents[map_name][agent]
agent_dict.update(agent_obj.getStateForSaving())
agent_dict["Rotation"] = agent_inst.getRotation()
agents_dict[agent] = agent_dict
save_state["Agents"][map_name] = agents_dict
agents_dict = {}
for agent in self.agents["All"]:
map_name = self.agents["All"][agent]["Map"]
agent_dict = self.agents["All"][agent]
agent_obj = None
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])