From 51e7618d495f875c8d7fa467398d702371371282 Mon Sep 17 00:00:00 2001 From: "FunnyMan3595 (Charlie Nolan)" Date: Sun, 4 Nov 2007 21:18:29 -0600 Subject: [PATCH] Docstrings for UIs. --- ui/ui.py | 8 +++++++- ui/zork_ui.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ui/ui.py b/ui/ui.py index be39f48..9394be8 100644 --- a/ui/ui.py +++ b/ui/ui.py @@ -2,6 +2,8 @@ # and licensed under the GNU GPL v3. For more details, see LICENSE in the main # Realism directory. +'''Holds the generic user interface, UI.''' + class NotImplementedException(Exception): pass class UI(object): @@ -15,7 +17,7 @@ class UI(object): raise NotImplementedException("This function is not implemented by " + self.class_name) def __init__(self): - '''Iself.nitializes the user interface.''' + '''Initializes the user interface.''' self.ni('__init__') def main_loop(self): @@ -53,3 +55,7 @@ class UI(object): def gained_level(self, character): '''Reports a level gain.''' self.ni('gained_level') + + def won_battle(self): + '''Returns the UI to the map after winning a battle.''' + self.ni('won_battle') diff --git a/ui/zork_ui.py b/ui/zork_ui.py index 6e743cb..756a3e9 100644 --- a/ui/zork_ui.py +++ b/ui/zork_ui.py @@ -2,6 +2,8 @@ # and licensed under the GNU GPL v3. For more details, see LICENSE in the main # Realism directory. +'''Holds the text-based UI, ZorkUI. This UI is pure text, and is therefore completely accessible to blind users.''' + from sys import stdin, stdout, exit from ui import UI from os import name as os_name @@ -20,22 +22,32 @@ except ImportError: # basic raw_input. pass +def copy_docstr(func): + '''Copies the docstring from the matching function on UI.''' + func_name = func.func_name + func.__doc__ = getattr(UI, func_name).__doc__ + class ZorkUI(UI): + '''The text-based UI. Accessible to blind users.''' class_name = "Zork UI" def __init__(self): pass + copy_docstr(__init__) def main_loop(self): input = 'look' while True: self.process_input(input) input = self.get_input() + copy_docstr(main_loop) def get_input(self): + '''Gets case-insensitive input from the user.''' return raw_input("> ").lower() def process_input(self, input): + '''Processes input from the user.''' if not input: return if self.mode == 'map': @@ -262,6 +274,7 @@ class ZorkUI(UI): exit() def i_attacked(self, character, event): + '''Prints messages for an attack by the (only) character.''' if event.target.hp <= 0: print "You killed %s!" % event.target.name if self.battle.over(): @@ -272,6 +285,7 @@ class ZorkUI(UI): print "You hit %s for %d damage." % (event.target.name, event.result) def was_attacked(self, events): + '''Prints messages for attacks from enemies.''' for event in events: print event.source.name.capitalize(), if event.result == "miss": @@ -286,36 +300,45 @@ class ZorkUI(UI): def hp_tick(self, character, amount): if amount > 0: print "Your health increases by %d, to a total of %d." % (amount, party.chars[character].hp) + copy_docstr(hp_tick) def mp_tick(self, character, amount): if amount > 0: print "You gain %d mana. You have %d mana." % (amount, party.chars[character].mp) + copy_docstr(mp_tick) def gained_ability_level(self, character, ability): print "Your %s improves!" % abilities[ability] + copy_docstr(gained_ability_level) def gained_level(self, character): print "You gain a level!" + copy_docstr(gained_level) def won_battle(self): self.map_mode() self.process_input("look") + copy_docstr(won_battle) def map_mode(self, game_map = None): self.mode = 'map' if game_map: self.game_map = game_map + copy_docstr(map_mode) def shop_mode(self, shop = None): self.mode = 'shop' if shop: self.shop = shop + copy_docstr(shop_mode) def battle_mode(self, battle = None): self.mode = 'battle' if battle: self.battle = battle print "You are attacked by %d critters!" % len(battle.party.chars) + copy_docstr(battle_mode) def conversation_mode(self, conversation = ""): print conversation + copy_docstr(conversation_mode) -- 2.11.4.GIT