Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
2.0 kB
0
Indexable
Never
import random

# Character class
class Character:
    def __init__(self, name, strength, health, defense):
        self.name = name
        self.strength = strength
        self.health = health
        self.defense = defense

    # Taking damage
    def take_damage(self, damage):
        damage_taken = max(0, damage - self.defense)  # Ensure damage taken is not negative
        self.health -= damage_taken
        return damage_taken

    # Attack method
    def attack(self, target):
        damage = self.strength * 2
        damage_dealt = target.take_damage(damage)
        return damage_dealt

    # Alive?
    def is_alive(self):
        return self.health > 0

# Extend a class
class Rogue(Character):
    # Attack method
    def attack(self, target):
        dexterity = 20
        critical_hit = random.randint(0, 100) < dexterity  # Fixed method call
        damage = self.strength * 2
        if critical_hit:
            damage *= 2
            print("*** Critical hit! ***")
        damage_dealt = target.take_damage(damage)
        return damage_dealt

# Class objects
player1 = Character("Steele", 10, 100, 2)
player2 = Rogue("Bobby", 8, 100, 4)

# Create an arena battle
print(player1.name + "; " + str(player1.health))
print(player2.name + "; " + str(player2.health))

while player1.is_alive() and player2.is_alive():
    print(player1.name + "; " + str(player1.health))
    print(player2.name + "; " + str(player2.health))

    damage = player1.attack(player2)
    print(player1.name + " hits " + player2.name + " for " + str(damage))

    if not player2.is_alive():
        break

    damage = player2.attack(player1)
    print(player2.name + " hits " + player1.name + " for " + str(damage))

# Declaring the winner
if player1.is_alive():
    print(player1.name + " wins!")
elif player2.is_alive():
    print(player2.name + " wins!")
else:
    print("It's a draw!")
Leave a Comment