Exemple #1
0
def game_loop():
    creatures = [
        Creature('Toad', 1),
        Creature('Tiger', 12),
        Creature('Bat', 3),
        Creature('Dragon', 50),
        Creature('Evil Wizard', 1000),
    ]
    hero = Player('Gandolf', 75)
    while True:
        active_creature = choice(creatures)
        print(f'A {active_creature.name} of level {active_creature.level} has appeared from a dark and foggy forest...')
        print()

        cmd = input('Do you [a]ttack, [r]unaway, or [l]ook around? ')
        if cmd == 'a':
            print(f'The hero has {hero.hp} hit points')
            print(f'The {active_creature.name} has {active_creature.hp} hit points')
            while hero.is_alive() and active_creature.is_alive() and input('Continue? (y/n)').strip().lower() == 'y':
                hero.attack(active_creature)
                if not active_creature.is_alive():
                    print(f'The wizard has killed the {active_creature.name}')
                    creatures.remove(active_creature)
                    break

                active_creature.attack(hero)
                if not hero.is_alive():
                    print(f'The wizard was killed by the {active_creature.name}')
                    hero.heal(hero.max_hp)
                    break

                print(f'The hero has {hero.hp} hit points')
                print(f'The {active_creature.name} has {active_creature.hp} hit points')

        elif cmd == 'r':
            hero.heal(hero.max_hp)
            print(f'The wizard has healed')
        elif cmd == 'l':
            print(f'The wizard {hero.name} takes in the surroundings and sees:')
            for c in creatures:
                print(f' * A {c.name} of level {c.level}')
        else:
            print('Ok, exiting game... bye!')
            break

        if not creatures:
            print('You have defeated all the creates, well done!')
            break

        print()
    def test_player_get_damage_dead(self):
        player = Player('Robin Hood', ARCHER)
        player.health = 14

        player.get_damage(15)

        self.assertFalse(player.is_alive())
    def test_player_get_damage_alive(self):
        player = Player('Robin Hood', ARCHER)
        player.health = 20

        player.get_damage(15)

        self.assertTrue(player.is_alive())
def main():
    print('======== RPG ===========')

    player_name = input('Elige el nombre de tu personaje: ')
    player_class = input('Elige entre Arquero(1) y Guerrero(2): ')
    player = Player(player_name, player_class)
    room_count = 1
    enemy_count = 0

    while True:
        player.has_fled = False
        navigation_action = choose_action()
        encounter = decide_encounter(navigation_action)

        if has_enemy_in_encounter(encounter):
            enemy = create_enemy(encounter)
            print('Un {} salvaje aparecio'.format(enemy))

            while is_battle_active(player, enemy):

                print("\nTURNO DE", player_name)
                action = input('Elige accion: (1) Atacar, (2) Huir: ')
                print('Procesando acción...')
                time.sleep(1)
                player.attack(action, enemy)
                enemy.attack(player)
            enemy_count += 1
            print('derrotaste a {} enemigos'.format(enemy_count))
        room_count += 1
        print('Has pasado por la habitación nº', room_count)
        if not player.is_alive():
            print('Tu puntuación es de {} salas'.format(room_count))
            print('GAME OVERRRRRRR!!')
            break