Tweak level object
authorNeil <neil@dip.sun.ac.za>
Mon, 15 Apr 2013 10:56:14 +0000 (12:56 +0200)
committerNeil <neil@dip.sun.ac.za>
Mon, 15 Apr 2013 10:56:14 +0000 (12:56 +0200)
erdslangetjie/level.py

index a2c2a2dadc1b8044676574d8c60d9134e84b6b5a..3214cf665fb7ec3f7e718015de74cbf887297f96 100644 (file)
@@ -7,6 +7,9 @@ class Level(object):
 
     def __init__(self):
         self.data = []
+        self.exit_pos = []
+        self.enter_pos = None
+        self.tiles = []
 
     def load(self, levelfile):
         """Load the level"""
@@ -14,9 +17,11 @@ class Level(object):
         for line in levelfile.readlines():
             self.data.append(list(line))
 
-    def get_tiles(self):
-        """Return a list of tiles for the level"""
-        tiles = []
+    def load_tiles(self):
+        """Load the list of tiles for the level"""
+        self.tiles = []
+        self.exit_pos = []
+        self.enter_pos = None
         for j, line in enumerate(self.data):
             tile_line = []
             for i, c in enumerate(line):
@@ -25,6 +30,17 @@ class Level(object):
                 elif c == '.':
                     tile_line.append(load_image('tiles/wall.bmp'))
                 elif c == 'E' or c == 'X':
+                    if c == 'E':
+                        if self.enter_pos:
+                            raise RuntimeError('Multiple entry points')
+                        self.enter_pos = (i, j)
+                    else:
+                        self.exit_pos.append((i, j))
                     tile_line.append(load_image('tiles/door.bmp'))
-            tiles.append(tile_line)
-        return tiles
+            self.tiles.append(tile_line)
+
+    def get_tiles(self):
+        return self.tiles
+
+    def at_exit(self, pos):
+        return pos in self.exit_pos