Example #1
0
    def generate_map(self):
        for count in range(C.DUNGEON_MAX_ROOMS):
            num_rooms = len(self.rooms)
            # 1/5 of rooms as circles
            if libtcod.random_get_int(0, 0, 5) == 4:
                r = libtcod.random_get_int(0, C.DUNGEON_ROOM_MIN_SIZE, C.DUNGEON_ROOM_MAX_SIZE / 2)
                x = libtcod.random_get_int(0, r, self.width - r - 2)
                y = libtcod.random_get_int(0, r, self.height - r - 2)
                new_room = Circle(x, y, r)
            else:
                w = libtcod.random_get_int(0, C.DUNGEON_ROOM_MIN_SIZE, C.DUNGEON_ROOM_MAX_SIZE)
                h = libtcod.random_get_int(0, C.DUNGEON_ROOM_MIN_SIZE, C.DUNGEON_ROOM_MAX_SIZE)
                x = libtcod.random_get_int(0, 1, self.width - w - 2)
                y = libtcod.random_get_int(0, 1, self.height - h - 2)
                new_room = Rect(x, y, w, h)

            # run through the other rooms and see if they intersect with this one
            failed = False
            for other_room in self.rooms:
                if new_room.intersect(other_room):
                    failed = True
                    break

            if not failed:
                # this means there are no intersections, so this room is valid
                # "paint" it to the map's tiles
                self.create_room(new_room)

                # center coordinates of new room, will be useful later
                (new_x, new_y) = new_room.center()

                # Display Room names for debugging
                if C.DEBUG:
                    debug_sprite_config = dict(
                        char=chr(65+num_rooms),
                        color=libtcod.white,
                        description="A debugging character."
                    )
                    room_no = Sprite(game.console, debug_sprite_config, (new_x, new_y), False)
                    game.sprites.insert(0, room_no)

                if num_rooms == 0:
                    # this is the first room, where the player starts at
                    self.starting_coords['x'] = new_x
                    self.starting_coords['y'] = new_y
                else:
                    # all rooms after the first:
                    # connect it to the previous room with a tunnel

                    # center coordinates of previous room
                    (prev_x, prev_y) = self.rooms[num_rooms-1].center()

                    # draw a coin (random number that is either 0 or 1)
                    if libtcod.random_get_int(0, 0, 1) == 1:
                        # first move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # first move vertically, then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # finally, append the new room to the list
                self.rooms.append(new_room)
                num_rooms += 1
Example #2
0
 def test_nonintersecting_circle_and_rectangle(self):
     """Do non-intersecting circles and rectangles intersect?"""
     circle1 = Circle(4, 4, 2)
     rect1 = Rect(20, 20, 4, 4)
     self.assertFalse(circle1.intersect(rect1) and rect1.intersect(circle1))
Example #3
0
 def test_nonintersecting_circles(self):
     """Do non-intersecting circles intersect?"""
     circle1 = Circle(4, 4, 2)
     circle2 = Circle(14, 14, 2)
     self.assertFalse(circle1.intersect(circle2) and circle2.intersect(circle1))
Example #4
0
 def test_intersecting_circle_and_rectangle(self):
     """Do intersecting circles and rectangles intersect?"""
     circle1 = Circle(4, 4, 2)
     rect1 = Rect(0, 0, 4, 4)
     self.assertTrue(circle1.intersect(rect1) and rect1.intersect(circle1))
Example #5
0
 def test_intersecting_circles(self):
     """Do intersecting circles intersect?"""
     circle1 = Circle(4, 4, 2)
     circle2 = Circle(5, 5, 2)
     self.assertTrue(circle1.intersect(circle2) and circle2.intersect(circle1))