Display dialog of levels with errors at startup, rather than crashing
authorNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 12:19:24 +0000 (14:19 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 12:19:24 +0000 (14:19 +0200)
erdslangetjie/__main__.py
erdslangetjie/level.py

index ebccae8129749946e9952c5eb2b6a7914f87223f..74a0c33a2509ac939988175ea4de3624d6a033f2 100644 (file)
@@ -5,6 +5,7 @@ from kivy.uix.widget import Widget
 from kivy.uix.relativelayout import RelativeLayout
 from kivy.uix.scrollview import ScrollView
 from kivy.uix.label import Label
+from kivy.uix.popup import Popup
 from kivy.graphics import Color, Rectangle
 from kivy.utils import platform
 from kivy.clock import Clock
@@ -305,8 +306,8 @@ class GameApp(App):
     title = "Peter's thread snake"
 
     def __init__(self):
-        self.levels = LevelList()
         super(GameApp, self).__init__()
+        self.levels = LevelList()
 
     def build(self):
         root = ScrollView(size_hint=(None, None))
@@ -318,6 +319,12 @@ class GameApp(App):
         if platform() == 'android':
             window.fullscreen = True
         self.root.size = window.size
+        errors = self.levels.get_errors()
+        if errors:
+            popup = Popup(title='Levels excluded',
+                    content=Label(text='\n'.join(errors)),
+                    size_hint=(.5, .5))
+            popup.open()
         self.make_intro()
 
     def make_intro(self):
index e4bc0e09a58072ea9c28dccdd2e4f8a790d9a6fb..2974b88b349adddc1bda0e41e072b0978f0d177b 100644 (file)
@@ -236,6 +236,7 @@ class LevelList(object):
 
     def __init__(self):
         self.levels = []
+        self.errors = []
         level_list = load(self.LEVELS)
         for line in level_list:
             line = line.strip()
@@ -245,12 +246,13 @@ class LevelList(object):
                 level_file.close()
                 try:
                     level.validate()
+                    self.levels.append(level)
                 except RuntimeError as err:
-                    raise RuntimeError(
+                    self.errors.append(
                             'Invalid level %s in level_list: %s' % (line, err))
-                self.levels.append(level)
             else:
-                raise RuntimeError('Level list includes non-existant level %s' % line)
+                self.errors.append(
+                    'Level list includes non-existant level %s' % line)
         level_list.close()
         self._cur_level = 0
 
@@ -260,6 +262,9 @@ class LevelList(object):
         else:
             return None
 
+    def get_errors(self):
+        return self.errors
+
     def advance_to_next_level(self):
         self._cur_level += 1
         return self.get_current_level()