ravi updated code

 avatar
unknown
plain_text
a year ago
3.2 kB
5
Indexable
# My edits:
# Corrected typos and formatting issues in strings.
# Fixed the logic for selecting an attack in the game loop.
# Corrected the input handling for choosing an attack.
# Ensured consistent string formatting and spacing.
# Added necessary breaks to stop the loop when an enemy is defeated.

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="Faze_fulldiddy",
        hp=160,
        attack_power=37,
        attack_names=["diddy party", "Full diddy experience"],
        attack_multipliers=[1, 1.5]  # diddy party has 1x damage, full diddy experience has 1.5x damage
    )
    enemy = Character(
        name="Stretchy Sketch",
        hp=130,
        attack_power=40,
        attack_names=["OF video", "Going in the deep end"],
        attack_multipliers=[1, 1.5]  # OF video has 1x damage, Going in the deep end has 1.5x damage
    )

    print("Welcome to my diddy ah RPG games")
    print(f"You are {player.name} with {player.hp} HP!\n")
    print(f"An enemy {enemy.name} appears with {enemy.hp} HP!\n")

    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 use {attack_name} and attack the {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 away.")

    print("Game over")

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