updated

 avatar
unknown
plain_text
a year ago
3.3 kB
4
Indexable
# Changes
Fixed the attack_multipliers Typo: Changed attack_multiplier to attack_multipliers in the Character class initialization for both player and enemy.
Corrected random.int to random.randint: Fixed the typo random.int to random.randint in the attack method.
Fixed HP Print Statement: Corrected print(f"Your enemy is aa {enemy.name} with {player.hp} HP") to print(f"Your enemy is {enemy.name} with {enemy.hp} HP").
Fixed Attack Choice Logic: Corrected the logic for determining the attack_choice to check against the correct variable attack_choice instead of action.
Added Missing strip() Method: Added strip() to the attack_choice input to handle any leading or trailing whitespace.

import random

class Character:
    def __init__(self, name, hp, attack_power, attack_names, attack_multipliers):
        self.name = name
        self.hp = hp
        self.attack_power = attack_power
        self.attack_names = attack_names
        self.attack_multipliers = attack_multipliers
    
    def attack(self, other, attack_name):
        multiplier = self.attack_multipliers[self.attack_names.index(attack_name)]
        damage = random.randint(1, self.attack_power) * multiplier
        other.hp -= damage
        return damage, attack_name

def game():
    player = Character(
        name="Chinese Gorilla",
        hp=40,
        attack_power=20,
        attack_names=["Sit on", "Devour"],
        attack_multipliers=[1, 1.5]
    )
    enemy = Character(
        name="Chinese Faaaaatty",
        hp=80,
        attack_power=26,
        attack_names=["Sit on", "Devour"],
        attack_multipliers=[1.25, 1.5]
    )

    print(f"You are {player.name} with {player.hp} HP")
    print(f"Your enemy is {enemy.name} with {enemy.hp} HP")
    
    while player.hp > 0 and enemy.hp > 0:
        action = input("Do you want to [a]ttack or [r]un away: ").strip().lower()

        if action == 'a':
            print(f"Choose your attack: [1] {player.attack_names[0]} or [2] {player.attack_names[1]}")
            attack_choice = input("Enter your attack choice: ").strip()
            if attack_choice == '1':
                attack_name = player.attack_names[0]
            elif attack_choice == '2':
                attack_name = player.attack_names[1]
            else:
                print("Invalid choice, you lost your turn.")
                continue

            damage, attack_name = player.attack(enemy, attack_name)
            print(f"You used {attack_name} and attacked {enemy.name} for {damage} damage.")
            if enemy.hp > 0:
                print(f"The {enemy.name} has {enemy.hp} HP left.")
            else:
                print(f"The {enemy.name} is defeated.")
                break

            attack_name = random.choice(enemy.attack_names)
            damage, attack_name = enemy.attack(player, attack_name)
            print(f"The {enemy.name} uses {attack_name} and attacks you for {damage} damage.")
            if player.hp > 0:
                print(f"You have {player.hp} HP left.")
            else:
                print("You are defeated!")
                break
        elif action == 'r':
            print("You ran away!")
            break
        else:
            print("Invalid action. Please choose 'a' to attack or 'r' to run.")

    print("Game over")

if __name__ == "__main__":
    game()
Editor is loading...
Leave a Comment