Moortiii icon

Dungeon Game Battle - Functional

Moortiii | PRO | 04/14/17 11:47:52 AM UTC | 0 ⭐ | 323 👁️ | Never ⏰ | []
Python |

3.76 KB

|

None

|

0 👍

/

0 👎

import random
import time
 
def sleep():
    time.sleep(1.5)
    print("")
    
class Player():
    def __init__(self, hitpoints, attack):
        self.hitpoints = hitpoints
        self.attack = attack
 
    def pAttack(self, monster):
        sleep()
        diceRoll = random.randint(1,6)
        if(diceRoll >= 5):
            criticalHit = (self.attack * 2) - 1
            monster.hitpoints -= criticalHit
            print("You critically hit the monster!")
            print("You deal {} damage. The monster now has {} hitpoints left".format(criticalHit, monster.hitpoints))
        else:
            monster.hitpoints -= self.attack
            print("You manage to hit the monster!")
            print("You deal {} damage. The monster now has {} hitpoints left".format(self.attack, monster.hitpoints))
 
    def pBlock(self):
        sleep()
        print("You manage to block the monster's hit!")
        print("You still have {} hitpoints left".format(self.hitpoints))
 
    def pMisses(self, monster):
        sleep()
        print("You miss your swing at the monster!")
        print("The monster still has {} hitpoints left.".format(monster.hitpoints))
        
class Monster():
    def __init__(self, color, hitpoints, attack):
        self.color = color
        self.hitpoints = hitpoints
        self.attack = attack
    
    def mAttack(self, player):
        sleep()
        player.hitpoints -= self.attack
        print("The monster hits its attack!")
        print("It deals {} damage. You now have {} hitpoints left.".format(self.attack, player.hitpoints))
 
    def mMisses(self, player):
        sleep()
        print("The monster misses its attack!")
        print("You still have {} hitpoints left".format(player.hitpoints))
        
    def mBlock(self):
        sleep()
        print("The monster is able to block your attack!")
        print("The monster still has {} hitpoints left".format(self.hitpoints))
 
def fightMonster(player, monster):
    print("The monster has {} attack and {} hitpoints.".format(monster.attack, monster.hitpoints))
    print("You have {} attack and {} hitpoints.".format(player.attack, player.hitpoints))
    while(True):
        mIsStunned = False
        if(player.hitpoints > 0):
            wantsToAttack = input("Do you want to attack or block? 'A' / 'B'\n\n").upper()
            if(wantsToAttack == "A"):
                pDiceRoll = random.randint(1,6)
                if(pDiceRoll > 3):
                    player.pAttack(monster)
                else:
                    player.pMisses(monster)
            else:
                pDiceRoll = random.randint(1,6)
                if(pDiceRoll > 3):
                    player.pBlock()
                    print("You stun the monster, it's a guaranteed hit!")
                    mIsStunned = True
                    player.pAttack(monster)
                else:
                    monster.mAttack(player)
            if(monster.hitpoints <= 0):
                print("The monster is all out of hitpoints! You win the battle!")
                break
            else:
                if(mIsStunned):
                    sleep()
                    print("The monster is stunned and unable to attack")
                else:
                    print("The monster tries to attack you.")
                    mDiceRoll = random.randint(1,6)
                    if(mDiceRoll > 3):
                        monster.mAttack(player)
                    else:
                        monster.mMisses(player)
        else:
            print("You are out of hitpoints! You lose the battle!")
            break
 
def playGame():
    myMonster = Monster("red", 15, 2)
    myPlayer = Player(20, 4)
    fightMonster(myPlayer, myMonster)
 
playGame()

Comments