def make_map(): global map, objects objects = [player] #fill map with blocked tiles because its True, if False all tiles would be unblocked map = [[Tile(True) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)] #our map algorithm rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #pick a random starting point x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1) #lets take advantage of our Rect class now new_room = Rect(x, y, w, h) #do our rooms intersect? failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break #if not lets carve it if not failed: make_room(new_room) #add some contents to this room, such as monsters place_things(new_room) #grabbing center coordinates for some reason (new_x, new_y) = new_room.center() if num_rooms == 0: #this must be first room, start player here at center coordinates player.x = new_x player.y = new_y else: #this is not the first room so lets make a tunnel now (prev_x, prev_y) = rooms[num_rooms - 1].center() #draw a coin, if 1 go horizontal first, if 0 go vertical first if libtcod.random_get_int(0, 0, 1) == 1: #go horizontal first make_h_tunnel(prev_x, new_x, prev_y) make_v_tunnel(prev_y, new_y, new_x) else: #go vertical first make_v_tunnel(prev_y, new_y, prev_x) make_h_tunnel(prev_x, new_x, new_y) #put the room in our list rooms.append(new_room) num_rooms += 1
def __generate_rooms(self): rooms = [] for i in xrange(self.max_rooms): w = libtcod.random_get_int(0, self.room_min_size, self.room_max_size) h = libtcod.random_get_int(0, self.room_min_size, self.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) intersects = False for prev_room in rooms: if new_room.intersects(prev_room): intersects = True break if intersects: continue self.create_room(new_room) if len(rooms) > 0: prev_x, prev_y = rooms[-1].center() new_x, new_y = new_room.center() if libtcod.random_get_int(0, 0, 1) == 1: self.create_tunnel(prev_x, new_x, y=prev_y) self.create_tunnel(prev_y, new_y, x=new_x) else: self.create_tunnel(prev_y, new_y, x=prev_x) self.create_tunnel(prev_x, new_x, y=new_y) rooms.append(new_room) self.__rooms = rooms
def make_map(player, height, width): #fill map with "blocked" tiles dmap = DungeonMap(width, height) num_rooms = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, width - w - 1) y = libtcod.random_get_int(0, 0, 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 failed = False for other_room in dmap.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 create_room(dmap, 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) = dmap.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 create_h_tunnel(dmap, prev_x, new_x, prev_y) create_v_tunnel(dmap, prev_y, new_y, new_x) else: #first move vertically, then horizontally create_v_tunnel(dmap, prev_y, new_y, prev_x) create_h_tunnel(dmap, prev_x, new_x, new_y) #finally, append the new room to the list dmap.rooms.append(new_room) num_rooms += 1 return dmap
def take_turn(self): if self.num_turns > 0: #move random self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1)) self.num_turns -= 1 else:#restore previous ai self.owner.ai = self.old_ai message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)
def take_turn(self): if self.num_turns > 0: #still confused... #move in a random direction, and decrease the number of turns confused self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1)) self.num_turns -= 1 else: #restore the previous AI (this one will be deleted because it's not referenced anymore) self.owner.ai = self.old_ai message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)
def take_turn(self): if self.num_turns > 0: #move random self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1)) self.num_turns -= 1 else: #restore previous ai self.owner.ai = self.old_ai message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)
def __generate_monsters(self): for room in self.__map.rooms: num_monsters = libtcod.random_get_int(0, 0, self.max_room_monsters) for i in xrange(num_monsters): x = libtcod.random_get_int(0, room.x1, room.x2) y = libtcod.random_get_int(0, room.y1, room.y2) if Entity.has_blocker(x, y): continue if libtcod.random_get_int(0, 0, 1) == 0: self.__create_entity(x, y, 'g', 'goblin', libtcod.desaturated_amber, True, Fighter(hp=12, power=2, defense=1, on_death=monster_death), BasicMonster()) else: self.__create_entity(x, y, 'k', 'kobold', libtcod.light_green, True, Fighter(hp=10, power=4, defense=0, on_death=monster_death), BasicMonster())
def render_random_ambient_sounds(): global frame_counter, next_random_frame frame_counter = frame_counter + 1 if next_random_frame == frame_counter: index = libtcod.random_get_int(0, 0, (len(sound_random_ambience))-1) sound_random_ambience[index].volume = libtcod.random_get_float(0, MIN_VOLUME_RANDOM_SOUND, MAX_VOLUME_RANDOM_SOUND) sound_random_ambience[index].play() next_random_frame = random_frame() frame_counter = 0
def render_random_ambient_sounds(): global frame_counter, next_random_frame frame_counter = frame_counter + 1 if next_random_frame == frame_counter: index = libtcod.random_get_int(0, 0, (len(sound_random_ambience)) - 1) sound_random_ambience[index].volume = libtcod.random_get_float( 0, MIN_VOLUME_RANDOM_SOUND, MAX_VOLUME_RANDOM_SOUND) sound_random_ambience[index].play() next_random_frame = random_frame() frame_counter = 0
def run(self, map, bounds=None): if bounds is None: bounds = Rect((1, 1), len(map) - 1, len(map[0]) - 1) random = libtcod.random_get_int(self.seed, 0, self.width_variation) room = Rect((bounds.x1 + random, bounds.y1 + random), 0, 0) room.setSecondPos(((bounds.x2 - random, bounds.y2 - random))) self.carve_room(map, room)
def place_things(room): #random number of monsters num_monsters = libtcod.random_get_int(0, 0, MAX_ROOM_MONSTERS) for i in range(num_monsters): #random position x = libtcod.random_get_int(0, room.x1+1, room.x2-1) y = libtcod.random_get_int(0, room.y1+1, room.y2-1) #only place shit if the tile isn't blocked by another thing if not is_blocked(x, y): if libtcod.random_get_int(0, 0, 100) < 80: #80% chance of getting a dick #make dick fighter_comp = Fighter(hp=10, defense=0, power=3, death_function=monster_death) ai_comp = BasicMonster() monster = Thing(x, y, 'd', 'dick', libtcod.pink, blocks=True, fighter=fighter_comp, ai=ai_comp) else: #make balls fighter_comp = Fighter(hp=5, defense=0, power=1, death_function=monster_death) ai_comp = BasicMonster() monster = Thing(x, y, '8', 'balls', libtcod.pink, blocks=True, fighter=fighter_comp, ai=ai_comp) objects.append(monster) #random number of items num_items = libtcod.random_get_int(0, 0, MAX_ROOM_ITEMS) for i in range(num_items): #random spot x = libtcod.random_get_int(0, room.x1+1, room.x2-1) y = libtcod.random_get_int(0, room.y1+1, room.y2-1) #only put on unblocked tiles if not is_blocked(x, y): dice = libtcod.random_get_int(0, 0, 100) if dice < 70: #create health pot item_comp = Item(use_function=cast_heal) item = Thing(x, y, '!', 'healing potion', libtcod.violet, item=item_comp) elif dice < 70 + 15: #create confuse scroll item_comp = Item(use_function=cast_confuse) item = Thing(x, y, '?', 'scroll of confusion', libtcod.white, item=item_comp) else: #create lightning bolt scroll item_comp = Item(use_function=cast_lightning) item = Thing(x, y, '?', 'scroll of static lightning', libtcod.light_yellow, item=item_comp) objects.append(item) item.send_to_back() #items appear below other objects when drawn on the console
def place_objects(room): #choose random number of monsters num_monsters = libtcod.random_get_int(0, 0, MAX_ROOM_MONSTERS) for i in range(num_monsters): #choose random spot for this monster x = libtcod.random_get_int(0, room.x1+1, room.x2-1) y = libtcod.random_get_int(0, room.y1+1, room.y2-1) #only place it if the tile is not blocked if not is_blocked(x, y): if libtcod.random_get_int(0, 0, 100) < 80: #80% chance of getting an orc #create an orc monster = Fighter(x, y, 'o', 'orc', libtcod.desaturated_green, blocks=True, ai=BasicMonster(), hp=10, defense=0, power=3, soundset=sound_orc, death_function=monster_death) else: #create a troll monster = Fighter(x, y, 'T', 'troll', libtcod.darker_green, blocks=True, ai=BasicMonster(), hp=16, defense=1, power=4, soundset=sound_troll, death_function=monster_death) objects.append(monster) #choose random number of items num_items = libtcod.random_get_int(0, 0, MAX_ROOM_ITEMS) for i in range(num_items): #choose random spot for this item x = libtcod.random_get_int(0, room.x1+1, room.x2-1) y = libtcod.random_get_int(0, room.y1+1, room.y2-1) #only place it if the tile is not blocked if not is_blocked(x, y): dice = libtcod.random_get_int(0, 0, 100) if dice < 70: #create a healing potion (70% chance) item = Item(x, y, '!', 'healing potion', libtcod.violet, blocks=False, ai=None, use_function=cast_heal) elif dice < 70+10: #create a lightning bolt scroll (10% chance) item = Item(x, y, '#', 'scroll of lightning bolt', libtcod.light_yellow, blocks=False, ai=None, use_function=cast_lightning) elif dice < 70+10+10: #create a fireball scroll (10% chance) item = Item(x, y, '#', 'scroll of fireball', libtcod.light_yellow, blocks=False, ai=None, use_function=cast_fireball) else: #create a confuse scroll (10% chance) item = Item(x, y, '#', 'scroll of confusion', libtcod.light_yellow, blocks=False, ai=None, use_function=cast_confuse) objects.append(item) item.send_to_back() #items appear below other objects
def random_choice_index(chances): # choose one option from list of chances, returning its index # the dice will land on some number between 1 and the sum of # the chances dice = libtcod.random_get_int(0, 1, sum(chances)) # go through all chances, keeping the sum so far running_sum = 0 choice = 0 for w in chances: running_sum += w # see if the dice landed in the part that corresponds to this # choice if dice <= running_sum: return choice choice += 1
def place_objects(room): # this is where we decide the chance of each monster or item # appearing. # maximum number of monsters per room max_monsters = from_dungeon_level([[2, 1], [3, 4], [5, 6]]) # chance of each monster monster_chances = {} # orc always shows up, even if all other monsters have 0 chance monster_chances['orc'] = 80 monster_chances['troll'] = from_dungeon_level([[15, 3], [30, 5], [60, 7]]) # maximum number of items per room max_items = from_dungeon_level([[1, 1], [2, 4]]) # chance of each item (by default they have a chance of 0 at # level 1, which then goes up) item_chances = {} # healing potion always shows up, even if all other items have 0 chance item_chances['heal'] = 35 item_chances['lightning'] = from_dungeon_level([[25, 4]]) item_chances['fireball'] = from_dungeon_level([[25, 6]]) item_chances['confuse'] = from_dungeon_level([[10, 2]]) item_chances['sword'] = from_dungeon_level([[5, 4]]) item_chances['shield'] = from_dungeon_level([[15, 8]]) # choose random number of monsters num_monsters = libtcod.random_get_int(0, 0, max_monsters) for i in range(num_monsters): # choose random spot for this monster x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) # only place it if the tile is not blocked if not is_blocked(x, y): choice = random_choice(monster_chances) if choice == 'orc': # create an orc fighter_component = Fighter(hp=20, defense=0, power=4, xp=35, death_function=monster_death) ai_component = BasicMonster() monster = Object(x, y, 'o', 'orc', libtcod.desaturated_green, blocks=True, fighter=fighter_component, ai=ai_component) elif choice == 'troll': # create a troll fighter_component = Fighter(hp=30, defense=2, power=8, xp=100, death_function=monster_death) ai_component = BasicMonster() monster = Object(x, y, 'T', 'troll', libtcod.darker_green, blocks=True, fighter=fighter_component, ai=ai_component) objects.append(monster) # choose random number of items num_items = libtcod.random_get_int(0, 0, max_items) for i in range(num_items): # choose random spot for this item x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) # only place it if the tile is not blocked if not is_blocked(x, y): choice = random_choice(item_chances) if choice == 'heal': # create a healing potion item_component = Item(use_function=cast_heal) item = Object(x, y, '!', 'healing potion', libtcod.violet, item=item_component) elif choice == 'lightning': # create a lightning bolt scroll item_component = Item(use_function=cast_lightning) item = Object(x, y, '#', 'scroll of lightning bolt', libtcod.light_yellow, item=item_component) elif choice == 'fireball': # create a fireball scroll item_component = Item(use_function=cast_fireball) item = Object(x, y, '#', 'scroll of fireball', libtcod.light_yellow, item=item_component) elif choice == 'confuse': # create a confuse scroll item_component = Item(use_function=cast_confuse) item = Object(x, y, '#', 'scroll of confusion', libtcod.light_yellow, item=item_component) elif choice == 'sword': # create a sword equipment_component = Equipment(slot='right hand', power_bonus=3) item = Object(x, y, '/', 'sword', libtcod.sky, equipment=equipment_component) elif choice == 'shield': # create a shield equipment_component = Equipment(slot='left hand', defense_bonus=1) item = Object(x, y, '[', 'shield', libtcod.darker_orange, equipment=equipment_component) objects.append(item) # items appear below other objects, and are visible even # outside of FOV, if in an explored area item.send_to_back() item.always_visible = True
def make_map(): global map, player map = [[]] #fill map with "blocked" tiles map = [[Tile(True) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)] rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 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 failed = False for other_room in 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 create_room(new_room) #add some contents to this room, such as monsters place_objects(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() #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 create_h_tunnel(prev_x, new_x, prev_y) create_v_tunnel(prev_y, new_y, new_x) else: #first move vertically, then horizontally create_v_tunnel(prev_y, new_y, prev_x) create_h_tunnel(prev_x, new_x, new_y) #finally, append the new room to the list rooms.append(new_room) num_rooms += 1 #and create random ambience for this room create_ambient_point( new_x, new_y, libtcod.random_get_int(0, MIN_AMBIENCE_RANGE, MAX_AMBIENCE_RANGE), sound_ambience[libtcod.random_get_int( 0, 0, (len(sound_ambience)) - 1)]) #and finally, place hole for the next level. random_room = rooms[libtcod.random_get_int(0, 0, num_rooms - 1)] place_hole(random_room)
def place_things(room): #random number of monsters num_monsters = libtcod.random_get_int(0, 0, MAX_ROOM_MONSTERS) for i in range(num_monsters): #random position x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) #only place shit if the tile isn't blocked by another thing if not is_blocked(x, y): if libtcod.random_get_int(0, 0, 100) < 80: #80% chance of getting a dick #make dick fighter_comp = Fighter(hp=10, defense=0, power=3, death_function=monster_death) ai_comp = BasicMonster() monster = Thing(x, y, 'd', 'dick', libtcod.pink, blocks=True, fighter=fighter_comp, ai=ai_comp) else: #make balls fighter_comp = Fighter(hp=5, defense=0, power=1, death_function=monster_death) ai_comp = BasicMonster() monster = Thing(x, y, '8', 'balls', libtcod.pink, blocks=True, fighter=fighter_comp, ai=ai_comp) objects.append(monster) #random number of items num_items = libtcod.random_get_int(0, 0, MAX_ROOM_ITEMS) for i in range(num_items): #random spot x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) #only put on unblocked tiles if not is_blocked(x, y): dice = libtcod.random_get_int(0, 0, 100) if dice < 70: #create health pot item_comp = Item(use_function=cast_heal) item = Thing(x, y, '!', 'healing potion', libtcod.violet, item=item_comp) elif dice < 70 + 15: #create confuse scroll item_comp = Item(use_function=cast_confuse) item = Thing(x, y, '?', 'scroll of confusion', libtcod.white, item=item_comp) else: #create lightning bolt scroll item_comp = Item(use_function=cast_lightning) item = Thing(x, y, '?', 'scroll of static lightning', libtcod.light_yellow, item=item_comp) objects.append(item) item.send_to_back( ) #items appear below other objects when drawn on the console
def place_hole(room): x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) hole = Object(x, y, chr(libtcod.CHAR_ARROW_S), 'hole', libtcod.black) objects.append(hole)
def place_objects(room): #choose random number of monsters num_monsters = libtcod.random_get_int(0, 0, MAX_ROOM_MONSTERS) for i in range(num_monsters): #choose random spot for this monster x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) #only place it if the tile is not blocked if not is_blocked(x, y): if libtcod.random_get_int(0, 0, 100) < 80: #80% chance of getting an orc #create an orc monster = Fighter(x, y, 'o', 'orc', libtcod.desaturated_green, blocks=True, ai=BasicMonster(), hp=10, defense=0, power=3, soundset=sound_orc, death_function=monster_death) else: #create a troll monster = Fighter(x, y, 'T', 'troll', libtcod.darker_green, blocks=True, ai=BasicMonster(), hp=16, defense=1, power=4, soundset=sound_troll, death_function=monster_death) objects.append(monster) #choose random number of items num_items = libtcod.random_get_int(0, 0, MAX_ROOM_ITEMS) for i in range(num_items): #choose random spot for this item x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) #only place it if the tile is not blocked if not is_blocked(x, y): dice = libtcod.random_get_int(0, 0, 100) if dice < 70: #create a healing potion (70% chance) item = Item(x, y, '!', 'healing potion', libtcod.violet, blocks=False, ai=None, use_function=cast_heal) elif dice < 70 + 10: #create a lightning bolt scroll (10% chance) item = Item(x, y, '#', 'scroll of lightning bolt', libtcod.light_yellow, blocks=False, ai=None, use_function=cast_lightning) elif dice < 70 + 10 + 10: #create a fireball scroll (10% chance) item = Item(x, y, '#', 'scroll of fireball', libtcod.light_yellow, blocks=False, ai=None, use_function=cast_fireball) else: #create a confuse scroll (10% chance) item = Item(x, y, '#', 'scroll of confusion', libtcod.light_yellow, blocks=False, ai=None, use_function=cast_confuse) objects.append(item) item.send_to_back() #items appear below other objects
def make_map(): global map, objects objects = [player] #fill map with blocked tiles because its True, if False all tiles would be unblocked map = [[ Tile(True) for y in range(MAP_HEIGHT) ] for x in range(MAP_WIDTH) ] #our map algorithm rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #pick a random starting point x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1) #lets take advantage of our Rect class now new_room = Rect(x, y, w, h) #do our rooms intersect? failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break #if not lets carve it if not failed: make_room(new_room) #add some contents to this room, such as monsters place_things(new_room) #grabbing center coordinates for some reason (new_x, new_y) = new_room.center() if num_rooms == 0: #this must be first room, start player here at center coordinates player.x = new_x player.y = new_y else: #this is not the first room so lets make a tunnel now (prev_x, prev_y) = rooms[num_rooms-1].center() #draw a coin, if 1 go horizontal first, if 0 go vertical first if libtcod.random_get_int(0, 0, 1) == 1: #go horizontal first make_h_tunnel(prev_x, new_x, prev_y) make_v_tunnel(prev_y, new_y, new_x) else: #go vertical first make_v_tunnel(prev_y, new_y, prev_x) make_h_tunnel(prev_x, new_x, new_y) #put the room in our list rooms.append(new_room) num_rooms += 1
def random_frame(): return libtcod.random_get_int(0, LIMIT_FPS*MIN_SECONDS_RANDOM_SOUND, LIMIT_FPS*MAN_SECONDS_RANDOM_SOUND)
def place_hole(room): x = libtcod.random_get_int(0, room.x1+1, room.x2-1) y = libtcod.random_get_int(0, room.y1+1, room.y2-1) hole = Object(x, y, chr(libtcod.CHAR_ARROW_S), 'hole', libtcod.black) objects.append(hole)
def make_map(): global map, player map = [[]] #fill map with "blocked" tiles map = [[ Tile(True) for y in range(MAP_HEIGHT) ] for x in range(MAP_WIDTH) ] rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 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 failed = False for other_room in 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 create_room(new_room) #add some contents to this room, such as monsters place_objects(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() #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 create_h_tunnel(prev_x, new_x, prev_y) create_v_tunnel(prev_y, new_y, new_x) else: #first move vertically, then horizontally create_v_tunnel(prev_y, new_y, prev_x) create_h_tunnel(prev_x, new_x, new_y) #finally, append the new room to the list rooms.append(new_room) num_rooms += 1 #and create random ambience for this room create_ambient_point(new_x, new_y, libtcod.random_get_int(0, MIN_AMBIENCE_RANGE, MAX_AMBIENCE_RANGE), sound_ambience[libtcod.random_get_int(0, 0, (len(sound_ambience))-1)]) #and finally, place hole for the next level. random_room = rooms[libtcod.random_get_int(0, 0, num_rooms-1)] place_hole(random_room)
def make_map(): global map, objects, stairs # the list of objects with just the player objects = [player] # fill map with "blocked" tiles map = [[Tile(True) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)] rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): # random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) # random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 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 failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: # this means there are no intersections, so this room is # valid, so 'paint' it to the map tiles create_room(new_room) # add some contents to this room, such as monsters place_objects(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: # connect all rooms after the first and connect them # to the previous room, but first get the coordinates # of the center for the previous room (prev_x, prev_y) = 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 create_h_tunnel(prev_x, new_x, prev_y) create_v_tunnel(prev_y, new_y, new_x) else: # first move vertically, then horizontally create_v_tunnel(prev_y, new_y, prev_x) create_h_tunnel(prev_x, new_x, new_y) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1 # create stairs at the center of the last room, and draw them below # the monsters stairs = Object(new_x, new_y, '<', 'stairs', libtcod.white, always_visible=True) objects.append(stairs) stairs.send_to_back()
def random_frame(): return libtcod.random_get_int(0, LIMIT_FPS * MIN_SECONDS_RANDOM_SOUND, LIMIT_FPS * MAN_SECONDS_RANDOM_SOUND)