import random class Fighter: name = 'fighter' skill = 12 stamina = 24 luck = 12 has_potion_luck = False has_potion_stamina = False def __str__(self): return "{} {}/{}/{}".format(self.name, self.skill, self.stamina, self.luck) def roll2d6(self): return random.randint(1, 6) + random.randint(1, 6) def swing(self): myroll = self.roll2d6() return self.skill + myroll def howmuchdamage(self): damage = 2 if self.luck >= 7: lucky = self.testmyluck() if lucky: damage = 4 print(" {} is lucky and deals {} damage".format(self.name, damage)) else: damage = 1 print(" {} is unlucky and deals {} damage".format(self.name, damage)) return damage def ouch(self, stamlost): if self.luck >= 7: lucky = self.testmyluck() if lucky: stamlost -= 1 print(" {} is lucky and takes {} damage".format(self.name, stamlost)) else: stamlost += 1 print(" {} is unlucky and takes {} damage".format(self.name, stamlost)) self.stamina -= stamlost if self.stamina < 4 and self.has_potion_stamina: self.take_potion_stamina() def testmyluck(self): lucky = self.roll2d6() <= self.luck self.luck -= 1 if self.luck < 7 and self.has_potion_luck: self.take_potion_luck() return lucky def amidead(self): return self.stamina < 1 def take_potion_luck(self): if not self.has_potion_luck: return self.luck = 13 # cheat because we know initial was 12 print("{} drinks luck potion, restored to {}".format(self.name, self.luck)) self.has_potion_luck = False def take_potion_stamina(self): if not self.has_potion_stamina: return self.stamina = 24 # cheat because we know initial was 24 print("{} drinks stamina potion, restored to {}".format(self.name, self.stamina)) self.has_potion_stamina = False def combatround(fighter1, fighter2): f1r = fighter1.swing() f2r = fighter2.swing() if f1r > f2r: hitter = fighter1 oucher = fighter2 hitter2d6 = f1r oucher2d6 = f2r elif f2r > f1r: hitter = fighter2 oucher = fighter1 hitter2d6 = f2r oucher2d6 = f1r else: hitter = None oucher = None hitter2d6 = None oucher2d6 = None if hitter and oucher: print("{} rolled {} + {} rolled {} = {} is hit".format(hitter, hitter2d6, oucher, oucher2d6, oucher.name)) dmg = hitter.howmuchdamage() oucher.ouch(dmg) else: print("Tie {} to {}, no damage".format(f1r, f2r)) def runpvp(): luckyguy = Fighter() luckyguy.name = 'Lucky' luckyguy.has_potion_luck = True stammyguy = Fighter() stammyguy.name = 'Stammy' stammyguy.has_potion_stamina = True while True: combatround(luckyguy, stammyguy) if luckyguy.amidead(): print("{} died".format(luckyguy.name)) break elif stammyguy.amidead(): print("{} died".format(stammyguy.name)) break if __name__ == '__main__': runpvp()