Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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()
Comment

PREVIOUS NEXT
Code Example
Python :: make python look good 
Python :: import tknter 
Python :: gluten 
Python :: how to create file using python cat command 
Python :: udmi2 roblox 
Python :: how to print the text of varying length in python 
Python :: colorized progress bar python in console 
Python :: python f string columns 
Python :: subplot adjust python 
Python :: python truncate string to length 
Python :: flask app example 
Python :: max of first element in a list of tuples 
Python :: how to install library in python 
Python :: how to traverse a linked list in python 
Python :: how to get more than one word in a list in python 
Python :: dropdown menu for qheaderview python 
Python :: creating a new enviroment in conda 
Python :: pandas series select first value 
Python :: string list into list pandas 
Python :: python how to create attribute of class while iterating a list 
Python :: How to create an infinite sequence of ids in python? 
Python :: array must not contain infs or NaNs 
Python :: discord.py ping command 
Python :: flask docker 
Python :: add day in date python 
Python :: How to efficiently find the first index in an array of distinct numbers that is equal to the value at that index? 
Python :: get package share vs FindPackageShare 
Python :: python show png 
Python :: restart computer py 
Python :: plt turn legend off 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =