Make 'quit' an ordinary command.
authorNeil Moore <neil@s-z.org>
Sat, 31 May 2008 21:49:21 +0000 (17:49 -0400)
committerNeil Moore <neil@s-z.org>
Sat, 31 May 2008 21:49:21 +0000 (17:49 -0400)
New exception class PlayerQuit, thrown when the player quits the game.

Catch PlayerQuit and return in the event loop.

roguelike.py

index bb72a46..a6bd54b 100755 (executable)
@@ -31,6 +31,8 @@ import ui
 
 do_profile = False
 
+class PlayerQuit (Exception):
+    pass
 
 class AppUI:
     instance = None
@@ -116,6 +118,7 @@ class AppUI:
                 'switch':       lambda: self.switch() and False,
                 'wield':        self.try_wield,
                 'unwield':      self.try_unwield,
+                'quit':         self.quit,
                 }
         self.keymap = {
                 curses.KEY_UP:    'north',
@@ -135,6 +138,7 @@ class AppUI:
                 ord('@'):         'switch',
                 ord('w'):         'wield',
                 ord('W'):         'unwield',
+                ord('Q'):         'quit',
                 }
 
     def message(self, msg=None):
@@ -205,7 +209,8 @@ class AppUI:
         self.invpanel.hide()
         curses.panel.update_panels()
         return rv
-
+    def quit(self):
+        raise PlayerQuit()
     def try_drop(self):
         it = self.draw_inventory()
         if not it:
@@ -336,10 +341,13 @@ class AppUI:
             self.message()
 
             if self.mapped(c):
-                if not self.perform(c):
-                    continue
-            elif c == ord('Q'):
-                break
+                try:
+                    if not self.perform(c):
+                        continue
+                except PlayerQuit:
+                    return
+            else:
+                continue
 
             self.level().affect_all()