Guest User

crit'n'miss mechanic

a guest
Feb 14th, 2020
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import choices
  2.  
  3. miss_count = 0
  4. crit_count = 0
  5.  
  6. flags = [True, False]
  7. probabilities = {
  8.     'crit' : [50, 50],
  9.     'miss' : [50, 50]
  10. }
  11.  
  12. class entity():
  13.  
  14.     def __init__(self, hp, damage, probabilities):
  15.         self.hp = hp
  16.         self.damage = damage
  17.         self.crit_probability = probabilities['crit']
  18.         self.miss_probability = probabilities['miss']
  19.  
  20.     def hit(self, target):
  21.         crit = random.choices(flags, weights=self.crit_probability)
  22.         miss = random.choices(flags, weights=target.miss_probability)
  23.  
  24.         if miss:
  25.             global miss_count
  26.             miss_count += 1
  27.             pass
  28.         else:
  29.             if crit:
  30.                 global crit_count
  31.                 crit_count += 1
  32.                 target.hp -= 2 * self.damage
  33.             else:
  34.                 target.hp -= self.damage
  35.  
  36. char = entity(hp=100, damage=10, probabilities=probabilities)
  37. tree = entity(hp=200, damage=0, probabilities=probabilities)
  38.  
  39. char.hit(tree)
  40.  
  41. print(miss_count)
  42. print(crit_count)
RAW Paste Data