Skip to content
Advertisement

How do I stop characters from attacking after they die?

I’m working on a python text-based adventure and I can’t figure out how to stop a character from doing damage once they die. The program works fine when I run it, but even after a character dies, they can still attack the other characters. I’ve looked on google but I can’t find any relevant info

Here’s a snippet of my code:

from random import randint
from random import choice

class Warrior():
    def __init__(self,name):
        self.name = name
        self.health = 130
        self.damage = 20
        self.stamina = 100
    def takeDamage(self,amt):
        self.health -= amt
    def fist(self,who):
        who.takeDamage(self.damage)
    def sword(self,amt2):
        if self.stamina > 25:
            chance = randint(0, 100)
            if chance > 80:
                self.health -= 50
                self.stamina -= 25
                print(self.name, "performed a critical attack! ")
            else:
                self.health -= 30
                self.stamina -= 15
        else:
            print(self.name, "is too tired")
        if self.health <= 0:
            print(self.name, "died. ")
            self.health = 0
        self.stamina += 10
        if self.health == 0:
            pass

p1 = Warrior("Bob")
p2 = Archer("Jim")
p3 = Mage("Tom")
p4 = Priest("Sam")

print(p1.name, p1.health)
print(p2.name, p2.health)
print(p3.name, p3.health)
print(p4.name, p4.health)

print(p1.name,"bonked",p2.name,"with a rock")
p1.sword(p2)
print(p2.name,"shot",p3.name,"with a bow")
p2.bow(p3)
print(p3.name,"fried",p4.name,"with a stick")
p3.staff(p4)
print(p4.name,"healed themselves")
p4.heal(p4)

Advertisement

Answer

The problem is this if statement

if self.stamina > 25:
    chance = randint(0, 100)
    if chance > 80:
        self.health -= 50
        self.stamina -= 25
        print(self.name, "performed a critical attack! ")
    else:
        self.health -= 30
        self.stamina -= 15
else:
    print(self.name, "is too tired")

It is run before the health check is run, and even if the health check was run, it still continues on to the above if statement.

You should edit your sword function to be like this:

def sword(self,amt2):
    if self.health <= 0:
        print(self.name, "is already dead.")
        return
    if self.stamina > 25:
        chance = randint(0, 100)
        if chance > 80:
            self.health -= 50
            self.stamina -= 25
            print(self.name, "performed a critical attack! ")
        else:
            self.health -= 30
            self.stamina -= 15
    else:
        print(self.name, "is too tired")
    if self.health <= 0:
        print(self.name, "died. ")
        self.health = 0
    self.stamina += 10

If the warrior’s health is below zero, the function returns and ends, and prints that the warrior is already dead.

Another way to handle it without returning to change this if statement:

if self.stamina > 25:

To an elif statement:

elif self.stamina > 25:

So that it only gets run if the warrior has health left, or else it is skipped. You still have to put the health check above that statement, else it wont work.

Advertisement