Code Cleanup
I've cleaned up your code a bit, created classes and functions to reduce the copy-pasted code and provide a much clearer functionality.
import random
import time
def GetChoice(message, choices):
success = False
while (not success):
choice = input(message + " " + str(choices) + ": ").strip().upper()
success = choice in choices
return choice
class Player:
name = "yourself"
maxHp = 50
hp = 50
inventory = "empty"
class Weapon:
def CanHit(self):
return random.randint(0, 100) < self.hitChance
def TryHit(self, enemy):
if self.CanHit():
self.HitEnemy(enemy)
enemy.TakeDamage(self.damage)
else:
print("You missed!")
class Sword(Weapon):
hitChance = 85
damage = 10
def HitEnemy(self, enemy):
print("You slash through the", enemy.name, "dealing", self.damage, "damage!")
class Bow(Weapon):
hitChance = 75
damage = 8
def HitEnemy(self, enemy):
print("You plunge your arrow into the", enemy.name, "dealing", self.damage, "damage!")
class Magic:
def CanCast(self):
return random.randint(0, 100) < self.castChance
def TryCast(self, target):
if self.CanCast():
self.CastOnTarget(target)
else:
print("You missed!")
class Fireball(Magic):
castChance = 65
damage = 15
def CastOnTarget(self, enemy):
print("Forming a ball of fire in your hand you hurl it at the", enemy.name, "dealing", self.damage, "damage!")
enemy.TakeDamage(self.damage)
class Healing(Magic):
castChance = 100
healMin = 5
healMax = 15
def CastOnTarget(self, target):
if target.maxHp > target.hp:
heal = random.randint(5, 15)
if heal > target.maxHp - target.hp:
heal = target.maxHp - target.hp
print("You have healed", target.name, "for", heal, "HP.")
else:
print("Already at maximum health.")
class Enemy:
def __init__(self, name):
self.name = name
def TakeDamage(self, damage):
self.hp = self.hp - damage
class Beatle(Enemy):
hp = 20
def __init__(self):
Enemy.__init__(self, "Beatle")
class Goblin(Enemy):
hp = 50
def __init__(self):
Enemy.__init__(self, "Goblin")
class Kobold(Enemy):
hp = 100
def __init__(self):
Enemy.__init__(self, "Kobold")
print ("welcome to Ryder's house of monstrositys!!!")
time.sleep (2)
print ("can you escape?")
time.sleep (1)
difficulty = GetChoice("What difficulty would you like?", ["EASY", "NORMAL", "HARD"])
print ("... preparing dungeon ...")
time.sleep (2)
counter = 3
while counter > 0:
print (".")
time.sleep (0.5)
counter = counter - 1
if difficulty == "EASY":
enemy = Beatle()
elif difficulty == "NORMAL":
enemy = Goblin()
elif difficulty == "HARD":
enemy = Kobold()
print("First encounter: " + enemy.name + "!")
choice = GetChoice("What would you like to do?", ["RUN", "FIGHT"])
if choice == "RUN":
print ("GAME OVER!")
exit ()
elif choice == ("FIGHT"):
print ("Battle Begin!")
print ("Your Turn!")
player = Player()
choice = GetChoice("What would you like to do?", ["ATTACK", "MAGIC", "INVENTORY", "RETREAT"])
if choice == "ATTACK":
choice = GetChoice("What weapon would you like to use?", ["SWORD", "BOW"])
if choice == "SWORD":
weapon = Sword()
elif choice == "BOW":
weapon = Bow()
else:
print("[Error] Unknown weapon: " + choice)
weapon.TryHit(enemy)
elif choice == "MAGIC":
choice = GetChoice("Which spell would you like to cast?", ["FIRE", "HEAL"])
if choice == "FIRE":
fireball = Fireball()
fireball.TryCast(enemy)
elif choice == "HEAL":
healing = Healing()
healing.TryCast(player)