- commit
- 41f992cb1f0982c1ebf0d17c3c3c58db7ae1a2f7
- parent
- 5d60676828fd6760ea15bd6bf959235ba4e91b7f
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-11-01 10:41
simple floor_layer generation
Diffstat
| M | laneya/map.py | 71 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 files changed, 71 insertions, 0 deletions
diff --git a/laneya/map.py b/laneya/map.py
@@ -25,8 +25,79 @@ class Map(object): 25 25 [None for i in xrange(height)] for i in xrange(width)] 26 26 self.floor_layer = [ 27 27 [None for i in xrange(height)] for i in xrange(width)] -1 28 self.generate() 28 29 self.ghost = Ghost('example', self, 15, 15) 29 30 -1 31 def generate(self): -1 32 rooms = [] -1 33 -1 34 # make sure user and ghost are inside of a room -1 35 rooms.append({ -1 36 'x_min': 5, -1 37 'x_max': 20, -1 38 'y_min': 5, -1 39 'y_max': 20, -1 40 }) -1 41 -1 42 for i in range(2000): -1 43 x1 = random.randint(1, self.width - 2) -1 44 x2 = random.randint(1, self.width - 2) -1 45 y1 = random.randint(1, self.height - 2) -1 46 y2 = random.randint(1, self.height - 2) -1 47 -1 48 room = { -1 49 'x_min': min(x1, x2), -1 50 'x_max': max(x1, x2), -1 51 'y_min': min(y1, y2), -1 52 'y_max': max(y1, y2), -1 53 } -1 54 -1 55 collision_free = lambda other: ( -1 56 room['x_min'] > other['x_max'] + 1 or -1 57 room['x_max'] < other['x_min'] - 1 or -1 58 room['y_min'] > other['y_max'] + 1 or -1 59 room['y_max'] < other['y_min'] - 1) -1 60 -1 61 if (room['x_max'] - room['x_min'] > 2 and -1 62 room['y_max'] - room['y_min'] > 2): -1 63 if all((collision_free(other) for other in rooms)): -1 64 rooms.append(room) -1 65 -1 66 in_room = lambda x, y, room: ( -1 67 x >= room['x_min'] and -1 68 x <= room['x_max'] and -1 69 y >= room['y_min'] and -1 70 y <= room['y_max']) -1 71 -1 72 # carve rooms -1 73 for x in range(self.width): -1 74 for y in range(self.height): -1 75 if any((in_room(x, y, room) for room in rooms)): -1 76 self.floor_layer[x][y] = 'floor' -1 77 else: -1 78 self.floor_layer[x][y] = 'wall' -1 79 -1 80 # carve paths -1 81 for i, room in enumerate(rooms): -1 82 if i != 0: -1 83 last = rooms[i - 1] -1 84 -1 85 x_center = (room['x_max'] + room['x_min']) / 2 -1 86 y_center = (room['y_max'] + room['y_min']) / 2 -1 87 last_x_center = (last['x_max'] + last['x_min']) / 2 -1 88 last_y_center = (last['y_max'] + last['y_min']) / 2 -1 89 -1 90 x_min = min(x_center, last_x_center) -1 91 x_max = max(x_center, last_x_center) + 1 -1 92 y_min = min(y_center, last_y_center) -1 93 y_max = max(y_center, last_y_center) + 1 -1 94 -1 95 for x in range(x_min, x_max): -1 96 self.floor_layer[x][last_y_center] = 'floor' -1 97 -1 98 for y in range(y_min, y_max): -1 99 self.floor_layer[x_center][y] = 'floor' -1 100 30 101 def step(self): 31 102 """Update this map and all of its sprites. 32 103