Nemesis appears on the map. Refactor code accordingly
[erdslangetjie.git] / erdslangetjie / __main__.py
index 7329b443567da11d149f4fe66c6893da3cf9bf86..0948e7401cea4df7e647fcf710fc1755c6487a38 100644 (file)
@@ -26,9 +26,11 @@ class GameWindow(FloatLayout):
         self.tiles = {}
 
         self.player = ThePlayer()
+        self.nemesis = Nemesis()
         if not self.level_obj.enter_pos:
             raise RuntimeError('No entry point')
         self.player_tile = None
+        self.nemesis_tile = None
 
         self.player.pos = self.level_obj.enter_pos
         self.keyboard = Window.request_keyboard(self._closed, self)
@@ -53,20 +55,36 @@ class GameWindow(FloatLayout):
             by += TILE_SIZE
 
         self.draw_player()
+        self.draw_nemesis()
 
     def draw_player(self):
         if self.player_tile:
             self.remove_widget(self.player_tile)
-        player_pos = (self.player.pos[0] * TILE_SIZE,
+        sprite_pos = (self.player.pos[0] * TILE_SIZE,
                 self.player.pos[1] * TILE_SIZE)
         self.player_tile = Widget(size=(TILE_SIZE, TILE_SIZE),
-                pos=player_pos)
+                pos=sprite_pos)
         with self.player_tile.canvas:
             Color(1, 1, 1)
-            Rectangle(pos=player_pos, size=self.player_tile.size,
-                    texture=self.player.sprite.texture)
+            Rectangle(pos=sprite_pos, size=self.player_tile.size,
+                    texture=self.player.get_texture())
         self.add_widget(self.player_tile)
 
+    def draw_nemesis(self):
+        if not self.nemesis.on_board():
+            return
+        if self.nemesis_tile:
+            self.remove_widget(self.nemesis_tile)
+        sprite_pos = (self.nemesis.pos[0] * TILE_SIZE,
+                self.nemesis.pos[1] * TILE_SIZE)
+        self.nemesis_tile = Widget(size=(TILE_SIZE, TILE_SIZE),
+                pos=sprite_pos)
+        with self.nemesis_tile.canvas:
+            Color(1, 1, 1)
+            Rectangle(pos=sprite_pos, size=self.nemesis_tile.size,
+                    texture=self.nemesis.get_texture())
+        self.add_widget(self.nemesis_tile)
+
     def _closed(self):
         self.keyboard.unbind(on_key_down=self._on_key_down)
 
@@ -82,19 +100,29 @@ class GameWindow(FloatLayout):
         elif keycode[0] == pygame.K_RIGHT:
             direction = (1, 0)
         if direction:
-            if self.player.can_move(direction, self.level_obj):
-                self.player.move(direction)
-                self.draw_player()
-                if self.level_obj.at_exit(self.player.pos):
-                    self.level_obj = self.level_list.advance_to_next_level()
-                    if self.level_obj:
-                        self.level_obj.load_tiles()
-                        self.player.pos = self.level_obj.enter_pos
-                        self.remove_widget(self.player_tile)
-                        self.build()
-                    else:
-                        print 'You won!'
-                        sys.exit(1)
+            self.nemesis.move(self.level_obj)
+            self.draw_nemesis()
+            self.player.move(direction, self.level_obj)
+            self.draw_player()
+        self.check_state()
+
+    def check_state(self):
+        if self.level_obj.at_exit(self.player.pos):
+            # Jump to next level
+            self.level_obj = self.level_list.advance_to_next_level()
+            self.nemesis.reset_pos()
+            if self.level_obj:
+                self.level_obj.load_tiles()
+                self.player.pos = self.level_obj.enter_pos
+                self.remove_widget(self.player_tile)
+                self.build()
+            else:
+                print 'You won!'
+                sys.exit(1)
+        elif self.nemesis.pos == self.player.pos:
+            # Caught
+            print 'You lost!'
+            sys.exit(1)
 
 
 class GameApp(App):