Moortiii

Dungeon Game Battle - Functional

Apr 14th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. def sleep():
  5.     time.sleep(1.5)
  6.     print("")
  7.    
  8. class Player():
  9.     def __init__(self, hitpoints, attack):
  10.         self.hitpoints = hitpoints
  11.         self.attack = attack
  12.  
  13.     def pAttack(self, monster):
  14.         sleep()
  15.         diceRoll = random.randint(1,6)
  16.         if(diceRoll >= 5):
  17.             criticalHit = (self.attack * 2) - 1
  18.             monster.hitpoints -= criticalHit
  19.             print("You critically hit the monster!")
  20.             print("You deal {} damage. The monster now has {} hitpoints left".format(criticalHit, monster.hitpoints))
  21.         else:
  22.             monster.hitpoints -= self.attack
  23.             print("You manage to hit the monster!")
  24.             print("You deal {} damage. The monster now has {} hitpoints left".format(self.attack, monster.hitpoints))
  25.  
  26.     def pBlock(self):
  27.         sleep()
  28.         print("You manage to block the monster's hit!")
  29.         print("You still have {} hitpoints left".format(self.hitpoints))
  30.  
  31.     def pMisses(self, monster):
  32.         sleep()
  33.         print("You miss your swing at the monster!")
  34.         print("The monster still has {} hitpoints left.".format(monster.hitpoints))
  35.        
  36. class Monster():
  37.     def __init__(self, color, hitpoints, attack):
  38.         self.color = color
  39.         self.hitpoints = hitpoints
  40.         self.attack = attack
  41.    
  42.     def mAttack(self, player):
  43.         sleep()
  44.         player.hitpoints -= self.attack
  45.         print("The monster hits its attack!")
  46.         print("It deals {} damage. You now have {} hitpoints left.".format(self.attack, player.hitpoints))
  47.  
  48.     def mMisses(self, player):
  49.         sleep()
  50.         print("The monster misses its attack!")
  51.         print("You still have {} hitpoints left".format(player.hitpoints))
  52.        
  53.     def mBlock(self):
  54.         sleep()
  55.         print("The monster is able to block your attack!")
  56.         print("The monster still has {} hitpoints left".format(self.hitpoints))
  57.  
  58. def fightMonster(player, monster):
  59.     print("The monster has {} attack and {} hitpoints.".format(monster.attack, monster.hitpoints))
  60.     print("You have {} attack and {} hitpoints.".format(player.attack, player.hitpoints))
  61.     while(True):
  62.         mIsStunned = False
  63.         if(player.hitpoints > 0):
  64.             wantsToAttack = input("Do you want to attack or block? 'A' / 'B'\n\n").upper()
  65.             if(wantsToAttack == "A"):
  66.                 pDiceRoll = random.randint(1,6)
  67.                 if(pDiceRoll > 3):
  68.                     player.pAttack(monster)
  69.                 else:
  70.                     player.pMisses(monster)
  71.             else:
  72.                 pDiceRoll = random.randint(1,6)
  73.                 if(pDiceRoll > 3):
  74.                     player.pBlock()
  75.                     print("You stun the monster, it's a guaranteed hit!")
  76.                     mIsStunned = True
  77.                     player.pAttack(monster)
  78.                 else:
  79.                     monster.mAttack(player)
  80.             if(monster.hitpoints <= 0):
  81.                 print("The monster is all out of hitpoints! You win the battle!")
  82.                 break
  83.             else:
  84.                 if(mIsStunned):
  85.                     sleep()
  86.                     print("The monster is stunned and unable to attack")
  87.                 else:
  88.                     print("The monster tries to attack you.")
  89.                     mDiceRoll = random.randint(1,6)
  90.                     if(mDiceRoll > 3):
  91.                         monster.mAttack(player)
  92.                     else:
  93.                         monster.mMisses(player)
  94.         else:
  95.             print("You are out of hitpoints! You lose the battle!")
  96.             break
  97.  
  98. def playGame():
  99.     myMonster = Monster("red", 15, 2)
  100.     myPlayer = Player(20, 4)
  101.     fightMonster(myPlayer, myMonster)
  102.  
  103. playGame()
Advertisement
Add Comment
Please, Sign In to add comment