Commits

Radomir Dopieralski committed 7feac4a

Refactor the menu a little

Comments (0)

Files changed (4)

                 self.display.draw()
                 self.clock.tick(15)
                 choice = menu.update()
-        except util.QuitLevel:
+        except (util.QuitLevel, util.CancelMenu):
             raise util.QuitGame()
-        if choice == 1 or choice is False:
+        if choice == 1:
             raise util.QuitGame()
 
     def start(self):
                 self.display.update()
                 self.display.draw()
                 choice = menu.update()
-        except util.QuitLevel:
+        except (util.QuitLevel, util.CancelMenu):
             return
         if choice == 1:
             raise util.QuitLevel()
 class Text(pygame.sprite.Sprite):
     font = None
 
-    def __init__(self, text):
+    def __init__(self, text, color=(250, 250, 250)):
         super(Text, self).__init__()
         if self.font is None:
             self.font = resource.load_font('pf_ronda_seven_bold.ttf')
-        self.image = self.outlined_text(text, (250, 250, 250), (0, 0, 0))
+        self.image = self.outlined_text(text, color, (0, 0, 0))
         self.rect = self.image.get_rect()
         self.depth = 1001
 
                 return
 
     def update(self):
+        item = None
         for event in pygame.event.get():
-            if event.type == pg.KEYDOWN and event.key == pg.K_UP:
-                self.move(-1)
-            elif event.type == pg.KEYDOWN and event.key == pg.K_DOWN:
-                self.move(1)
-            elif event.type == pg.KEYDOWN and event.key == pg.K_z:
-                item = self.cursor.item
-                self.cleanup()
-                return item
-            elif event.type == pg.KEYDOWN and event.key == pg.K_x:
-                self.cleanup()
-                return False
+            if event.type == pg.KEYDOWN:
+                if event.key == pg.K_UP:
+                    self.move(-1)
+                elif event.key == pg.K_DOWN:
+                    self.move(1)
+                elif event.key == pg.K_z:
+                    item = self.cursor.item
+                    self.cleanup()
+                elif event.key == pg.K_x:
+                    self.cleanup()
+                    raise util.CancelMenu()
+                elif event.key == pg.K_ESCAPE:
+                    self.cleanup()
+                    raise util.QuitLevel()
             elif event.type == pg.QUIT:
                 self.cleanup(False)
                 raise util.QuitGame()
-            elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
-                self.cleanup()
-                raise util.QuitLevel()
-        return None
+        return item
 
 class Health(pygame.sprite.Sprite):
     tiles = None
 from jelly import message
 from jelly import items
 from jelly import behavior
-from jelly.util import DX, DY, dist, pos2dir
+from jelly.util import DX, DY, dist, pos2dir, CancelMenu
 
 
 KEY_DIRS = {
         display.render_menu(menu)
         choice = None
         clock = pygame.time.Clock()
-        while choice is None:
-            clock.tick(15)
-            display.update()
-            display.draw()
-            choice = menu.update()
-        if choice is False or not item_list:
+        try:
+            while choice is None:
+                clock.tick(15)
+                display.update()
+                display.draw()
+                choice = menu.update()
+        except CancelMenu:
             return None
-        else:
-            return item_list[choice]
+        if not item_list:
+            return None
+        return item_list[choice]
 
     def control(self, level, display):
         direction = None
 
 class QuitGame(Exception):
     pass
+
+
+class CancelMenu(Exception):
+    pass