import random class Character: def __init__(self, name, health, attack, defense): self.name = name self.health = health self.attack = attack self.defense = defense def is_alive(self): return self.health > 0 def take_damage(self, damage): self.health -= max(0, damage - self.defense) def attack_target(self, target): damage = random.randint(1, self.attack) target.take_damage(damage) print(f"{self.name} attacks {target.name} for {damage} damage!") class Player(Character): def __init__(self, name): super().__init__(name, health=100, attack=20, defense=10) self.inventory = {"Health Potion": 5} def use_health_potion(self): if self.inventory["Health Potion"] > 0: self.health += 30 self.inventory["Health Potion"] -= 1 print(f"{self.name} uses a Health Potion and restores 30 health!") else: print("No Health Potions left!") class Enemy(Character): def __init__(self, name): super().__init__(name, health=50, attack=15, defense=5) def main(): player_name = input("Enter your character's name: ") player = Player(player_name) enemy = Enemy("Evil Goblin") print(f"A wild {enemy.name} appears!") while player.is_alive() and enemy.is_alive(): print("\nPlayer Stats:") print(f"Health: {player.health}") print(f"Enemy Stats:") print(f"Health: {enemy.health}") action = input("\nWhat do you want to do? (attack/use potion/quit): ").lower() if action == "attack": player.attack_target(enemy) if not enemy.is_alive(): print(f"You defeated the {enemy.name}!") break enemy.attack_target(player) if not player.is_alive(): print("You were defeated. Game over!") break elif action == "use potion": player.use_health_potion() enemy.attack_target(player) if not player.is_alive(): print("You were defeated. Game over!") break elif action == "quit": print("Thanks for playing! Goodbye.") break else: print("Invalid action. Try again.") if __name__ == "__main__": main()