Advertisement
Nizarus

combat with decorators

Oct 7th, 2022
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | Gaming | 0 0
  1. import random
  2. import numpy as np
  3.  
  4. class Character:
  5.         def __init__(self, name="character", gender="male"):
  6.             self.name=name
  7.             self.gender=gender
  8.             self.age=np.random.randint(15,55)
  9.  
  10.         @property
  11.         def attributes(self, health, damage, resistance):
  12.             if self.age<20:
  13.                 self.health=health-5
  14.             elif self.age>40:
  15.                 self.health=health-5
  16.             else:
  17.                 self.health=health
  18.             self.skill="cleave"
  19.             self.damage=damage
  20.             self.resistance=resistance
  21.  
  22.  
  23.  
  24. class Enemy (Character):
  25.     pass
  26.  
  27.  
  28. class Ally(Character):
  29.     pass
  30.  
  31. def main():
  32.     #define the hero
  33.     name=input("Name: ")
  34.     gender=gn()
  35.     hero=Character(name,gender)
  36.     health, damage, resistance=attr()
  37.     hero.attributes(health, damage, resistance)
  38.     for r in range(np.random.randint(10)):
  39.         print(Character)
  40.         print(f"round:{r}")
  41.         casualties=combatphases(hero, r)
  42.         for casualty in casualties:
  43.             del casualty
  44. def combatpreparation(hero):
  45.     enemies=[]
  46.     allies=[]
  47.     count=0
  48.     number_of_enemies=np.random.randint(5,10)
  49.     number_of_allies=np.random.randint(1,5)
  50.     for en in range(number_of_enemies):
  51.         count+=1
  52.         name=f"enemy {count}"
  53.         gender=random.choice(["male", "female"])
  54.         enemy=Enemy(name, gender)
  55.         enemy.health=np.random.randint(5, 10)
  56.         enemy.damage=np.random.randint(2,8)
  57.         enemy.resistance=enemy.health/2
  58.         enemies.append(enemy)
  59.     for al in range(number_of_allies):
  60.         count+=1
  61.         name=f"ally {count}"
  62.         gender=random.choice(["male", "female"])
  63.         ally=Ally(name, gender)
  64.         ally.health=np.random.randint(10,15)
  65.         ally.damage=np.random.randint(1,5)
  66.         ally.resistance=ally.health/3
  67.         allies.append(ally)
  68.     return allies, enemies
  69. def combatphases(hero, r):
  70.     combat1=[]
  71.     combat2=[]
  72.     enemies=[]
  73.     allies=[]
  74.     attacks=[]
  75.  
  76.     for i in range(1,r):
  77.             allies, enemies=combatpreparation(hero)
  78.             allies.append(hero)
  79.             for ally in allies:
  80.                 combat1.append({ally.name: (ally.health, ally.damage)})
  81.             for enemy in enemies:
  82.                 combat2.append({enemy.name: (enemy.health, enemy.damage)})
  83.  
  84.     for j in combat1:
  85.         ally=random.choice(allies)
  86.         enemy=random.choice(enemies)
  87.         count=0
  88.         if ally==hero:
  89.             if count<2:
  90.                 decide=input("Press Y to use cleave: ")
  91.                 count+=1
  92.                 if decide=="Y":
  93.                     print(f"{hero.name} uses cleave")
  94.                     hero.health+=1
  95.                     attacking=attack(ally, enemy, True)
  96.                     attacking=attack(ally,enemy)
  97.                 else:
  98.                     pass
  99.             else:
  100.                 attacking=attack(ally,enemy)
  101.                 attacks.append(attacking)
  102.                 hero.health+=1
  103.     for k in combat2:
  104.         hero.health+=1
  105.         ally=random.choice(allies)
  106.         enemy=random.choice(enemies)
  107.         attacked=attack(enemy, ally)
  108.         attacks.append(attacked)
  109.     return attacks
  110. def attack(attacker, attacked, cleave=False):
  111.     res=""
  112.     luck1=random.choice([1,2,3,4])
  113.     luck2=random.choice([1,2,3])
  114.     damage=attacker.damage*luck2
  115.     if cleave==False:
  116.         damage=damage/luck1
  117.     else:
  118.         damage=damage+2
  119.     health=attacked.health
  120.     attacked.health=health-damage
  121.     if attacked.health>0:
  122.         print(f"{attacker.name} attacks {attacked.name}:{health} with a normal attack causing {damage} damage")
  123.         return Character("none","None")
  124.     else:
  125.         print(f"{attacker.name} deals a mortal blow to {attacked.name}. {attacked.name} dies!")
  126.         res=attacked
  127.         return res
  128. def attr():
  129.     health=hlth()
  130.     damage=10-(health/10)
  131.     resistance=health/2
  132.     return health, damage, resistance
  133. def hlth():
  134.     try:
  135.         health=int(input("health min 10 max20: "))
  136.         if 20<health or health<10:
  137.             hlth()
  138.         else:
  139.             return health
  140.     except:
  141.         hlth()
  142. def gn():
  143.     gender=input("Gender: ")
  144.     if gender!="male" and gender!="female":
  145.         gn()
  146.     else:
  147.         return gender
  148.  
  149. if __name__=="__main__":
  150.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement