Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/game_map.py
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@gmail.com>2011-11-29 02:22:49 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2011-11-29 02:22:49 (GMT)
commitf995cc648b6ef9172528c44ef444d51b66f29bcc (patch)
treecf94f6945b1acf714e22939a826f4e7163c2de0d /game_map.py
parentb10aff64a5f25818ccc13175da52a8231dcf699d (diff)
Add a class to implement a game map
The class have the data to a basic map (we can add others later) and basic methods to support navigation across the map. A simple test is provided in the same file. Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
Diffstat (limited to 'game_map.py')
-rw-r--r--game_map.py242
1 files changed, 242 insertions, 0 deletions
diff --git a/game_map.py b/game_map.py
new file mode 100644
index 0000000..42ed8b2
--- /dev/null
+++ b/game_map.py
@@ -0,0 +1,242 @@
+"""
+This is a data structure to define a game map
+At start we will use only one map, but later we wil add more maps
+"""
+
+"""
+Default Map
+
+The symbol # is a door.
+
+ 0 1 2 3
+ +-----+-----+-----+-----+
+ 0 | A |
+ | |
+ +--#--+--#--------+--#--+
+ 1 | | C # |
+ | # | |
+ + B +--------#--+ E +
+ 2 | | D # |
+ | # | |
+ +--#--+--#--------+--#--+
+ 3 | | G # |
+ | # | |
+ + F +--------#--+ I +
+ 4 | # H # |
+ | | | |
+ +--#--+--#--------+--#--+
+ 5 | J |
+ | |
+ +-----+-----+-----+-----+
+
+We represent this map with the following structure:
+"""
+
+
+class GameMap():
+
+ data = {'max_x': 4, 'max_y': 6,
+
+ 'rooms': {'A': {}, 'B': {}, 'C': {}, 'D': {},
+ 'E': {}, 'F': {}, 'G': {}, 'H': {},
+ 'I': {}, 'J': {}},
+
+ 'cells': ['AAAA',
+ 'BCCE',
+ 'BDDE',
+ 'FGGI',
+ 'FHHI',
+ 'JJJJ'],
+
+ # walls are defined by the cell position x,y and the direction
+ # N,S,E,W
+ 'walls': [{'position': [0, 0, 'S'], 'objects': ['door_1']},
+ {'position': [1, 0, 'S'], 'objects': ['door_2']},
+ {'position': [3, 0, 'S'], 'objects': ['door_3']},
+ {'position': [0, 1, 'E'], 'objects': ['door_4']},
+ {'position': [2, 1, 'S'], 'objects': ['door_5']},
+ {'position': [2, 1, 'E'], 'objects': ['door_6']},
+ {'position': [0, 2, 'S'], 'objects': ['door_7']},
+ {'position': [0, 2, 'E'], 'objects': ['door_8']},
+ {'position': [1, 2, 'S'], 'objects': ['door_9']},
+ {'position': [3, 2, 'E'], 'objects': ['door_10']},
+ {'position': [4, 2, 'S'], 'objects': ['door_11']},
+ {'position': [0, 3, 'E'], 'objects': ['door_12']},
+ {'position': [3, 3, 'E'], 'objects': ['door_13']},
+ {'position': [0, 4, 'S'], 'objects': ['door_14']},
+ {'position': [0, 4, 'E'], 'objects': ['door_15']},
+ {'position': [1, 4, 'S'], 'objects': ['door_16']},
+ {'position': [2, 4, 'E'], 'objects': ['door_17']},
+ {'position': [3, 4, 'S'], 'objects': ['door_18']}]}
+
+ def get_room(self, x, y):
+ """ Return room key and the dictionary based in
+ the position x,y in the map"""
+ room_key = self.data['cells'][y][x]
+ return room_key
+
+ def get_next_coords(self, x, y, direction):
+ if direction == 'N':
+ y -= 1
+ if direction == 'S':
+ y += 1
+ if direction == 'W':
+ x -= 1
+ if direction == 'E':
+ x += 1
+ if x < 0 or y < 0 or \
+ x > (self.data['max_x'] - 1) or \
+ y > (self.data['max_y'] - 1):
+ return -1, -1
+ return x, y
+
+ def get_next_room(self, x, y, direction):
+ x, y = self.get_next_coords(x, y, direction)
+ if x == -1 and y == -1:
+ return None
+ return self.get_room(x, y)
+
+ def get_reversed_direction(self, direction):
+ if direction == 'N':
+ return 'S'
+ if direction == 'S':
+ return 'N'
+ if direction == 'E':
+ return 'W'
+ if direction == 'W':
+ return 'E'
+
+ def get_direction_cw(self, direction):
+ """Return the direction if the user turn clock wise"""
+ if direction == 'N':
+ return 'E'
+ if direction == 'S':
+ return 'W'
+ if direction == 'E':
+ return 'S'
+ if direction == 'W':
+ return 'N'
+
+ def get_direction_ccw(self, direction):
+ """Return the direction if the user turn reverse clock wise"""
+ if direction == 'N':
+ return 'W'
+ if direction == 'S':
+ return 'E'
+ if direction == 'E':
+ return 'N'
+ if direction == 'W':
+ return 'S'
+
+ def get_wall_info(self, x, y, direction):
+ """ Return the array of objects associated to a defined wall
+ or None if there are not a wall in this cell and direction"""
+
+ # verify if there are a wall in the requested position
+ actual_room = self.get_room(x, y)
+ next_room = self.get_next_room(x, y, direction)
+ # if the two rooms are the same, there are no wall
+ if next_room is not None and actual_room == next_room:
+ return None
+ # Search in walls data
+ for wall in self.data['walls']:
+ if wall['position'] == [x, y, direction]:
+ return wall['objects']
+ # look for information in the other side of the room too.
+ # (only valid for doors)
+ if next_room is not None:
+ reversed_direction = self.get_reversed_direction(direction)
+ x2, y2 = self.get_next_coords(x, y, direction)
+ if x2 == -1 and y2 == -1:
+ return []
+ for wall in self.data['walls']:
+ if wall['position'] == [x2, y2, reversed_direction]:
+ return wall['objects']
+ # Nothing found
+ return []
+
+ def go_right(self, x, y, direction):
+ """ Return next position if the user go to the right"""
+ # check if there are a wall
+ direction_cw = self.get_direction_cw(direction)
+ wall_right = self.get_wall_info(x, y, direction_cw)
+ if wall_right is not None:
+ return x, y, direction_cw
+ if direction == 'N':
+ return x + 1, y, direction
+ if direction == 'E':
+ return x, y + 1, direction
+ if direction == 'S':
+ return x - 1, y, direction
+ if direction == 'W':
+ return x, y - 1, direction
+
+ def go_left(self, x, y, direction):
+ """ Return next position if the user go to the left"""
+ # check if there are a wall
+ direction_ccw = self.get_direction_ccw(direction)
+ wall_left = self.get_wall_info(x, y, direction_ccw)
+ if wall_left is not None:
+ return x, y, direction_ccw
+ if direction == 'N':
+ return x - 1, y, direction
+ if direction == 'E':
+ return x, y - 1, direction
+ if direction == 'S':
+ return x + 1, y, direction
+ if direction == 'W':
+ return x, y + 1, direction
+
+ def cross_door(self, x, y, direction, door):
+ """ Return next position if the user go to the left"""
+ # verify is the door is in the right position/direction
+ objects = self.get_wall_info(x, y, direction)
+ if objects is None:
+ return x, y, direction
+ else:
+ for door_object in objects:
+ if door_object == door:
+ if direction == 'N':
+ return x, y - 1, direction
+ if direction == 'E':
+ return x + 1, y, direction
+ if direction == 'S':
+ return x, y + 1, direction
+ if direction == 'W':
+ return x - 1, y, direction
+ # do not found the door
+ return x, y, direction
+
+
+# testing
+if __name__ == '__main__':
+
+ game_map = GameMap()
+ print "test get_room"
+ print game_map.get_room(0, 0)
+ print game_map.get_room(3, 2)
+ print game_map.get_room(1, 4)
+ print game_map.get_room(3, 5)
+
+ print "test get_next_room"
+ print game_map.get_next_room(3, 5, 'N')
+ print game_map.get_next_room(3, 3, 'W')
+ print game_map.get_next_room(2, 1, 'S')
+
+ print "test get_wall_info"
+ print game_map.get_wall_info(0, 3, 'N')
+ print game_map.get_wall_info(0, 3, 'E')
+ print game_map.get_wall_info(0, 3, 'S')
+ print game_map.get_wall_info(0, 3, 'W')
+
+ print "test go_right"
+ print game_map.go_right(1, 1, 'N')
+ print game_map.go_right(2, 1, 'N')
+
+ print "test go_left"
+ print game_map.go_left(1, 1, 'N')
+ print game_map.go_left(2, 1, 'N')
+
+ print "test cross_door"
+ print game_map.cross_door(1, 1, 'N', 'door_2')
+ print game_map.cross_door(2, 1, 'N', 'door_2')