import random import time class Player: def __init__(self, name, hp=50, att=0, defense=13, experience=0): self.name = name self.hp = hp self.att = att self.defense = defense self.experience = experience self.spells = {'Freeze': 0} # Starting with the Freeze spell def attack(self, enemy): print(f"{self.name} attacks the {enemy.name}...") if random.random() < 0.9: # 90% chance to hit damage = random.randint(1, 20) + self.att enemy.take_damage(damage) print(f"You deal {damage} damage") else: print("You miss the enemy!") def defend(self): print(f"{self.name} defends...") return True def cast_spell(self, spell, enemy): spell = spell.capitalize() # Convert spell name to capitalize if spell in self.spells and self.experience >= self.spells[spell]: print(f"{self.name} casts {spell}...") if spell == 'Freeze': # Correctly spelled as 'Freeze' enemy.freeze() print("The enemy is frozen and skips its next attack!") elif spell == 'Heal': self.hp += 20 + self.experience // 100 * 5 print(f"You heal yourself. Your HP is now {self.hp}") elif spell == 'Fireball': damage = random.randint(5, 35) + self.experience // 100 * 10 enemy.take_damage(damage) print(f"You deal {damage} damage with Fireball!") elif spell == 'Magic Missile': damage = random.randint(5, 25) + self.experience // 100 * 5 enemy.take_damage(damage) print(f"You deal {damage} damage with Magic Missile!") elif spell == 'Lightning Strike': damage = random.randint(5, 50) enemy.take_damage(damage) print(f"You deal {damage} damage with Lightning Strike!") elif spell == 'Power-word Sleep': enemy.put_to_sleep() print("The enemy falls asleep for 1-3 rounds of attacks!") elif spell == 'Power-word Kill': if random.random() < 0.8: # 80% chance of survival print("The enemy survives the Power-word Kill!") else: print("You annihilate the enemy with Power-word Kill!") enemy.hp = 0 self.experience -= self.spells[spell] else: print("You cannot cast this spell!") def take_damage(self, damage): self.hp -= damage if self.hp <= 0: print(f"{self.name} has been defeated!") return False else: print(f"{self.name} takes {damage} damage. {self.name}'s HP: {self.hp}") return True def level_up(self): print(f"Congratulations! You've leveled up. Current level: {self.experience // 100}") self.experience += 100 self.att += 1 self.defense += 1 if self.experience % 100 == 0: self.learn_spell() def learn_spell(self): new_spell = random.choice(['Ice', 'Heal', 'Fireball', 'Magic Missile', 'Lightning Strike', 'Power-word Sleep', 'Power-word Kill']) if new_spell not in self.spells: self.spells[new_spell] = self.experience class Enemy: def __init__(self, name, hp, att, defense, move): self.name = name self.hp = hp self.att = att self.defense = defense self.move = move self.frozen = False # Add a 'frozen' attribute initialized to False def attack(self, player): if not self.frozen: # Check if the enemy is not frozen print(f"The {self.name} attacks...") time.sleep(1) damage = random.randint(self.move[0], self.move[1]) player.take_damage(damage) else: print(f"The {self.name} is frozen and skips its next attack!") self.frozen = False # Reset the frozen status after skipping one attack def take_damage(self, damage): self.hp -= damage if self.hp <= 0: print(f"The {self.name} has been defeated!") return False else: print(f"The {self.name} takes {damage} damage. {self.name}'s HP: {self.hp}") return True def freeze(self): print(f"The {self.name} is frozen!") self.frozen = True # Set the 'frozen' attribute to True when the enemy is frozen def put_to_sleep(self): print(f"The {self.name} falls asleep for 1-3 rounds of attacks!") def main(): player_name = input("Enter your name: ") player = Player(player_name) enemies_defeated = 0 while True: enemy_list = [ Enemy('Orc', 100, 0, 13, (1, 20)), Enemy('Goblin', 80, 1, 10, (1, 18)), Enemy('Minotaur', 150, 1, 14, (5, 35)), Enemy('Troll', 250, 2, 15, (8, 45)), Enemy('Dragon', 1000, 5, 17, (10, 100)) ] enemy = random.choice(enemy_list) print(f"\nYou encounter a {enemy.name}!") while player.hp > 0 and enemy.hp > 0: print(f"\nHealth levels:\n{player.name} {player.hp}\n{enemy.name} {enemy.hp}") print("\nPossible actions:") print("[a] Attack") print("[d] Defend") print("[s] Cast Spell") choice = input("Your choice? ") if choice == 'a': player.attack(enemy) if enemy.take_damage(player.att): enemy.attack(player) elif choice == 'd': if player.defend(): enemy.attack(player) elif choice == 's': spell_choice = input("Enter spell name: ") player.cast_spell(spell_choice, enemy) if enemy.take_damage(player.spells.get(spell_choice, 0)): enemy.attack(player) else: print("Invalid choice!") if player.hp <= 0: print("Game over! You have been defeated.") break elif enemy.hp <= 0: print(f"You have defeated the {enemy.name}!") player.experience += 50 enemies_defeated += 1 if enemies_defeated % 3 == 0: player.level_up() break play_again = input("Do you want to play again? (yes/no): ") if play_again.lower() != 'yes': print(f"Total enemies defeated: {enemies_defeated}. Final experience: {player.experience}") break if __name__ == "__main__": main()