python enemy npc class

Solutions on MaxInterview for python enemy npc class by the best coders in the world

showing results for - "python enemy npc class"
Wade
19 Apr 2020
1#!/usr/bin/env python3
2
3import random
4
5class Enemy():
6    def __init__(self,ancestry,gear):
7        self.enemy=ancestry
8        self.weapon=gear
9        self.hp=random.randrange(10,20)
10        self.ac=random.randrange(12,20)
11        self.alive=True
12
13    def fight(self,tgt):
14        print("You take a swing at the " + self.enemy + ".")
15        hit=random.randrange(0,20)
16
17        if self.alive and hit > self.ac:
18            print("You hit the " + self.enemy + " for " + str(hit) + " damage!")
19            self.hp = self.hp - hit
20            print("The " + self.enemy + " has " + str(self.hp) + " HP remaining")
21        else:
22            print("You missed.")
23
24        if self.hp < 1:
25            self.alive=False
26
27# game start
28foe=Enemy("troll","great axe")
29print("You meet a " + foe.enemy + " wielding a " + foe.weapon)
30
31# main loop
32while True:
33   
34    print("Type the a key and then RETURN to attack.")
35        
36    action=input()
37
38    if action.lower() == "a":
39        foe.fight(foe)
40                
41    if foe.alive == False:
42        print("You have won...this time.")
43        exit()
similar questions
queries leading to this page
python enemy npc class