Created
June 17, 2017 15:11
-
-
Save DemiKara/f99c87d6d5db02319c075daabf7dc938 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pprint | |
from .magic import Spell | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
class Person: | |
def __init__(self, name, hp, mp, atk, df, magic, items): | |
self.maxhp = hp | |
self.hp = hp | |
self.maxmp = mp | |
self.mp = mp | |
self.atkl = atk - 10 | |
self.atkh = atk + 10 | |
self.df = df | |
self.magic = magic | |
self.items = items | |
self.actions = ["Attack", "Magic", "Items"] | |
self.name = name | |
def generate_damage(self): | |
return random.randrange(self.atkl, self.atkh) | |
def take_damage(self, dmg): | |
self.hp -= dmg | |
if self.hp < 0: | |
self.hp = 0 | |
return self.hp | |
def heal(self, dmg): | |
self.hp += dmg | |
if self.hp > self.maxhp: | |
self.hp = self.maxhp | |
def get_hp(self): | |
return self.hp | |
def get_max_hp(self): | |
return self.maxhp | |
def get_mp(self): | |
return self.mp | |
def get_max_mp(self): | |
return self.maxmp | |
def reduce_mp(self, cost): | |
self.mp -= cost | |
def choose_action(self): | |
i = 1 | |
print("\n" +" " + bcolors.BOLD + self.name + bcolors.ENDC) | |
print(bcolors.OKBLUE + bcolors.BOLD +" ACTIONS:"+bcolors.ENDC) | |
for item in self.actions: | |
print(" " + str(i)+ ".", item) | |
i += 1 | |
def choose_magic(self): | |
i=1 | |
print("\n" + bcolors.OKBLUE + bcolors.BOLD +" MAGIC"+bcolors.ENDC) | |
for spell in self.magic: | |
print(" " + str(i)+".", spell.name,"(cost:", str(spell.cost)+")") | |
i += 1 | |
def choose_item(self): | |
i = 1 | |
print("\n" + bcolors.OKGREEN + bcolors.BOLD + " ITEMS:" + bcolors.ENDC) | |
for item in self.items: | |
print(" " + str(i)+ ".", item["item"].name,":", item["item"].description, " (x" +str(item["quantity"])+")") | |
i += 1 | |
def choose_target(self, enemies): | |
i = 1 | |
print("\n" + bcolors.FAIL + bcolors.BOLD + " TARGET:" + bcolors.ENDC) | |
for enemy in enemies: | |
if enemy.get_hp() != 0: | |
print (" " +str(i) + ".", enemy.name) | |
i += 1 | |
choice = int(input(" Choose target:"))-1 | |
return choice | |
def get_enemy_stats(self): | |
hp_bar = "" | |
bar_ticks = (self.hp / self.maxhp) *100 / 2 | |
while bar_ticks > 0: | |
hp_bar += "█" | |
bar_ticks -= 1 | |
while len(hp_bar) <50: | |
hp_bar += " " | |
hp_string = str(self.hp) + "/" + str(self.maxhp) | |
current_hp = "" | |
if len(hp_string) < 11: | |
decreased = 11 - len(hp_string) | |
while decreased > 0: | |
current_hp += " " | |
decreased -= 1 | |
current_hp += hp_string | |
else: | |
current_hp = hp_string | |
print(self.name + " " | |
+ current_hp + "|" + bcolors.FAIL + hp_bar + bcolors.ENDC + "|") | |
def get_stats(self): | |
hp_bar = "" | |
bar_ticks = (self.hp / self.maxhp) * 100 / 4 | |
mp_bar = "" | |
mp_ticks = (self.mp / self.maxmp) * 100 / 10 | |
while bar_ticks >0: | |
hp_bar += "█" | |
bar_ticks -=1 | |
while len(hp_bar) < 25: | |
hp_bar += " " | |
while mp_ticks > 0: | |
mp_bar += "█" | |
mp_ticks -= 1 | |
while len(mp_bar) <10: | |
mp_bar += " " | |
hp_string = str(self.hp) + "/" + str(self.maxhp) | |
current_hp = "" | |
if len(hp_string) < 9: | |
decreased = 9 - len(hp_string) | |
while decreased > 0 : | |
current_hp += " " | |
decreased -= 1 | |
current_hp += hp_string | |
else: | |
current_hp = hp_string | |
mp_string = str(self.mp) + "/" + str(self.maxmp) | |
current_mp = "" | |
if len(mp_string) < 7: | |
decreased = 7-len(mp_string) | |
while decreased > 0: | |
current_mp += " " | |
decreased -= 1 | |
current_mp += mp_string | |
else: | |
current_mp = mp_string | |
print(self.name + " " | |
+ current_hp + "|" + bcolors.OKGREEN + hp_bar + bcolors.ENDC + "| " + | |
current_mp + "|" + bcolors.OKBLUE + mp_bar + bcolors.ENDC + "|") | |
def choose_enemy_spell(self): | |
magic_choice = random.randrange(0, len(self.magic)) | |
spell = self.magic[magic_choice] | |
magic_dmg = spell.generate_damage() | |
pct = self.hp / self.maxhp *100 | |
if self.mp < spell.cost or spell.type == "white" and pct > 50: | |
self.choose_enemy_spell() | |
else: | |
return spell, magic_dmg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item: | |
def __init__(self, name, type, description, prop): | |
self.name = name | |
self.type = type | |
self.description = description | |
self.prop = prop | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Spell: | |
def __init__(self, name, cost, dmg, type): | |
self.name = name | |
self.cost = cost | |
self.dmg = dmg | |
self.type = type | |
def generate_damage(self): | |
low = self.dmg - 15 | |
high = self.dmg +15 | |
return random.randrange(low, high) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from classes.game import Person, bcolors | |
from classes.magic import Spell | |
from classes.inventory import Item | |
import random | |
#Create black magic | |
fire = Spell("Fire", 25, 600, "black") | |
thunder = Spell("Thunder", 25, 600, "black") | |
blizzard = Spell("Blizzard", 25, 600, "black") | |
meteor = Spell("Meteor", 40, 1200, "black") | |
quake = Spell("Quake", 14, 140, "black") | |
#Create white magic | |
cure = Spell("Cure", 25, 620, "white") | |
cura = Spell("Cura", 32, 1500, "white") | |
curaga = Spell("Curaga", 50, 6000, "white") | |
#create some Items | |
potion = Item("Potion", "potion", "Heals 50 HP", 50) | |
hipotion = Item("Hi-Potion", "potion", "Heals 100 HP", 100) | |
superpotion = Item("Super Potion", "potion", "Heals 1000 HP", 1000) | |
elixer = Item("Elixer", "elixer", "Fully restore HP/MP of one party member", 9999) | |
hielixer = Item("MegaElixer", "elixer","Fully restore party's HP/MP", 9999) | |
grenade = Item("Grenade", "attack", "Deals 500 damage", 500) | |
player_spells = [fire, thunder, blizzard, meteor, cure, cura] | |
enemy_spells = [fire, meteor,curaga] | |
player_items = [{"item":potion, "quantity":15}, {"item":hipotion, "quantity":5}, | |
{"item":superpotion, "quantity":5}, {"item":elixer, "quantity":5}, | |
{"item":hielixer, "quantity":2}, {"item":grenade, "quantity":5}] | |
#Instatiate People | |
player1 = Person("Valos:", 3260, 132, 300, 34, player_spells, player_items) | |
player2 = Person("Nick :", 4160, 188, 311, 34, player_spells, player_items) | |
player3 = Person("Robot:", 3089, 174, 288, 34, player_spells, player_items) | |
enemy1 = Person(" ImpA ", 1250, 130, 560, 325, enemy_spells,[]) | |
enemy2 = Person("Magus", 18200, 701, 525, 25, enemy_spells, []) | |
enemy3 = Person(" ImpB ", 1250, 130, 560, 325, enemy_spells,[]) | |
players = [player1, player2, player3] | |
enemies = [enemy1, enemy2, enemy3] | |
running = True | |
i=0 | |
print(bcolors.FAIL + bcolors.BOLD + "AN ENEMY ATTACKS!" + bcolors.ENDC) | |
while running: | |
print("========================================") | |
print("\n\n") | |
print(" NAME HP MP") | |
for player in players: | |
player.get_stats() | |
print("\n") | |
for enemy in enemies: | |
enemy.get_enemy_stats() | |
for player in players: | |
player.choose_action() | |
choice = input("Choose action:") | |
index = int(choice) - 1 | |
if index == 0: | |
dmg = player.generate_damage() | |
enemy = player.choose_target(enemies) | |
enemies[enemy].take_damage(dmg) | |
print(player.name, " attacks " + enemies[enemy].name.replace(" ", "") + " for", dmg, "points of damage.") | |
if enemies[enemy].get_hp() ==0: | |
print(enemies[enemy].name.replace(" ","") + " has died.") | |
del enemies[enemy] | |
elif index == 1: #Magic Things! | |
player.choose_magic() | |
magic_choice = int(input("Choose magic:"))-1 | |
if magic_choice == -1: | |
continue | |
spell = player.magic[magic_choice] | |
magic_dmg = spell.generate_damage() | |
current_mp = player.get_mp() | |
if spell.cost > current_mp: | |
print(bcolors.FAIL+ "\nNot enough MP\n" +bcolors.ENDC) | |
continue | |
player.reduce_mp(spell.cost) | |
if spell.type == "white": | |
player.heal(magic_dmg) | |
print(bcolors.OKBLUE + "\n" + spell.name + " heals for", str(magic_dmg), "HP." + bcolors.ENDC) | |
elif spell.type == "black": | |
enemy = player.choose_target(enemies) | |
enemies[enemy].take_damage(magic_dmg) | |
print(bcolors.OKBLUE + "\n" + spell.name +" deals" , str(magic_dmg), "points of damage to " + enemies[enemy].name.replace(" ","") +bcolors.ENDC) | |
if enemies[enemy].get_hp == 0: | |
print(enemies[enemy].name.replace(" ","") + " has died.") | |
del enemies[enemy] | |
elif index == 2: | |
player.choose_item() | |
item_choice = int(input("Choose item: "))-1 | |
if item_choice == -1: | |
continue | |
item = player.items[item_choice]["item"] | |
if player.items[item_choice]["quantity"] ==0: | |
print(bcolors.FAIL + "\n" + "None left..." +bcolors.ENDC) | |
continue | |
player.items[item_choice]["quantity"] -=1 | |
if item.type == "potion": | |
player.heal(item.prop) | |
print(bcolors.OKGREEN+ "\n" + item.name + " heals for", str(item.prop), "HP" + bcolors.ENDC) | |
elif item.type == "elixer": | |
if item.name == "MegaElixer": | |
for i in players: | |
i.hp = i.maxhp | |
i.mp = i.maxmp | |
else: | |
player.hp = player.maxhp | |
player.mp = player.maxmp | |
print(bcolors.OKGREEN + "\n" + item.name + " fully restores HP/MP" + bcolors.ENDC) | |
elif item.type =="attack": | |
enemy = player.choose_target(enemies) | |
enemies[enemy].take_damage(item.prop) | |
print(bcolors.FAIL + "\n"+ item.name + " deals", str(item.prop), "points of damage to " | |
+ enemies[enemy].name + bcolors.ENDC) | |
if enemies[enemy].get_hp ==0: | |
print(enemies[enemy].name.replace(" ","") + " has died.") | |
del enemies[enemy] | |
# Check if Battle is over | |
defeated_enemies = 0 | |
defeated_players = 0 | |
for enemy in enemies: | |
if enemy.get_hp() == 0: | |
defeated_enemies += 1 | |
for player in players: | |
if player.get_hp() == 0: | |
defeated_players += 1 | |
#check if player won | |
if defeated_enemies == 3: | |
print(bcolors.OKGREEN + "You win!" + bcolors.ENDC) | |
running = False | |
#check if enemy won | |
elif defeated_players == 3: | |
print(bcolors.FAIL + "Your enemies have defeated you!" + bcolors.ENDC) | |
running = False | |
#Enemy attack phase | |
for enemy in enemies: | |
enemy_choice = random.randrange(0,2) | |
if enemy_choice == 0 : | |
#chose attack | |
target = random.randrange(0,3) | |
enemy_dmg = enemy.generate_damage() | |
players[target].take_damage(enemy_dmg) | |
print(enemy.name.replace(" ", "") + " attacks" + players[target].name.replace(" ", ""), "deals", enemy_dmg, "damage") | |
elif enemy_choice == 1: | |
#getting an error message to do with this line | |
spell, magic_dmg = enemy.choose_enemy_spell() | |
enemy.reduce_mp(spell.cost) | |
if spell.type == "white": | |
enemy.heal(magic_dmg) | |
print(bcolors.OKBLUE + spell.name + " heals " + enemy.name + " for", str(magic_dmg), "HP." + bcolors.ENDC) | |
elif spell.type == "black": | |
target = random.randrange(0, 3) | |
players[target].take_damage(magic_dmg) | |
print(bcolors.OKBLUE + "\n" + enemy.name.replace(" ", "") + "'s " + spell.name + " deals", str(magic_dmg), | |
"points of damage to " + players[target].name.replace(" ", "") + bcolors.ENDC) | |
if players[target].get_hp() == 0: | |
print(players[target].name.replace(" ", "") + " has died.") | |
del players[player] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment