Exemple #1
0
    def generate(self):
        room = self.create_room()
        x = (self.grid.size_x - room.grid.size_x) / 2
        y = (self.grid.size_y - room.grid.size_y) / 2
        self.place_room(room, x, y)

        for i in xrange(self.grid.size_x * self.grid.size_y * 2):
            if len(self.rooms) == self.max_rooms:
                break

            room = self.create_room()
            x, y, dir = self.choose_gate()

            if dir == 'n':
                room_x = x - randint_triangular(1, room.grid.size_x - 2)
                room_y = y + 1
            elif dir == 's':
                room_x = x - randint_triangular(1, room.grid.size_x - 2)
                room_y = y - room.grid.size_y
            elif dir == 'e':
                room_x = x + 1
                room_y = y - randint_triangular(1, room.grid.size_y - 1)
            elif dir == 'w':
                room_x = x - room.grid.size_x
                room_y = y - randint_triangular(1, room.grid.size_y - 1)

            if self.has_space_for_room(room, room_x, room_y):
                self.place_room(room, room_x, room_y)
                self.connect_rooms(x, y, dir)
Exemple #2
0
    def choose_gate(self):
        room = random.choice(self.rooms)
        dir = random.choice('nsew')

        if dir == 'n':
            x = randint_triangular(room.x + 1, room.x + room.grid.size_x - 2)
            y = room.y + room.grid.size_y - 1
        elif dir == 's':
            x = randint_triangular(room.x + 1, room.x + room.grid.size_x - 2)
            y = room.y
        elif dir == 'e':
            x = room.x + room.grid.size_x - 1
            y = randint_triangular(room.y + 1, room.y + room.grid.size_y - 2)
        elif dir == 'w':
            x = room.x
            y = randint_triangular(room.y + 1, room.y + room.grid.size_y - 2)

        return x, y, dir