本文整理汇总了Python中console.Console.cleanup方法的典型用法代码示例。如果您正苦于以下问题:Python Console.cleanup方法的具体用法?Python Console.cleanup怎么用?Python Console.cleanup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类console.Console
的用法示例。
在下文中一共展示了Console.cleanup方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import cleanup [as 别名]
#.........这里部分代码省略.........
self._input = self._output
self._player = Player(self.let_player_act, self.render)
self._message_buffer = []
self._closed = False
def has_saved_game(self):
return False
def load_game(self):
raise Exception("no")
def new_game(self):
self._player.xy(int(self._game_config.world_width / 2), int(self._game_config.world_height / 2))
maker = MapMaker()
self._worldmap = maker.generate(self._game_config.world_width, self._game_config.world_height)
self._worldmap.set_message(self.add_message)
self._worldmap.insert_objects(self._player)
self._tick = 0
def run(self):
while not self._closed:
self.render()
self.tick()
def render(self):
self._worldmap.restack()
map_view = self._worldmap.get_view(
self._player.x, self._player.y, self._output.get_view_width(), self._output.get_view_height()
)
display_data = AttrDict(
{
"view": "main",
"tick": self._tick,
"xy": "{}, {}".format(self._player.x, self._player.y),
"player_health": "{}/{}".format(self._player.max_health, self._player.health),
"player_energy": self._player._energy,
"player_inventory": [i.name for i in self._player._inventory],
"map_view": map_view,
"messages": self._message_buffer[-8:],
}
)
self._output.render(display_data)
def let_player_act(self):
key = self._input.get_key()
self.handle_key(key)
def shutdown(self):
self._console.teardown()
def tick(self):
self._worldmap.tick()
self._tick += 1
def handle_key(self, key):
if key in ("q", "Q"):
self._closed = True
self._player._energy = -1
return True
elif key in self._config.keybindings:
return self.act_on_bound_key(key)
else:
self.add_message("Unbound key '{}'".format(key))
return False
def act_on_bound_key(self, key):
binding = self._config.keybindings[key]
if binding in (
Bind.MOVE_N,
Bind.MOVE_NE,
Bind.MOVE_E,
Bind.MOVE_SE,
Bind.MOVE_S,
Bind.MOVE_SW,
Bind.MOVE_W,
Bind.MOVE_NW,
):
return self.object_act_direction(self._player, AttrDict({"x": binding[0], "y": binding[1]}))
elif binding == Bind.PICKUP:
return self._worldmap.grab_object(self._player)
elif binding == Bind.WAIT:
self._player._energy -= self._player._speed # wait 1 tick
def object_act_direction(self, obj, movement):
if self._worldmap.can_move_object(obj, movement):
obj._energy -= self._worldmap.move_object(obj, movement)
else:
obj._energy -= self._worldmap.object_act_in_direction(obj, movement)
def add_message(self, message):
if isinstance(message, list):
self._message_buffer.extend(message)
else:
self._message_buffer.append(message)
def cleanup(self):
self._output.cleanup()