Nemesis appears on the map. Refactor code accordingly
authorNeil <neil@dip.sun.ac.za>
Tue, 16 Apr 2013 10:34:58 +0000 (12:34 +0200)
committerNeil <neil@dip.sun.ac.za>
Tue, 16 Apr 2013 10:34:58 +0000 (12:34 +0200)
erdslangetjie/__main__.py
erdslangetjie/player.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):
index 2f8def2976ae4bf0c7e1dabb6d295c2702108996..5bb6472b1b88fca7b942d28a60e97e8eb35690ff 100644 (file)
@@ -3,33 +3,59 @@
 from erdslangetjie.data import load_image
 
 
-class PlayerSprite(object):
+class GameSprite(object):
 
     def __init__(self):
         self.pos = (0, 0)
         self.sprite = None
 
-    def move(self, direction):
-        self.pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
+    def get_image(self):
+        return self.sprite
+
+    def get_texture(self):
+        return self.sprite.texture
+
+
+class FigureSprite(GameSprite):
 
     def can_move(self, direction, level):
         '''Check if we can move in the given direction'''
         cand_pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
         return not level.blocked(cand_pos)
 
-    def get_image(self):
-        return self.sprite
-
 
-class ThePlayer(PlayerSprite):
+class ThePlayer(FigureSprite):
 
     def __init__(self):
         super(ThePlayer, self).__init__()
         self.sprite = load_image('sprites/player.png')
 
+    def move(self, direction, level):
+        if self.can_move(direction, level):
+            self.pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
+            return True
+        return False
 
-class Nemesis(PlayerSprite):
+
+class Nemesis(FigureSprite):
 
     def __init__(self):
-        super(ThePlayer, self).__init__()
+        super(Nemesis, self).__init__()
         self.sprite = load_image('sprites/nemesis.png')
+        self.reset_pos()
+
+    def move(self, level):
+        if not self.on_board():
+            # Advance towards the map a step at a time
+            self.pos = (self.pos[0] + 1, self.pos[1])
+            if self.on_board():
+                self.pos = level.enter_pos
+            return False
+        # AI goes here
+        return True
+
+    def reset_pos(self):
+        self.pos = (-10, 0)
+
+    def on_board(self):
+        return self.pos[0] >= 0