Exemplo n.º 1
0
def state(game: GameData, root_console: tdl.Console) -> GameState:

    root_console.clear()
    draw_player_status_bar(game.the_player, 1, root_console.height - 2,
                           root_console)

    # draw the room
    if game.current_room:
        # odd number of tiles makes "centering" easier
        room_draw_width = game.room_draw_width
        room_draw_height = game.room_draw_height

        top = 5
        left = 5
        center_x = int(math.floor(room_draw_width / 2))
        center_y = int(math.floor(room_draw_height / 2))

        for x in range(0, room_draw_width):
            for y in range(0, room_draw_height):
                tile = '.'
                if x == 0 or y == 0 or x == room_draw_width - 1 or y == room_draw_height - 1:
                    tile = '#'
                if game.current_room.north and x == math.floor(
                        room_draw_width / 2) and y == 0:
                    tile = '.'
                if game.current_room.south and x == math.floor(
                        room_draw_width / 2) and y == room_draw_height - 1:
                    tile = '.'
                if game.current_room.west and x == 0 and y == math.floor(
                        room_draw_height / 2):
                    tile = '.'
                if game.current_room.east and x == room_draw_width - 1 and y == math.floor(
                        room_draw_height / 2):
                    tile = '.'
                root_console.draw_char(left + x,
                                       top + y,
                                       tile,
                                       bg=None,
                                       fg=(255, 255, 255))

        # draw the player in the room
        p_x = math.floor(room_draw_width / 2)
        p_y = math.floor(room_draw_height / 2)
        if game.previous_room:
            if game.previous_room == game.current_room.north:
                # came from the north
                p_y = 2
            elif game.previous_room == game.current_room.south:
                # came from the south
                p_y = room_draw_height - 3
            elif game.previous_room == game.current_room.east:
                # came from the east
                p_x = room_draw_width - 3
            elif game.previous_room == game.current_room.west:
                # came from the west
                p_x = 2
        root_console.draw_char(left + p_x,
                               top + p_y,
                               '@',
                               bg=None,
                               fg=(255, 255, 255))

        # draw encounter
        if game.current_room.encounter and not game.current_room.encountered:
            if game.current_room.encounter == EncounterType.MONSTER:
                root_console.draw_char(left + center_x,
                                       top + center_y,
                                       game.current_room.monster.tile,
                                       bg=None,
                                       fg=(255, 255, 255))

            elif game.current_room.encounter == EncounterType.STAIRS:
                game.log(
                    'You find stairs to the next level! Press [ENTER] to advance.',
                    (0, 200, 235))

    # draw info bar
    info_con = tdl.Console(int(root_console.width / 2),
                           root_console.height - 21)
    info_con.set_colors(fg=(220, 220, 220), bg=(0, 0, 50))
    info_con.clear()
    left = 1
    top = 1
    info_con.draw_str(left, top, 'MOVE:')
    top += 1
    info_con.draw_str(left, top, '[UP] North')
    top += 1
    info_con.draw_str(left, top, '[DOWN] South')
    top += 1
    info_con.draw_str(left, top, '[RIGHT] East')
    top += 1
    info_con.draw_str(left, top, '[LEFT] West')
    top += 2
    if len(game.the_player.get_explore_skills()) > 0:
        info_con.draw_str(left, top, 'SKILLS:')
        top += 1
        for index, skill in enumerate(game.the_player.get_explore_skills(), 1):
            skill_text = f'{skill.name}'
            if not skill.ready():
                skill_text = f'{skill_text} ({skill.cooldown_left} turns)'

            info_con.draw_str(left, top, f'[{index}] {skill_text}')
            top += 1
        top += 1
    info_con.draw_str(left, top, 'MENU:')
    top += 1
    info_con.draw_str(left, top, '[m] Map')
    top += 1
    info_con.draw_str(left, top, '[i] Inventory')
    top += 1
    info_con.draw_str(left, top, '[c] Character')
    top += 1
    info_con.draw_str(left, top, '[ESC] Quit')
    root_console.blit(info_con, int(root_console.width / 2), 0)

    # draw the log
    game.draw_log(root_console)

    tdl.flush()

    user_input = wait_for_keys([
        'ESCAPE', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'ENTER', 'i', 'm', 'c', '1',
        '2'
    ])

    if user_input == 'UP':
        return GameState.MOVE_NORTH

    elif user_input == 'DOWN':
        return GameState.MOVE_SOUTH

    elif user_input == 'LEFT':
        return GameState.MOVE_WEST

    elif user_input == 'RIGHT':
        return GameState.MOVE_EAST

    elif user_input == 'ESCAPE':
        return GameState.CLOSE

    elif user_input == 'm':
        return GameState.MAP

    elif user_input == 'i':
        return GameState.INVENTORY

    elif user_input == 'c':
        return GameState.CHARACTER_SHEET

    elif user_input == 'ENTER':
        return GameState.NEXT_LEVEL

    elif user_input == '1' and len(game.the_player.get_explore_skills()) > 0:
        # User skill 1
        skill: ActiveSkill = game.the_player.get_explore_skills()[0]
        if skill.ready():
            if skill.activate(game):
                if skill.uses_turn:
                    game.advance_turn()

    elif user_input == '2' and len(game.the_player.get_explore_skills()) > 1:
        # User skill 2
        skill: ActiveSkill = game.the_player.get_explore_skills()[1]
        if skill.ready():
            if skill.activate(game):
                if skill.uses_turn:
                    game.advance_turn()

    return GameState.ROOM
Exemplo n.º 2
0
def travel_direction(direction, root_console: tdl.Console):
    """
    self-contained "animation" of moving through a hallway in a certain direction
    """
    start_x = 0
    start_y = 0
    end_x = 0
    end_y = 0

    room_draw_width = 9
    room_draw_height = 9

    top = 5
    left = 5

    for x in range(0, room_draw_width):
        for y in range(0, room_draw_height):
            tile = ' '
            if direction == 'north' or direction == 'south':
                if x == math.floor(room_draw_width / 2):
                    tile = '.'
                if x == math.floor(room_draw_width / 2) - 1 or x == math.floor(
                        room_draw_width / 2) + 1:
                    tile = '#'
            elif direction == 'east' or direction == 'west':
                if y == math.floor(room_draw_height / 2):
                    tile = '.'
                if y == math.floor(
                        room_draw_height / 2) - 1 or y == math.floor(
                            room_draw_height / 2) + 1:
                    tile = '#'

            root_console.draw_char(left + x,
                                   top + y,
                                   tile,
                                   bg=None,
                                   fg=(255, 255, 255))

    if direction == 'north' or direction == 'south':
        start_x = math.floor(room_draw_width / 2)
        end_x = start_x
    elif direction == 'east' or direction == 'west':
        start_y = math.floor(room_draw_height / 2)
        end_y = start_y

    if direction == 'north':
        start_y = room_draw_height - 1
    elif direction == 'south':
        end_y = room_draw_height - 1
    elif direction == 'east':
        end_x = room_draw_width - 1
    elif direction == 'west':
        start_x = room_draw_width - 1

    x = start_x
    y = start_y
    while x != end_x or y != end_y:
        root_console.draw_char(left + x,
                               top + y,
                               '.',
                               bg=None,
                               fg=(255, 255, 255))

        if direction == 'north':
            y -= 1
        elif direction == 'south':
            y += 1
        elif direction == 'east':
            x += 1
        elif direction == 'west':
            x -= 1

        root_console.draw_char(left + x,
                               top + y,
                               '@',
                               bg=None,
                               fg=(255, 255, 255))
        tdl.flush()
        time.sleep(.1)
Exemplo n.º 3
0
def state(game: GameData, root_console: tdl.Console) -> GameState:

    if game.current_room.monster is None:
        monster = game.current_room.monster = create_monster_for_level(game.current_level)
    else:
        monster = game.current_room.monster

    player: Character = game.the_player
    player_x = int(root_console.width / 2)
    monster_x = player_x
    top = 5
    left = 5
    room_draw_width = game.room_draw_width
    room_draw_height = game.room_draw_height
    center_x = int(math.floor(room_draw_width / 2))
    center_y = int(math.floor(room_draw_height / 2))

    # if you flee a monster and come back, it will return to full health
    monster.reset()

    game.log(f'You encounter a {monster.name}!', (255, 0, 0))

    fighting: bool = True
    did_flee: bool = False
    player_used_turn: bool = False

    player_combat_skills = player.get_combat_skills()

    while fighting:

        if player_used_turn:
            # tick status effects
            player.tick_status_effects()
            monster.tick_status_effects()

            # tick skills
            player.tick_skill_cooldowns()
            monster.tick_skill_cooldowns()

        # clear console
        root_console.clear()

        # draw room
        root_console.draw_rect(left, top, room_draw_width, room_draw_height, '.', (255, 255, 255), (0, 0, 0))
        root_console.draw_frame(left, top, room_draw_width, room_draw_height, '#', (255, 255, 255), (0, 0, 0))
        if game.current_room.north:
            root_console.draw_char(left + center_x, top, '.', (255, 255, 255), (0, 0, 0))
        if game.current_room.south:
            root_console.draw_char(left + center_x, top + room_draw_height - 1, '.', (255, 255, 255), (0, 0, 0))
        if game.current_room.west:
            root_console.draw_char(left, top + center_y, '.', (255, 255, 255), (0, 0, 0))
        if game.current_room.east:
            root_console.draw_char(left + room_draw_width - 1, top + center_y, '.', (255, 255, 255), (0, 0, 0))

        # draw player
        p_x = center_x
        p_y = center_y
        if game.previous_room:
            if game.previous_room == game.current_room.north:
                # came from the north
                p_y = 2
            elif game.previous_room == game.current_room.south:
                # came from the south
                p_y = room_draw_height - 3
            elif game.previous_room == game.current_room.east:
                # came from the east
                p_x = room_draw_width - 3
            elif game.previous_room == game.current_room.west:
                # came from the west
                p_x = 2
        root_console.draw_char(left + p_x, top + p_y, player.tile, bg=None, fg=(255, 255, 255))

        # draw monster
        root_console.draw_char(left + center_x, top + center_y, monster.tile, bg=None, fg=(255, 0, 0))

        # draw the log
        game.draw_log(root_console)

        # Draw Monster Information
        monster_y = 0
        root_console.draw_str(monster_x, monster_y, f'{monster.name}')
        monster_y += 1
        root_console.draw_str(monster_x, monster_y, f'HP: {monster.health} / {monster.max_health}')
        monster_y += 1
        for status in monster.active_status_effects:
            # list each status effect
            root_console.draw_str(monster_x, monster_y,
                                  f'({status.status_effect.name} x{status.duration})',
                                  fg=(255, 255, 0))
            monster_y += 1

        # Draw Player Information
        player_y = 10
        root_console.draw_str(player_x, player_y, f'{player.name}')
        player_y += 1
        root_console.draw_str(player_x, player_y, f'HP: {player.health} / {player.max_health}')
        player_y += 1
        for status in player.active_status_effects:
            # list each status effect
            root_console.draw_str(player_x, player_y,
                                  f'({status.status_effect.name} x{status.duration})',
                                  fg=(255, 0, 0))
            player_y += 1

        player_y += 2
        root_console.draw_str(player_x, player_y, '[A] Attack')
        player_y += 1

        if len(player_combat_skills) > 0:
            for index, skill in enumerate(player_combat_skills, 1):
                skill_text = f'{skill.name}'
                if not skill.ready():
                    skill_text = f'{skill_text} ({skill.cooldown_left} turns)'

                root_console.draw_str(player_x, player_y, f'[{index}] {skill_text}')
                player_y += 1

        root_console.draw_str(player_x, player_y + 5, '[U] Use Item')
        root_console.draw_str(player_x, player_y + 6, '[F] Flee')

        tdl.flush()

        # only allow available keys
        available_keys = ['ESCAPE', 'a', 'f', 'u']
        if len(player_combat_skills) >= 1 and player_combat_skills[0].ready():
            available_keys.append('1')
        if len(player_combat_skills) >= 2 and player_combat_skills[1].ready():
            available_keys.append('2')
        user_input = wait_for_keys(available_keys)

        if user_input == 'ESCAPE':
            return GameState.CLOSE

        # will be set to True if the player attempts to flee below
        tried_to_flee: bool = False
        player_used_turn = False

        # ATTACK
        if user_input == 'a':
            game.log(f'You attack the {monster.name}...')
            player_used_turn = True
            results = do_attack(player, monster)
            if not results.dodged:
                game.log(f'...and hit for {results.damage} damage.')
                # if the player's weapon has the "Vampiric" trait, heal the player (maybe)
                if player.weapon and player.weapon.suffix == WeaponSuffix.Vampirism:
                    did_vamp = random.randint(1, 101) <= player.weapon.life_steal_chance
                    vamp_amount = int(math.ceil(player.weapon.life_steal_percent * results.damage))
                    # TODO: decide whether or not to tell the player...
                    player.heal(vamp_amount)
                # if the player's weapon has the "Doom" trait, kill the monster if it only has 1 HP left
                elif player.weapon and player.weapon.suffix == WeaponSuffix.Doom and monster.health == 1:
                    monster.health = 0
                    game.log(f'DOOM has befallen the {monster.name}!')

            else:
                # enemy dodged the attack
                game.log(f'...and miss as the {monster.name} dodges out of the way.')
        # USE SKILL 1
        elif user_input == '1' and len(player_combat_skills) > 0:
            skill: ActiveSkill = player_combat_skills[0]
            if skill.ready():
                if skill.activate(game):
                    player_used_turn = skill.uses_turn
        # USE SKILL 2
        elif user_input == '2' and len(player_combat_skills) > 1:
            skill: ActiveSkill = player_combat_skills[1]
            if skill.ready():
                if skill.activate(game):
                    player_used_turn = skill.uses_turn
        # ATTEMPT TO FLEE THE MONSTER
        elif user_input == 'f':
            player_used_turn = True
            game.log('You attempt to flee.')
            tried_to_flee = True
            chance_to_flee = 80
            roll = random.randrange(0, 100)
            if roll <= chance_to_flee:
                did_flee = True
        # OPEN INVENTORY
        elif user_input == 'u':
            # only uses a turn if the player uses an item
            player_used_turn = state_usable_inventory(game, root_console)

        # if the monster is dead, combat is over
        if monster.health <= 0:
            fighting = False
            game.log(f'You slay the {monster.name}!')
            continue

        # if monster is alive, it takes a turn (assuming the player did a "used_turn" action)
        # monster gets a turn even if the player succeeded at fleeing
        # make sure the monster is ABLE to attack
        if player_used_turn:
            if not monster.has_status_effect(StatusEffectType.Stunned):
                game.log(f'The {monster.name} attacks...', (255, 0, 0))
                if tried_to_flee and player.armor and player.armor.suffix == ArmorSuffix.Fleeing:
                    game.log(f'...and misses.')
                else:
                    results = do_attack(monster, player)
                    if not results.dodged:
                        game.log(f'...and hits for {results.damage} damage')
                    else:
                        game.log(f'...and misses you when you dodge out of the way.')

                if player.health <= 0:
                    fighting = False
                    # cannot flee if you are dead, Jim
                    did_flee = False
                    continue
                elif tried_to_flee and not did_flee:
                    game.log("You failed to run away", (255, 0, 0))
                elif tried_to_flee and did_flee:
                    fighting = False
                    continue
            else:
                game.log(f'The {monster.name} is STUNNED and cannot attack.')

    if did_flee:
        # fleeing does not mark the encounter as complete
        text = f'You run away from the {monster.name}, returning to the previous room.'
        game.log(text)
        game.current_room = game.previous_room
        game.previous_room = None
        return GameState.ROOM

    game.current_room.encountered = True

    # killed the monster! yay!
    if monster.health <= 0:
        experience_gained = monster.experience
        if player.weapon and player.weapon.suffix == WeaponSuffix.Adventuring:
            # 15% boost to XP gain
            experience_gained = int(math.ceil(experience_gained * 1.15))
        text = f'You have killed the {monster.name}! You have been awarded {experience_gained} XP.'
        game.log(text)
        if player.grant_experience(experience_gained):
            # player leveled up!
            game.log(f'LEVEL UP! You have reached level {player.level}!', (255, 255, 0))

        return GameState.ROOM

    # player is dead
    return GameState.GAME_OVER