Horje
Python Enemy NPC CLass Code Example
Python Enemy NPC CLass
#!/usr/bin/env python3

import random

class Enemy():
    def __init__(self,ancestry,gear):
        self.enemy=ancestry
        self.weapon=gear
        self.hp=random.randrange(10,20)
        self.ac=random.randrange(12,20)
        self.alive=True

    def fight(self,tgt):
        print("You take a swing at the " + self.enemy + ".")
        hit=random.randrange(0,20)

        if self.alive and hit > self.ac:
            print("You hit the " + self.enemy + " for " + str(hit) + " damage!")
            self.hp = self.hp - hit
            print("The " + self.enemy + " has " + str(self.hp) + " HP remaining")
        else:
            print("You missed.")

        if self.hp < 1:
            self.alive=False

# game start
foe=Enemy("troll","great axe")
print("You meet a " + foe.enemy + " wielding a " + foe.weapon)

# main loop
while True:
   
    print("Type the a key and then RETURN to attack.")
        
    action=input()

    if action.lower() == "a":
        foe.fight(foe)
                
    if foe.alive == False:
        print("You have won...this time.")
        exit()




Python

Related
how to get sum specific columns value in machine learning Code Example how to get sum specific columns value in machine learning Code Example
reverse keys and values in dictionary with zip python Code Example reverse keys and values in dictionary with zip python Code Example
how to use an indefinite number of args in python Code Example how to use an indefinite number of args in python Code Example
python slicing nested list Code Example python slicing nested list Code Example
xpath helium Code Example xpath helium Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
6