import json import random with open("waterabilities.json", encoding="utf8") as…
- User
import json import random
with open("water_abilities.json", encoding="utf8") as water_ability_file: water_abilities = json.load(water_ability_file)
with open("fire_abilities.json", encoding="utf8") as fire_ability_file: fire_abilities = json.load(fire_ability_file)
enemies_defeated = 0
class User: def init(self, id, name, health, max_health, energy, max_energy): self.id = id self.name = name self.health = health self.max_health = max_health self.energy = energy self.max_energy = max_energy
class Fire(User): def init(self, id, name, moves, health, max_health, energy, max_energy): super().init(id, name, health, max_health, energy, max_energy) self.moves = moves
class Water(User): def init(self, id, name, moves, health, max_health, energy, max_energy): super().init(id, name, health, max_health, energy, max_energy) self.moves = moves
class Enemy: def init(self, id, name, health, max_health, damage): self.health = health self.id = id self.name = name self.max_health = max_health self.damage = damage
class Boss: def init(self, id, name, health, max_health, damage): self.health = health self.id = id self.name = name self.max_health = max_health self.damage = damage
attackchance = random.randint(0, 100)
def deal_damage(player, enemy, move): hit_chance = random.random() if hit_chance <= 0.3: print("You missed") else: damage = move["base"]["Attack"] enemy.health -= damage print(f"{enemy.name} took {damage} damage")
codeenergy_cost = move["base"]["Energy"] player.energy -= energy_cost if player.energy < 0: player.energy = 0 print(f"{player.name} used {move['name']['english']}") print(f"Current energy: {player.energy}") if enemy.health <= 0: print(f"{enemy.name} has been defeated")def take_damage(player, enemy, move): damage = enemy.damage print(f"{player.name} took {damage} damage") player.health -= damage if player.health <= 0: print(f"{player.name} has been defeated") return True return False
def normal_enemy_fight(player, enemies_defeated, abilities): randomenemy = random.randint(100, 200) enemy = Enemy(0, "Normal Enemy", randomenemy, randomenemy, random.randint(10, 100)) print(f"You have encountered a normal enemy with {enemy.health} health") while enemy.health > 0: if isinstance(player, Water): attackordefend = input("Do you want to defend or attack: ") if attackordefend.upper() == "DEFEND": defend_success = random.random() if defend_success > 0.3: print("You successfully defended against the enemy") add_energy(player) else: print("You unsuccessfully defended against the enemy") take_damage(player, enemy, abilities) elif attackordefend.upper() == "ATTACK": if player.energy <= 0: print("You don't have enough energy to use a move") continue watermove = input("Choose a water move (Ice Breath, Tsunami, Icecle, Icecle Snipe, Water Slash, Water Blaze, Hail Storm): ") for move in abilities: if watermove == move["name"]["english"]: defeated = deal_damage(player, enemy, move) if defeated: enemies_defeated += 1 take_damage(player, enemy, move) lost = take_damage(player, enemy, move) if lost: print(f"Name: {player.name}, Enemies defeated: {enemies_defeated}") if enemy.health <= 0: break break
- Assistant
Here's the updated code with changes made only to the
mainfunction and thenormal_enemy_fightfunction to ensure that it only goes to the next round when the enemy is defeated:python - User
it still generates a new enemy everytime i use a move instead of when the enemy is defeated
- Assistant
Apologies for the misunderstanding. I found the issue with the code and made the necessary changes to the
normal_enemy_fightandboss_fightfunctions. Thebreakstatements were placed incorrectly, causing the program to exit the loop earlier than intended. Here is the corrected version:python - User
still does not work
- Assistant
Apologies for the confusion earlier. I have updated the normal_enemy_fight and boss_fight functions to ensure that the new enemy is generated only when the current enemy is defeated. To achieve this, I have added a variable
enemy_defeatedto track whether the enemy has been defeated after making an attack. Here is the corrected version:python