Cleanup more state - hopefully fix some stale game running bugs
authorNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 21:48:41 +0000 (23:48 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 21:48:41 +0000 (23:48 +0200)
erdslangetjie/__main__.py

index b4c25c2814b7e60d9e02470dbd0d1b4e19dead0a..935d212b98ccb6b246859c91b24efee773d63122 100644 (file)
@@ -123,20 +123,15 @@ class GameWindow(RelativeLayout):
                 if true_point[0] >= self.view.size[0]:
                     self.view.scroll_x += self.x_scroll_margin
                     true_point = self.to_parent(*check_point)
-                    #print '-x', self.view.scroll_x, self.view.scroll_y
                 elif true_point[0] < 0:
                     self.view.scroll_x -= self.x_scroll_margin
                     true_point = self.to_parent(*check_point)
-                    #print '+x', self.view.scroll_x, self.view.scroll_y
                 elif true_point[1] >= self.view.size[1]:
                     self.view.scroll_y += self.y_scroll_margin
                     true_point = self.to_parent(*check_point)
-                    #print '+y', self.view.scroll_x, self.view.scroll_y
                 elif true_point[1] < 0:
                     self.view.scroll_y -= self.y_scroll_margin
                     true_point = self.to_parent(*check_point)
-                    #print '-y', self.view.scroll_x, self.view.scroll_y
-                #print true_point, self.view.size
 
     def included(self, point, margin):
         if point[0] < margin:
@@ -192,11 +187,17 @@ class GameWindow(RelativeLayout):
 
     def do_nemesis_move(self):
         self.nemesis.move(self.level_obj, self.player.pos)
-        self.move_counter += 1
         self.check_state()
-        self.reset_timer()
+        if self.move_counter > 4:
+            self.move_counter = 0
+            self.draw_nemesis()
+            self.nemesis.move(self.level_obj, self.player.pos)
+            self.check_state()
+        else:
+            self.move_counter += 1
         self.draw_nemesis()
         self.draw_player()
+        self.reset_timer()
 
     def timed_move(self, event):
         if not self.level_obj:
@@ -206,12 +207,7 @@ class GameWindow(RelativeLayout):
     def reset_timer(self):
         self.timer_set = True
         Clock.unschedule(self.timed_move)
-        if self.move_counter > 4:
-            self.move_counter = 0
-            # The superhero is faster than the player
-            Clock.schedule_once(self.timed_move, 0.02)
-        else:
-            Clock.schedule_once(self.timed_move, 1)
+        Clock.schedule_once(self.timed_move, 3)
 
     def check_caught(self):
         return self.nemesis.pos == self.player.pos
@@ -245,13 +241,18 @@ class GameWindow(RelativeLayout):
             # Jump to next level
             self.level_obj = self.level_list.advance_to_next_level()
             if not self.load_level():
-                self.app.game_over(True)
+                app = self.app
+                self.app = None
+                self._closed()
+                app.game_over(True)
             return
         elif self.check_caught():
             # Caught
             self.reset_level()
-            self.load_level()
-            self.app.game_over(False)
+            app = self.app
+            self.app = None
+            self._closed()
+            app.game_over(False)
             return
         elif self.level_obj.is_button(self.player.pos):
             self.level_obj.trigger_button(self.player.pos)
@@ -340,6 +341,7 @@ class GameApp(App):
     def __init__(self):
         super(GameApp, self).__init__()
         self.levels = LevelList()
+        self.game = None
 
     def build(self):
         root = ScrollView(size_hint=(None, None))
@@ -369,15 +371,15 @@ class GameApp(App):
 
     def start_game(self, label, ref):
         """Start the game"""
-        game = GameWindow(self.levels, self)
-        game.build()
+        self.game = GameWindow(self.levels, self)
+        self.game.build()
         self.root.clear_widgets()
-        self.root.add_widget(game)
+        self.root.add_widget(self.game)
         # Ensure the player is visible
         self.root.scroll_x = 0
         self.root.scroll_y = 0
-        game.draw_player()
-        game.draw_nemesis()
+        self.game.draw_player()
+        self.game.draw_nemesis()
 
     def game_over(self, won):
         if won:
@@ -385,6 +387,8 @@ class GameApp(App):
             self.levels.reset()
         else:
             screen = LostScreen(self)
+        del self.game
+        self.game = None
         self.root.clear_widgets()
         self.root.add_widget(screen)