def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): # random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: self.create_room(new_room) (new_x, new_y) = new_room.center() if num_rooms == 0: player.x = new_x player.y = new_y else: (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities, max_monsters_per_room) rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): # random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # "Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) # run through the other rooms and see if they intersect with this one for other_room in rooms: if new_room.intersect(other_room): break else: # 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() if num_rooms == 0: # this is the first room, where the player starts at player.x = new_x player.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) = rooms[num_rooms - 1].center() # flip a coin (random number that is either 0 or 1) if randint(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) self.place_entities(new_room, entities, max_monsters_per_room, max_items_per_room) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, max_preset_room, room_min_size, room_max_size, map_width, map_height, player): rooms = [] num_rooms = 0 preset_room = 0 current_room_template = [[1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 1, 0, 1, 1, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1]] for c in current_room_template: for r in c: if max_preset_room >= preset_room: if c == 1: self.tiles[c][r].blocked = False self.tiles[c][r].block_sight = False elif c == 0: self.tiles[c][r].blocked = True self.tiles[c][r].block_sight = True for r in range(max_rooms): '''random width and height''' w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) ''' random position without going outside map''' x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) ''' Rect class makes rects easier to work with''' new_room = Rect(x, y, w, h) ''' run thru other rooms to checks for intersects''' for other_room in rooms: if new_room.intersect(other_room): break else: self.create_room(new_room) # paint the room (new_x, new_y) = new_room.center() if num_rooms == 0: player.x = new_x player.y = new_y else: """ all rooms after the first: connect it to the previous room with a tunnel """ (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player): rooms = [] num_rooms = 0 for r in range(max_rooms): width = randint(room_min_size, room_max_size) height = randint(room_min_size, room_max_size) x = randint(0, map_width - width - 1) y = randint(0, map_height - height - 1) new_room = Rect(x, y, width, height) for other_room in rooms: if new_room.intersect(other_room): break else: self.create_room(new_room) (new_x, new_y) = new_room.center() if num_rooms == 0: player.x = new_x player.y = new_y else: (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) rooms.append(new_room) num_rooms += 1
def place_trees(self): density = self.owner.width * self.owner.height // ( self.average_tree_diameter * 2) self.trees = [] for _ in range(density): new_tree_entity = self.flora.get_tree() tree = new_tree_entity.tree # new tree diameter # new tree position x = randint(0, self.owner.width - tree.diameter - 1) y = randint(0, self.owner.height - tree.diameter - 1) square_trunk = Rect(x, y, tree.diameter, tree.diameter) if self.landmark and self.landmark.rect.intersect_with_additional_space( square_trunk): continue for tree_entity in self.trees: if square_trunk.intersect(tree_entity.tree.trunk): break else: tree.set_trunk(square_trunk) self.draw_tree(tree) self.trees.append(new_tree_entity)
def make_map(self): #create two rooms for demonstration purposes room1 = Rect(20, 15, 10, 15) room2 = Rect(35, 15, 10, 15) self.create_room(room1) self.create_room(room2)
def make_home(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities): # random width and height w = map_width - 4 h = map_height - 4 # random position without going out of the boundaries of the map x = 2 y = 2 # "Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) self.create_room(new_room) # center coordinates of new room, will be useful later (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y player.x = new_x player.y = new_y self.place_entities(new_room, entities.entity) stairs_component = Stairs(self.dungeon_level + 1) down_stairs = Entity(center_of_last_room_x, (map_height - 4), '>', libtcod.white, 'Stairs', render_order=RenderOrder.STAIRS, stairs=stairs_component) entities.add_new_entity(down_stairs)
def make_map_next_room(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player): # Generate dungeon genNewRoom = False while (not genNewRoom): if self.num_attempts < max_rooms: # random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # "Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) # Run through the other rooms and see if they intersect with this one for other_room in self.rooms: if new_room.intersect(other_room): break else: # 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() if self.num_rooms == 0: # this is the first room, where they player starts at player.x = new_x player.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[self.num_rooms - 1].center() # flip a coin (random number that is either 0 or 1) if randint(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) self.num_rooms += 1 genNewRoom = True else: genNewRoom = True self.num_attempts += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): # random width & height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # Random position without going out of bounds of map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # Using Rect class to instantiate rooms new_room = Rect(x, y, w, h) # Check other rooms and see if they intersect with this one for other_room in rooms: if new_room.intersect(other_room): break else: # Room is valid, create it # "Paint" room to map's tiles self.create_room(new_room) # Set coordinate of new room (new_x, new_y) = new_room.center() if num_rooms == 0: # This is the first room being created, so place player here player.x = new_x player.y = new_y else: # For all rooms after the first: # Connect to previous room with a tunnel # Center coordinates of previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() # Random selection for tunner route (50/50 chance) if (randint(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 vertical, then horizontal self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) # Place any rendered enemies self.place_entities(new_room, entities, max_monsters_per_room, max_items_per_room) # Finished with room, append it to room list and increment count rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities): # Create two rooms rooms = [] num_rooms = 0 center_of_last_room_x = None cneter_of_last_room_y = None for r in range(max_rooms): # Randomly generate rooms w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # "Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) # Run through the other rooms and see if they intersect with this one for other_room in rooms: if new_room.intersect(other_room): break else: # Room is valid self.create_room(new_room) (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: # This is the first room, spawn the player here player.x = new_x player.y = new_y else: # Connect new room to previous one (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(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) # Append the new room to the list of rooms self.place_entities(new_room, entities) rooms.append(new_room) num_rooms += 1 stairs_component = Stairs(self.dungeon_level + 1) down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', libtcod.black, 'Stairs', render_order=RenderOrder.STAIRS, stairs=stairs_component) entities.append(down_stairs)
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities): rooms = [] num_rooms = 0 center_of_last_room_x = None center_of_last_room_y = None for r in range(max_rooms): w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: self.create_room(new_room) (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: player.x = new_x player.y = new_y else: (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities) rooms.append(new_room) num_rooms += 1 stairs_component = Stairs(self.dungeon_level + 1) down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', libtcod.white, 'Stairs', render_order=RenderOrder.STAIRS, stairs=stairs_component) entities.append(down_stairs)
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities): # seed(1) rooms = [] num_rooms = 0 center_of_last_room_x = None center_of_last_room_y = None for r in range(max_rooms): w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) new_room = Rect(x, y, w, h) # Check if this room intersects with any others for other_room in rooms: if new_room.intersect(other_room): break else: # no intersections, room is valid self.create_room(new_room) (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: # if this is the first room, the player starts here player.x = new_x player.y = new_y else: # all rooms after the first: connect it to the previous room with a tunnel (prev_x, prev_y) = rooms[num_rooms - 1].center() # randomly decide whether to move horizontally or vertically first if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities) # add new room to the list rooms.append(new_room) num_rooms += 1 stairs_component = Stairs(self.dungeon_level + 1) down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', libtcod.white, 'Stairs', render_order=RenderOrder.STAIRS, stairs=stairs_component) entities.append(down_stairs)
def make_map(self): # Create two rooms for demonstration purposes room1 = Rect(20, 15, 10, 15) room2 = Rect(35, 15, 10, 15) self.create_room(room1) self.create_room(room2) self.create_h_tunnel(25, 40, 23)
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): # random width & height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of bounds of map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) new_room = Rect(x, y, w, h) # Check if new room intersects with other rooms: for other_room in rooms: if new_room.intersect(other_room): break else: # no intersections, room is valid # "paint" new room to tiles self.create_room(new_room) # center coordinates of new room (new_x, new_y) = new_room.center() if num_rooms == 0: # First room, where player starts player.x = new_x player.y = new_y else: # all rooms after first # connect to previous room w/ tunnel # center coordinates of previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() # flip a coin if randint(0, 1) == 1: # first move horizontally, then vertically self.create_h_tunnel(prev_x, new_x, new_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) self.place_entities(new_room, entities, max_monsters_per_room, max_items_per_room) rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room): # Create two rooms for deomnstration rooms = [] num_rooms = 0 for r in range(max_rooms): # random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # 'Rect' class makes rectangels easier to work with new_room = Rect(x, y, w, h) center_x, center_y = 0, 0 prev_x, prev_y = 0, 0 # run through existing rooms and see if they intersect with the new one for other_room in rooms: if new_room.intersect(other_room): break else: # no intersections, valid room # paint it to the map's tiles self.create_room(new_room) # center coordinates of new room (center_x, center_y) = new_room.center() if num_rooms == 0: # first room player.x = center_x player.y = center_y else: # not the fist room # have to connect it to previous room with a tunnel (prev_x, prev_y) = rooms[num_rooms - 1].center() # flip a coin if randint(0, 1) == 1: # first move horizontally, then vertically self.create_h_tunnel(prev_x, center_x, prev_y) self.create_v_tunnel(prev_y, center_y, center_x) else: # first move vertically, then horizontal self.create_v_tunnel(prev_y, center_y, center_x) self.create_h_tunnel(prev_x, center_x, prev_y) self.place_entities(new_room, entities, max_monsters_per_room) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room): # Creat two rooms for demonstration purpose rooms = [] num_rooms = 0 for _ in range(max_rooms): # random w and h w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # "Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) # run through the other rooms and check if intersect for other_room in rooms: if new_room.intersect(other_room): break else: # this means no intersections, so room valid # "paint" it to the map's tiles self.create_room(new_room) # center coord of new room (new_x, new_y) = new_room.center() if num_rooms == 0: # The first room, where the player starts player.x = new_x player.y = new_y else: # all other room: # connect it to the previous room with a tunnel # center coord of previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() # flip a coin (0 or 1) if randint(0, 1) == 1: # first move horizon, then vertical self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: # first vertical, then horizon self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities, max_monsters_per_room, max_items_per_room) # finally append the new room to the list rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): #rand width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) #random position in boundaries of map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) #makes rects easier to work with new_room = Rect(x, y, w, h) #check intersections for other_room in rooms: if new_room.intersect(other_room): break else: #room is now valid #create tiles self.create_room(new_room) #center coordinates of the new room (new_x, new_y) = new_room.center() if num_rooms == 0: #First room, where player starts player.x = new_x player.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) = rooms[num_rooms - 1].center() # flip a coin (random number that is either 0 or 1) if randint(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) self.place_entities(new_room, entities, max_monsters_per_room) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room): # Procedurally generate map rooms = [] num_rooms = 0 for r in range(max_rooms): # Random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # Random position without going out of bounds of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # "Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) # Run through other rooms to see if they intersect for other_room in rooms: if new_room.intersect(other_room): break else: # No intersection, valid room # Paint room to maps tiles self.create_room(new_room) # Center coordinates of new room, to use later (new_x, new_y) = new_room.center() if num_rooms == 0: # First room, where the player should spawn player.x = new_x player.y = new_y else: # All rooms after the first # Connect it to the previous room using a tunnel # Center coordinates of previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() # Flip a coin if randint(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) # Add monsters self.place_entities(new_room, entities, max_monsters_per_room) # Finally, append new room to the list rooms.append(new_room) num_rooms += 1
def test_intersects_upper_right(self): # Given rect_a = Rect(0, 2, 5, 5) rect_b = Rect(2, 0, 5, 5) # When result = rect_a.intersects(rect_b) # Then self.assertTrue(result)
def test_not_in_corner(self): # Given rect = Rect(2, 3, 4, 5) # When x, y = rect.center() # Then self.assertEqual(x, 4) self.assertEqual(y, 5)
def test_mixed_sides(self): # Given rect = Rect(0, 0, 4, 7) # When x, y = rect.center() # Then self.assertEqual(x, 2) self.assertEqual(y, 3)
def test_uneven_sides(self): # Given rect = Rect(0, 0, 5, 5) # When x, y = rect.center() # Then self.assertEqual(x, 2) self.assertEqual(y, 2)
def test_no_intersection(self): # Given rect_a = Rect(0, 0, 4, 4) rect_b = Rect(5, 5, 4, 4) # When result = rect_a.intersects(rect_b) # Then self.assertFalse(result)
def test_intersects_touching_corner(self): # Given rect_a = Rect(0, 0, 5, 5) rect_b = Rect(5, 5, 5, 5) # When result = rect_a.intersects(rect_b) # Then self.assertTrue(result)
def test_intersects_tounching_vertically(self): # Given rect_a = Rect(0, 0, 5, 5) rect_b = Rect(0, 5, 5, 5) # When result = rect_a.intersects(rect_b) # Then self.assertTrue(result)
def test_intersects_aligned_vertically(self): # Given rect_a = Rect(2, 2, 5, 5) rect_b = Rect(2, 1, 5, 5) # When result = rect_a.intersects(rect_b) # Then self.assertTrue(result)
def test_center(): expected_x1 = 0 expected_y1 = 0 expected_w = 5 expected_h = 4 rect1 = Rect(expected_x1, expected_y1, expected_w, expected_h) assert rect1.center() == (int( (expected_x1 + expected_w) / 2), int((expected_y1 + expected_h) / 2))
def generate_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities): rooms = [] num_rooms = 0 center_final_x = None center_final_y = None for r in range(max_rooms): w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: self.generate_room(new_room) (new_x, new_y) = new_room.get_center() center_final_x = new_x center_final_y = new_y if num_rooms == 0: player.x = new_x player.y = new_y else: (prev_x, prev_y) = rooms[num_rooms - 1].get_center() if randint(0, 1) == 1: self.generate_horizontal_tunnel(prev_x, new_x, prev_y) self.generate_vertical_tunnel(prev_y, new_y, new_x) else: self.generate_vertical_tunnel(prev_y, new_y, prev_x) self.generate_horizontal_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities) rooms.append(new_room) num_rooms += 1 bonfire_component = Bonfires(self.dungeon_depth + 1) bonfire = Entity(center_final_x, center_final_y, '&', tcod.light_orange, 'Bonfire', render_order=RenderOrder.BONFIRES, bonfire=bonfire_component) entities.append(bonfire)
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): # random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) # "Rect" Class makes rectangles new_room = Rect(x, y, w, h) # run through the other rooms and see if they intersect with this new one for other_room in rooms: if new_room.intersect(other_room): break else: # If the condition didn't catch, this room is valid # Add it to the map's tiles self.create_room(new_room) (new_x, new_y) = new_room.center() if num_rooms == 0: player.x = new_x player.y = new_y else: # This is all the rooms after ther first room was created # Connect them all via tunnels (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: # First horizontal, then vertical self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: # First vertical, then horizontal self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities, max_monsters_per_room, max_items_per_room) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room): rooms = [] num_rooms = 0 for r in range(max_rooms): # random width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) #-1 becasue trying to find top left x and y axis (rem original formula room.x1+1) new_room = Rect(x, y, w, h) #Rect class make rectangle easier to work with for other_room in rooms: # Run through the others rooms and see if they intersect with this one if interect breaks. if new_room.intersect(other_room): break else: # this means there are no intersections, so this room is valid self.create_room(new_room) # paint it to the map's tile (new_x, new_y) = new_room.center( ) #Center coordinate of new rooms, used to put player in and tunnels if num_rooms == 0: # This is the first room, where the player starts at (variable above) player.x = new_x player.y = new_y else: #All rooms after thie first : Connect it to the previous room with a tunnel in the center (prev_x, prev_y) = rooms[ num_rooms - 1].center() # center coordinates of previous room if randint( 0, 1 ) == 1: #Flip a coin ( random that is either 1 or 0 ) 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 hoizontally self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) # create a v and h tunnel if new room is distant away from the center from previous room # only create one tunnel if new room is only 1 direction away from previous room (same y or x axis) self.place_entities(new_room, entities, max_monsters_per_room) rooms.append(new_room) num_rooms += 1
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities): rooms = [] num_rooms = 0 center_of_last_room_x = None center_of_last_room_y = None for r in range(max_rooms): #rand width and height w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) #rand position without going out of map boundaries x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) #Rect class makes rectangles easier to work with new_room = Rect(x, y, w, h) #run through the other rooms and see if they intersect with this one for other_room in rooms: if new_room.intersect(other_room): break else: #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() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: #this is the first room, where the player starts at player.x = new_x player.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) = rooms[num_rooms - 1].center() # flip a coin (random number that is either 0 or 1) if randint(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) self.place_entities(new_room, entities) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1 stairs_component = Stairs(self.dungeon_level + 1) down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', libtcod.white, 'Stairs', render_order=RenderOrder.STAIRS, stairs=stairs_component) entities.append(down_stairs) return False