Guest User

Untitled

a guest
May 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. from random import choice, randint
  2.  
  3. class Player(object):
  4. """Class to represent the player. Takes a name and the healthpoints as parameters."""
  5. def __init__(self, name, life):
  6. self.name = name
  7. self.life = life
  8. self.alive = True
  9. self.defend = False
  10. self.attacks = [ ("Slap", 5, 95)
  11. , ("Frying pan", 10, 80)
  12. , ("Kick to the groin", 80, 10)
  13. , ("Defend", None) ]
  14.  
  15. def get_hit(self, damage):
  16. print "%s (%d HP) got hit for %d damage!" % (self.name, self.life, damage)
  17. self.life -= damage
  18. if self.life < 0:
  19. self.alive = False
  20.  
  21. def choose_attack(self):
  22. print "Choose an attack:"
  23. for i, attack in enumerate(self.attacks):
  24. if attack[1] is None:
  25. print "\t(%d) Defend (-50%% chance to get hit)" % i
  26. else:
  27. name, damage, chance = attack
  28. print "\t(%d) %s (Damage: %d, %d%%)" % (i, name, damage, chance)
  29. # no error checking, because I'm lazy \o/
  30. choice = int(raw_input("Choose now: "))
  31. return self.attacks[choice]
  32.  
  33.  
  34. class Enemy(object):
  35. """Class to represent the enemy. Takes a name and the healthpoints as parameters."""
  36. def __init__(self, name, life):
  37. self.name = name
  38. self.life = life
  39. self.alive = True
  40. self.defend = False
  41. self.attacks = [ ("Claw", 5, 95)
  42. , ("Rake", 20, 60)
  43. , ("Defend", None, None) ]
  44.  
  45. def get_hit(self, damage):
  46. print "%s (%d HP) got hit for %d damage!" % (self.name, self.life, damage)
  47. self.life -= damage
  48. if self.life < 0:
  49. self.alive = False
  50.  
  51. def choose_attack(self):
  52. return choice(self.attacks)
  53.  
  54.  
  55. if __name__ == "__main__":
  56. mausi = Player("Mausi", 100)
  57. ben = Enemy("Ben", 70)
  58.  
  59. while True:
  60. mausi_attack = mausi.choose_attack()
  61. ben_attack = ben.choose_attack()
  62.  
  63. if mausi_attack[1] is None:
  64. mausi.defend = True
  65. else:
  66. mausi.defend = False
  67. if ben_attack[1] is None:
  68. ben.defend = True
  69. else:
  70. ben.defend = False
  71.  
  72. if not mausi.defend:
  73. _, damage, chance = mausi_attack
  74. if ben.defend: chance -= 50
  75. if chance > randint(0, 100):
  76. ben.get_hit(damage)
  77.  
  78. if not ben.defend:
  79. _, damage, chance = ben_attack
  80. if mausi.defend: chance -= 50
  81. if chance > randint(0, 100):
  82. mausi.get_hit(damage)
  83.  
  84. if not mausi.alive:
  85. print "You are dead."
  86. break
  87.  
  88. if not ben.alive:
  89. print "You win!"
  90. break
  91.  
  92. print "%s life: %d -- %s life: %d" % (mausi.name, mausi.life, ben.name, ben.life)
Add Comment
Please, Sign In to add comment