def draw(self, console): bar_width = int(self.current_value / self.max_value * self.width) - 1 display_text = ' {}/{}'.format(int(self.current_value), self.max_value) percentage = self.current_value / self.max_value if percentage > 1.0: self.color = palette.BRIGHT_BLUE elif percentage > 0.75: self.color = palette.BRIGHT_GREEN elif percentage > 0.5: self.color = palette.BRIGHT_YELLOW elif percentage > 0.25: self.color = palette.get_nearest((255, 163, 0)) else: self.color = palette.BRIGHT_RED if percentage > 0 and bar_width == 0: bar_width = 1 for i in range(self.width): c = ' ' if i < len(display_text) and self.show_text: c = display_text[i] bg = 0, 0, 0 if i <= bar_width: bg = self.color console.draw_char(self.x + i, self.y, c, bg=bg)
def draw(self, console): for x, y in self.data: ch, fg, bg = self.data.get_char(x, y) if (x, y) in self.seen_tiles or game.Game.args.no_fog == 'true': if (x, y) in self.visible_tiles: pass #fg = palette.WHITE #fg = color_table[chr(ch)][True] else: fg = palette.get_nearest((95, 87, 79)) ox, oy = utils.math.add(self.position, (x, y)) console.draw_char(ox, oy, ch, fg, bg) for child in self.children: child.draw(console)
def __init__(self, char='+', position=(0, 0)): super().__init__(char, position, fg=palette.get_nearest((171, 82, 54))) self.blocks_visibility = True self.fog_color = palette.get_nearest((95, 87, 79))
def generate_level(width, height, player_count): new_entities = [] new_level = level.Level(0, 0, width, height) new_level.data.draw_str(0, 0, 'X' * width * height) TOP = 0b0001 RIGHT = 0b0010 BOTTOM = 0b0100 LEFT = 0b1000 MASK = 0b1111 STAIRDOWN = 0b010000 STAIRUP = 0b100000 floor = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0] cursor = random.randint(0, 2) floor[cursor] |= STAIRUP def up(): nonlocal floor nonlocal cursor if cursor >= 3: floor[cursor] |= TOP cursor -= 3 floor[cursor] |= BOTTOM return cursor return None def down(): nonlocal floor nonlocal cursor if cursor <= 5: floor[cursor] |= BOTTOM cursor += 3 floor[cursor] |= TOP return cursor return None def right(): nonlocal floor nonlocal cursor if cursor not in [2, 5, 8]: floor[cursor] |= RIGHT cursor += 1 floor[cursor] |= LEFT return cursor return None def left(): nonlocal floor nonlocal cursor if cursor not in [0, 3, 6]: floor[cursor] |= LEFT cursor -= 1 floor[cursor] |= RIGHT return cursor return None possible_moves = [up, down, left, right] moves = random.randint(8, 20) while moves > 0: current_cursor = random.choice(possible_moves)() if current_cursor: moves -= 1 floor[cursor] |= STAIRDOWN # Build out floor for i, m in enumerate(floor): x = i % 3 * 9 + 1 y = i // 3 * 7 current_room = tdl.Console(9, 7) templates = room_templates[m & MASK] room = templates[random.randint(0, len(templates) - 1)] current_room.draw_str(0, 0, room) new_level.data.blit(current_room, x, y) if m & STAIRUP: rect = utils.rect(x, y, 9, 7) potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.Stairs(position=coord) new_entities.append(ent) if m & STAIRDOWN: rect = utils.rect(x, y, 9, 7) potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.StairsDown(position=coord) new_entities.append(ent) color_table = { '#': palette.WHITE, 'X': palette.get_nearest((171, 82, 54)), '.': palette.BRIGHT_BLACK } #temp_level = new_level.data.__copy__() # Colorize tiles for x, y in new_level.data: char, fg, bg = new_level.data.get_char(x, y) if chr(char) == '+': char = ord('.') coords = helpers.DirectionHelper.directions coords = [utils.math.add(c, (x, y)) for c in coords] chars = [ new_level.data.get_char(*c)[0] for c in coords if c in new_level.data ] if ord('+') not in chars: d = door.Door(position=(x, y)) new_entities.append(d) fg = color_table.get(chr(char)) if not fg: fg = palette.WHITE new_level.data.draw_char(x, y, char, fg, bg) # Placing Entities for (x, y) in new_level.data: ch, fg, bg = new_level.data.get_char(x, y) if ch == ord('.'): if random.random() < 1 / 25 * max(1.0, player_count / 3): MonsterClass = registry.Registry.get('monster') mon = MonsterClass(position=(x, y)) new_entities.append(mon) elif random.random() < 1 / 80: WeaponClass = registry.Registry.get('weapon') weapon = WeaponClass(position=(x, y)) new_entities.append(weapon) elif random.random() < 1 / 80: ItemClass = registry.Registry.get('item') item = ItemClass(position=(x, y)) new_entities.append(item) return new_level, new_entities
def generate_level(width, height, player_count, scene_info): new_entities = [] new_level = level.Level(0, 0, width, height) new_level.data.draw_str(0, 0, 'X' * width * height) TOP = 0b0001 RIGHT = 0b0010 BOTTOM = 0b0100 LEFT = 0b1000 MASK = 0b1111 STAIRDOWN = 0b010000 STAIRUP = 0b100000 floor = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0] cursor = random.randint(0, 2) start_cursor = cursor floor[cursor] |= STAIRUP def up(): nonlocal floor nonlocal cursor if cursor >= 3: floor[cursor] |= TOP cursor -= 3 floor[cursor] |= BOTTOM return cursor return None def down(): nonlocal floor nonlocal cursor if cursor <= 5: floor[cursor] |= BOTTOM cursor += 3 floor[cursor] |= TOP return cursor return None def right(): nonlocal floor nonlocal cursor if cursor not in [2, 5, 8]: floor[cursor] |= RIGHT cursor += 1 floor[cursor] |= LEFT return cursor return None def left(): nonlocal floor nonlocal cursor if cursor not in [0, 3, 6]: floor[cursor] |= LEFT cursor -= 1 floor[cursor] |= RIGHT return cursor return None possible_moves = [up, down, left, right] moves = random.randint(8, 20) while moves > 0: current_cursor = random.choice(possible_moves)() if current_cursor and current_cursor != start_cursor: moves -= 1 floor[cursor] |= STAIRDOWN dont_place_monsters_here = [] # Build out floor for i, m in enumerate(floor): x = i % 3 * 9 + 1 y = i // 3 * 7 current_room = tdl.Console(9, 7) templates = room_templates[m & MASK] room = templates[random.randint(0, len(templates) - 1)] current_room.draw_str(0, 0, room) new_level.data.blit(current_room, x, y) if m & STAIRUP: rect = utils.rect(x, y, 9, 7) dont_place_monsters_here = rect potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.Stairs(position=coord) new_entities.append(ent) if m & STAIRDOWN: rect = utils.rect(x, y, 9, 7) potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.StairsDown(position=coord) new_entities.append(ent) color_table = { '#': palette.WHITE, 'X': palette.get_nearest((171, 82, 54)), '.': palette.BRIGHT_BLACK } # Colorize tiles for x, y in new_level.data: char, fg, bg = new_level.data.get_char(x, y) if chr(char) == '+': char = ord('.') coords = helpers.DirectionHelper.directions coords = [utils.math.add(c, (x, y)) for c in coords] chars = [ new_level.data.get_char(*c)[0] for c in coords if c in new_level.data ] if ord('+') not in chars: d = door.Door(position=(x, y)) new_entities.append(d) fg = color_table.get(chr(char)) if not fg: fg = palette.WHITE new_level.data.draw_char(x, y, char, fg, bg) level_scaling_factor = (scene_info['level'] / 10) + 1 monster_spawn_rate = 1 / 16 * max( 1.0, player_count / 3) #* level_scaling_factor uncommon_monster_class = registry.Registry.get('uncommon_monster') common_monster_class = registry.Registry.get('common_monster') uncommon_monster_statuses = [] common_monster_statuses = [] if random.random() < 1 / 2: uncommon_monster_statuses = list( set([ registry.Registry.get('statuses_drop_table') for _ in range(random.randint(0, 2)) ])) uncommon_monster_statuses = [_ for _ in uncommon_monster_statuses if _] if random.random() < 1 / 2: common_monster_statuses = list( set([ registry.Registry.get('statuses_drop_table') for _ in range(random.randint(0, 2)) ])) common_monster_statuses = [_ for _ in common_monster_statuses if _] registry.Registry.clear('monster_drop_table') registry.Registry.register(uncommon_monster_class, 'monster_drop_table', 3) registry.Registry.register(common_monster_class, 'monster_drop_table', 4) scene_info['enemies'] = [] scene_info['enemies'].append( (uncommon_monster_class, uncommon_monster_statuses)) scene_info['enemies'].append( (common_monster_class, common_monster_statuses)) # Placing Entities for (x, y) in new_level.data: ch, fg, bg = new_level.data.get_char(x, y) if ch == ord('.'): if random.random() < monster_spawn_rate and ( x, y) not in dont_place_monsters_here: MonsterClass = registry.Registry.get('monster_drop_table') mon = MonsterClass(position=(x, y)) if MonsterClass is uncommon_monster_class: for status in uncommon_monster_statuses: s = status(mon) mon.add_status(s) else: for status in common_monster_statuses: s = status(mon) mon.add_status(s) new_entities.append(mon) elif random.random() < 1 / 80: WeaponClass = registry.Registry.get('weapon') weapon = WeaponClass(position=(x, y)) new_entities.append(weapon) elif random.random() < 1 / 80: ItemClass = registry.Registry.get('item') item = ItemClass(position=(x, y)) new_entities.append(item) return new_level, new_entities, dont_place_monsters_here
def handle_events(self, event): current_scene = instances.scene_root if event.type == 'TWITCHCHATMESSAGE': if event.message: if event.message.upper() == '!JOIN': player_names = [e.name for e in current_scene.children if hasattr(e, 'name')] bonus = None if not event.nickname in player_names: # Set player color if 'broadcaster' in event.tags['badges']: try: player_color = self.get_config_color('BroadcasterColor') except: player_color = palette.get_nearest((255, 163, 0)) elif event.tags['subscriber'] != '0': try: player_color = self.get_config_color('SubscriberColor') except: player_color = palette.BRIGHT_BLUE bonus = registry.Registry.get('weapon')() elif event.nickname.lower() in TwitchChatManager.special_viewers: try: player_color = self.get_config_color('SpecialViewerColor') except: player_color = palette.BRIGHT_RED else: try: player_color = self.get_config_color('ViewerColor') except: player_color = palette.get_nearest((255, 163, 0)) # Add player pos = current_scene.get_location_near_stairs() p = player.Player(event.nickname[0], pos, fg=player_color) p.name = event.nickname if bonus: p.equip_weapon(bonus) current_scene.append(p) instances.console.print('{} has joined!'.format(p.display_string)) elif event.message.upper() == '!LEAVE': for e in current_scene.children: if not e.isinstance('Player'): continue if e.name == event.nickname: e.die() instances.console.print('{} has left.'.format(e.display_string)) elif event.message.upper().startswith('!CHEER'): s = event.message.split(' ') if len(s) <= 1: return player_names = [p.name for p in instances.scene_root.players if p.state != 'EXITED'] if event.nickname in player_names: return player_name = s[1].lower() if player_name[0] == '@': player_name = player_name[1:] target_player = [p for p in instances.scene_root.players if p.state != 'EXITED' and p.name == player_name] target_player = target_player[0] if target_player else None if target_player: target_player.cheer_counter += 4 elif event.message.upper() == '!HELP': current_time = time.time() if current_time - self.time_since_last_help_message > 30: help_message = 'Available commands: !join !leave !move [uldr] !move @username !stop !attack [uldr] !throw [uldr] !drop !cheer @username' instances.game.observer.send_message(help_message, instances.game.channel) self.time_since_last_help_message = current_time