This project can be viewed better on GitHub:
This project can be viewed better on GitHub:
The goal of this project was to learn Python syntax and understand how to make a basic program. This program is a simple text-based combat game with 5 different encounters.
PyProject.py
import os
from pickle import FALSE
import random
import sys
from Encounter import encounter
name = ""
health = 25
maxHealth = 0
shield = 10
atkPwr = 5
potions = 5
gold = 10
class Player:
def __init__(self, name, health, maxHealth, shield, atkPwr, potions, gold):
self.name = name
self.health = health
self.maxHealth = maxHealth
self.shield = shield
self.atkPwr = atkPwr
self.potions = potions
self.gold = gold
class encounters:
def startEncounterE2S1():
golbin_encounter = encounter("Golbin", 75, 5, 30, 50, 2, 5)
golbin_encounter.start(player, encounters)
def startEncounterE3S1():
skeleton_encounter = encounter("Skeleton", 100, 8, 50, 75, 3, 5)
skeleton_encounter.start(player, encounters)
def startEncounterE4S1():
zombie_encounter = encounter("Zombie", 125, 10, 75, 100, 4, 5)
zombie_encounter.start(player, encounters)
def startEncounterE5S1():
dragon_encounter = encounter("Dragon", 150, 15, 100, 150, 5, 5)
dragon_encounter.start(player, encounters)
#intro to game
#region
hasEnteredName = False
while hasEnteredName == False:
os.system('cls')
print("What is your name: ")
name = input()
if name != '':
hasEnteredName = True
print("\nHello", name)
print("Welcome to the PyProject Testing Environment")
input()
hasChosenAClass = False
while hasChosenAClass == False:
os.system('cls')
print("Please, choose a class to embark on this journey:\n")
print("|========Class Selection========|")
print("| (1) Knight | (2) Alchemist |")
print("| (3) Warrior | (4) Archer |")
print("|-------------------------------|")
print()
classNumber = input()
if classNumber != '':
hasChosenAClass = True
os.system('cls')
if classNumber == '1':
print(f"Congrats {name}, you choose the Knight class!")
maxHealth = 45
shield = 30
gold = 25
atkPwr = 15
if classNumber == '2':
print(f"Congrats {name}, you choose the Alchemist class!")
maxHealth = 40
shield = 15
gold = 15
atkPwr = 13
if classNumber == '3':
print(f"Congrats {name}, you choose the Warrior class!")
maxHealth = 50
shield = 35
gold = 20
atkPwr = 20
if classNumber == '4':
print(f"Congrats {name}, you choose the Archer class!")
maxHealth = 40
shield = 10
gold = 30
atkPwr = 20
input()
os.system('cls')
print("Your stats have been adjusted to the following:\n")
print("|===================|")
print(f"| Health: {maxHealth} |")
print(f"| Shield: {shield} |")
if classNumber != '2':
print(f"| Attack Power: {atkPwr} |")
if classNumber == '2':
print(f"| Attack Power: {atkPwr} |")
print(f"| Gold: {gold} |")
print(f"| Potions: {potions} |")
print("|===================|")
input()
os.system('cls')
print(f"Your task is to clear the Python Dungeon.\nEnjoy {name}, and may luck be on your side!")
print("\nEmbark on your journey? Y/N\n")
inA = input()
if inA == "Y":
print("The journey begins")
if inA == "N":
sys.exit()
input()
os.system('cls')
health = maxHealth
player = Player(name, health, maxHealth, shield, atkPwr, potions, gold)
slime_encounter = encounter("Slime", 50, 3, 20, 30, 1, 5)
slime_encounter.start(player, encounters)
#endregion
Encounter.py
import random
import os
import sys
from Shop import shop
from colorama import Fore, Back, Style, init
class encounter:
def __init__(self, enemyName, enemyHealth, enemyAtkPower, rewardLower, rewardHigher, encounterNum, encounterNumMax):
self.enemyName = enemyName
self.enemyHealth = enemyHealth
self.enemyAtkPower = enemyAtkPower
self.rewardLower = rewardLower
self.rewardHigher = rewardHigher
self.encounterNum = encounterNum
self.encounterNumMax = encounterNumMax
def start(self, Player, encounters):
ranAwayFromEnemy = False
while self.enemyHealth > 0:
if Player.health > Player.maxHealth:
Player.health = Player.maxHealth
if Player.health <= 0:
os.system('cls')
print(Fore.LIGHTRED_EX + "You've died!")
input(Fore.CYAN)
print(Fore.LIGHTRED_EX + "You have failed the journey and thus the experiment. You may close the game now.")
sys.exit()
os.system('cls')
print(Fore.GREEN + f"- Encounter {self.encounterNum} of {self.encounterNumMax} -")
print(Fore.LIGHTRED_EX + f"{self.enemyName}" + Fore.GREEN + f" |" + Fore.LIGHTRED_EX + f" HP: {self.enemyHealth:.2f}" + Fore.GREEN + f" |" +Fore.LIGHTRED_EX + f" Power: {self.enemyAtkPower}")
print(Fore.GREEN + "|-------Actions-------|")
print(Fore.GREEN + f"| (a)ttack | (h)eal |")
print(Fore.GREEN + f"| (g)uard | (r)un |")
print(Fore.GREEN + "|-------Actions-------|")
print(Fore.LIGHTBLUE_EX + f"{Player.name} | HP: {Player.health:.2f} / {Player.maxHealth} | Potions Left: {Player.potions}\n")
en1 = input(Fore.CYAN)
# attack
if en1 == "a":
self.enemyHealth -= Player.atkPwr
if Player.health < (Player.maxHealth/2):
dmg = (self.enemyAtkPower / 0.25)
if Player.health >= (Player.maxHealth / 1.25):
dmg = (self.enemyAtkPower / 0.3)
a_rand = random.randint(1, 2)
if a_rand == 1:
dmg -= random.randint(1, 3)
if a_rand == 2:
dmg += random.randint(1, 3)
Player.health -= dmg
print(Fore.GREEN + f"You deal {Player.atkPwr} damage! However, the {self.enemyName} attacks back dealing {dmg:.2f} damage!")
# heal
if en1 == "h":
if Player.potions <= 0:
Player.potions = 0
print(Fore.LIGHTRED_EX + "You reach for a flask, however, you discover that you have run out!")
if Player.potions > 0:
Player.potions -= 1
if Player.health < (Player.maxHealth / 2):
regainHealth = (Player.health * 0.25 + 5)
if Player.health >= (Player.maxHealth / 2):
regainHealth = (Player.health * 0.25 + 2)
h_rand = random.randint(1, 2)
if h_rand == 1:
regainHealth -= random.randint(1, 2)
if h_rand == 2:
regainHealth += random.randint(1, 2)
Player.health += regainHealth
print(Fore.GREEN + f"You reach for a flask and pop open the cork. Taking a swig you regain {regainHealth:.2f}!")
# guard
if en1 == "g":
g_dmgCalc = self.enemyAtkPower + (self.enemyAtkPower * 2.5)
g_dmgCalc -= Player.shield
if g_dmgCalc < 0:
g_dmgCalc = random.randint(1, 3)
Player.health -= g_dmgCalc
print(Fore.GREEN + f"The {self.enemyName} reaches out, tackling you as hard as it can!\nThe {self.enemyName} deals {g_dmgCalc:.2f} damage!")
# run
if en1 == "r":
canRunAway = random.randint(1, 2)
if canRunAway == 1:
print(Fore.GREEN + f"You ran away from the {self.enemyName} successfully! You move onto the next room.")
input(Fore.CYAN)
ranAwayFromEnemy = True
break
if canRunAway == 2:
print(Fore.LIGHTRED_EX + f"You attempt to sprint past the {self.enemyName} into the next room, however, the {self.enemyName} slaps you back into the far wall and keeps you boxed in!")
r_dmgCalc = self.enemyAtkPower + (self.enemyAtkPower * 2.5)
r_dmgCalc -= Player.shield
if r_dmgCalc < 0:
r_dmgCalc = random.randint(1, 3)
Player.health -= r_dmgCalc
print(Fore.LIGHTRED_EX + f"You take {r_dmgCalc:.2f} damage!")
input(Fore.CYAN)
os.system('cls')
if ranAwayFromEnemy == False:
print(Fore.GREEN + f"Congrats! You beat the {self.enemyName}!")
award = random.randint(self.rewardLower, self.rewardHigher)
Player.gold += award
print(Fore.GREEN + f"You are awarded {award} gold!")
input(Fore.CYAN)
shop(Player, self.encounterNum, encounters)
shop(Player, self.encounterNum, encounters)
Shop.py
import os
import random
from colorama import Fore, Back, Style, init
class shop:
def __init__(self, Player, lastEncounter, encounters):
moveOnEn = False
while moveOnEn == False:
os.system('cls')
print(Fore.GREEN + "SHOP")
print(Fore.LIGHTBLUE_EX + "|-----------------Actions-----------------|")
print(Fore.LIGHTBLUE_EX + f"| (h) increase max health - 10 gold |")
print(Fore.LIGHTBLUE_EX + f"| (g) increase guard points - 12 gold |")
print(Fore.LIGHTBLUE_EX + f"| (a) increase attack power - 15 gold |")
print(Fore.LIGHTBLUE_EX + f"| (p) buy potions (x1) - 5 gold |")
print(Fore.LIGHTBLUE_EX + f"| (m) move onto next encounter |")
print(Fore.LIGHTBLUE_EX + "|-----------------Actions-----------------|")
print(Fore.GREEN + f"{Player.name} | HP: {Player.health:.2f} / {Player.maxHealth:.2f} | Shield: {Player.shield:.2f} | Attack Power: {Player.atkPwr:.2f} \n Potions Left: {Player.potions} | Gold: {Player.gold}\n")
shopEn1 = input(Fore.CYAN)
# increase max health
if shopEn1 == "h":
increaseHealth = (Player.maxHealth * 0.25) + random.randint(5, 10)
Player.maxHealth += increaseHealth
Player.gold -= 10
print(Fore.GREEN + f"Your max health has been increased by {increaseHealth:.2f}")
# increase guard points/shield
if shopEn1 == "g":
increaseGuard = (Player.shield * 0.25) + random.randint(4, 8)
Player.shield += increaseGuard
Player.gold -= 12
print(Fore.GREEN + f"Your guard points has been increased by {increaseGuard:.2f}")
# increase attack power
if shopEn1 == "a":
increaseAttackPower = (Player.atkPwr * 0.25) + random.randint(2, 6)
Player.atkPwr += increaseAttackPower
Player.gold -= 15
print(Fore.GREEN + f"Your attack power has been increased by {increaseAttackPower:.2f}")
# buy potions
if shopEn1 == "p":
doublePotions = random.randint(1, 2)
Player.gold -= 5
if doublePotions == 1:
Player.potions += 1
print(Fore.GREEN + "You have bought 1 potion")
if doublePotions == 2:
Player.potions += 2
print(Fore.GREEN + "Congrats! The shopkeeper decided to give you a 2nd potion for the price of 1!")
# move onto next encounter
if shopEn1 == "m":
moveOnEn = True
input(Fore.CYAN)
# move onto next encounter
if lastEncounter == 1:
encounters.startEncounterE2S1()
if lastEncounter == 2:
encounters.startEncounterE3S1()
if lastEncounter == 3:
encounters.startEncounterE4S1()
if lastEncounter == 4:
encounters.startEncounterE5S1()