Update functions.py
Moved methods of Player class and added to functions. Improved inventory and equipment checking/updating
This commit is contained in:
261
functions.py
261
functions.py
@@ -1,60 +1,7 @@
|
|||||||
import time
|
import time
|
||||||
import json
|
from job_list import Barbarian, Cleric, Wizard
|
||||||
|
from player import *
|
||||||
with open("SRD.json", encoding="utf8") as f:
|
from race_list import Elf, Dwarf, Human
|
||||||
SRD = json.load(f)
|
|
||||||
|
|
||||||
SRDraces = SRD["Races"]
|
|
||||||
SRDraces_list = list(SRD['Races'])
|
|
||||||
|
|
||||||
|
|
||||||
def show_race(race):
|
|
||||||
for x, y in SRDraces[race].items():
|
|
||||||
if isinstance(y, dict):
|
|
||||||
print(f'\n{x}')
|
|
||||||
for a, b in y.items():
|
|
||||||
if not isinstance(b, dict):
|
|
||||||
print(f'{a} - {b}')
|
|
||||||
else:
|
|
||||||
for d, e in b.items():
|
|
||||||
print(f'{d} - {e}')
|
|
||||||
else:
|
|
||||||
print(f'{x} - {y}')
|
|
||||||
|
|
||||||
|
|
||||||
def race_select():
|
|
||||||
race = "None"
|
|
||||||
print("Please select a race to find out more about them:")
|
|
||||||
for i, v in enumerate(SRDraces_list, 1):
|
|
||||||
print(" %d) %s" % (i, v))
|
|
||||||
query = "0"
|
|
||||||
while not query == "1":
|
|
||||||
racial_choice = input(">> ").title()
|
|
||||||
for i, v in enumerate(SRDraces_list, 1):
|
|
||||||
if racial_choice in str(i) or racial_choice == v:
|
|
||||||
for x, y in SRDraces[v].items():
|
|
||||||
if isinstance(y, dict):
|
|
||||||
print(f'\n{x}')
|
|
||||||
for a, b in y.items():
|
|
||||||
if not isinstance(b, dict):
|
|
||||||
print(f'{a} - {b}')
|
|
||||||
else:
|
|
||||||
for d, e in b.items():
|
|
||||||
print(f'{d} - {e}')
|
|
||||||
else:
|
|
||||||
print(f'{x} - {y}')
|
|
||||||
query = input("Would you like to select this as your race or view another?\n"
|
|
||||||
f'1) Select {v} as your race'
|
|
||||||
"2) View the other races")
|
|
||||||
if query == "1":
|
|
||||||
race = v
|
|
||||||
break
|
|
||||||
elif query == "2":
|
|
||||||
print("Please select a race to find out more about them:")
|
|
||||||
for i, v in enumerate(SRDraces_list, 1):
|
|
||||||
print(" %d) %s" % (i, v))
|
|
||||||
continue
|
|
||||||
return race
|
|
||||||
|
|
||||||
|
|
||||||
def wait():
|
def wait():
|
||||||
@@ -67,7 +14,7 @@ def startGame():
|
|||||||
# Initialise age
|
# Initialise age
|
||||||
age = None
|
age = None
|
||||||
# Name
|
# Name
|
||||||
name = input("\n" + "What is your name?" + "\n" + ">> ").title()
|
name = input(f'\nWhat is your name?\n >> ').title()
|
||||||
# Gender
|
# Gender
|
||||||
gender = None
|
gender = None
|
||||||
gender_select = input("What is your gender?\n"
|
gender_select = input("What is your gender?\n"
|
||||||
@@ -88,7 +35,7 @@ def startGame():
|
|||||||
while True:
|
while True:
|
||||||
# Age
|
# Age
|
||||||
try:
|
try:
|
||||||
age = input("How old are you?" + "\n" + ">> ")
|
age = input(f'How old are you?\n >> ')
|
||||||
age = int(age)
|
age = int(age)
|
||||||
break
|
break
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -97,7 +44,7 @@ def startGame():
|
|||||||
# Double check responses
|
# Double check responses
|
||||||
correct = input(f"Hello, {name}. You are a {age} year old {gender}.\nIs this correct? Y/N\n >> ").lower()
|
correct = input(f"Hello, {name}. You are a {age} year old {gender}.\nIs this correct? Y/N\n >> ").lower()
|
||||||
if correct in ['y', 'yes']:
|
if correct in ['y', 'yes']:
|
||||||
break
|
pass
|
||||||
elif correct in ['n', 'no']:
|
elif correct in ['n', 'no']:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
@@ -107,49 +54,55 @@ def startGame():
|
|||||||
break
|
break
|
||||||
elif correct in ['n', 'no']:
|
elif correct in ['n', 'no']:
|
||||||
continue
|
continue
|
||||||
|
break
|
||||||
while True:
|
while True:
|
||||||
# Race
|
# Race
|
||||||
print("Please select a race to find out more about them:")
|
race = input("Please select a race to learn more about it: Elf, Dwarf, or Human.\n >> ").title()
|
||||||
for i, v in enumerate(SRDraces_list, 1):
|
while True:
|
||||||
print(" %d) %s" % (i, v))
|
if race not in ("Elf", "Dwarf", "Human", None):
|
||||||
query = "0"
|
race = input("Sorry I didn't recognise that race. "
|
||||||
while not query == "1":
|
"Please select 'Elf', 'Dwarf', or 'Human'.\n >> ").title()
|
||||||
racial_choice = input(">> ").title()
|
elif race is None:
|
||||||
for i, v in enumerate(SRDraces_list, 1):
|
race = input("Please select a race to learn more about it: Elf, Dwarf, or Human.\n >> ").title()
|
||||||
if racial_choice in str(i) or racial_choice == v:
|
|
||||||
race = v
|
|
||||||
for x, y in SRDraces[v].items():
|
|
||||||
if isinstance(y, dict):
|
|
||||||
print(f'\n{x}')
|
|
||||||
for a, b in y.items():
|
|
||||||
if not isinstance(b, dict):
|
|
||||||
print(f'{a} - {b}')
|
|
||||||
else:
|
|
||||||
for d, e in b.items():
|
|
||||||
print(f'{d} - {e}')
|
|
||||||
else:
|
|
||||||
print(f'{x} - {y}')
|
|
||||||
|
|
||||||
query = input("\nWould you like to select this as your race or view another?\n"
|
|
||||||
f'1) Select {race} as your race\n'
|
|
||||||
"2) View the other races\n"
|
|
||||||
">> ")
|
|
||||||
if query == "1":
|
|
||||||
break
|
|
||||||
elif query == "2":
|
|
||||||
print("Please select a race to find out more about them:")
|
|
||||||
for i, v in enumerate(SRDraces_list, 1):
|
|
||||||
print(" %d) %s" % (i, v))
|
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
print
|
if race == "Elf":
|
||||||
|
print(Elf)
|
||||||
job = input("Please select a class from: Warrior, Ranger, or Mage.\n>> ").lower()
|
elif race == "Dwarf":
|
||||||
while True:
|
print(Dwarf)
|
||||||
if job not in ("warrior", "ranger", "mage"):
|
elif race == "Human":
|
||||||
job = input("Sorry I didn't recognise that class. Please select 'Warrior', 'Ranger', or 'Mage'.\n>> ")
|
print(Human)
|
||||||
else:
|
print('Would you like to proceed with this race or view another?')
|
||||||
|
proceed = input(f'1) Proceed\n2) View another race\n >> ').lower()
|
||||||
|
if proceed in ('1', 'proceed'):
|
||||||
break
|
break
|
||||||
|
elif proceed in ('2', 'view', 'view another', 'view another race'):
|
||||||
|
race = None
|
||||||
|
continue
|
||||||
|
# Job
|
||||||
|
job = input("Please select a job to learn more about it: Barbarian, Cleric, or Wizard.\n >> ").title()
|
||||||
|
while True:
|
||||||
|
if job not in ("Barbarian", "Cleric", "Wizard", None):
|
||||||
|
job = input("Sorry I didn't recognise that job. "
|
||||||
|
"Please select 'Barbarian', 'Cleric', or 'Wizard'.\n >> ").title()
|
||||||
|
elif job is None:
|
||||||
|
job = input("Please select a job to learn more about it: Barbarian, Cleric, or Wizard.\n >> ").title()
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if job == "Barbarian":
|
||||||
|
print(Barbarian)
|
||||||
|
elif job == "Cleric":
|
||||||
|
print(Cleric)
|
||||||
|
elif job == "Wizard":
|
||||||
|
print(Wizard)
|
||||||
|
print('Would you like to proceed with this job or view another?')
|
||||||
|
proceed = input(f'1) Proceed\n2) View another job\n >> ').lower()
|
||||||
|
if proceed in ('1', 'proceed'):
|
||||||
|
break
|
||||||
|
elif proceed in ('2', 'view', 'view another', 'view another job'):
|
||||||
|
job = None
|
||||||
|
continue
|
||||||
|
|
||||||
# Final check
|
# Final check
|
||||||
correct = input(f"{name}, you are a {race} {job}.\nIs this correct? Y/N\n >> ").lower()
|
correct = input(f"{name}, you are a {race} {job}.\nIs this correct? Y/N\n >> ").lower()
|
||||||
if correct in ['y', 'yes']:
|
if correct in ['y', 'yes']:
|
||||||
@@ -169,39 +122,69 @@ def startGame():
|
|||||||
def query_equip():
|
def query_equip():
|
||||||
print("Do you want to:\n"
|
print("Do you want to:\n"
|
||||||
"1) View your equipped items\n"
|
"1) View your equipped items\n"
|
||||||
"2) Change your equipped items")
|
"2) Change your equipped items - Weapons complete\n"
|
||||||
|
"3) Return")
|
||||||
response = input(">> ")
|
response = input(">> ")
|
||||||
if response == "1":
|
if response == "1":
|
||||||
return "view"
|
return "view"
|
||||||
elif response == "2":
|
elif response == "2":
|
||||||
return "change"
|
return "change"
|
||||||
|
elif response == "3":
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def change_equip():
|
def change_equip():
|
||||||
print("Which equipment slot would you like to change?\n"
|
print("Which equipment slot would you like to change?\n"
|
||||||
"1) Weapon\n"
|
"1) Weapon\n"
|
||||||
"2) Armor\n"
|
"2) Armor\n"
|
||||||
"3) Other")
|
"3) Other\n"
|
||||||
|
"4) Return")
|
||||||
response = input(">> ")
|
response = input(">> ")
|
||||||
if response == "1":
|
if response == "1": # Weapon
|
||||||
print(f"You currently have {Player.weapon} equipped.\n"
|
|
||||||
f"What item would you like to swap it for?")
|
|
||||||
for item_key, item_value in Player.inventory.items(): # object, dictionary
|
|
||||||
for key, value in item_value.items(): # string, value (int, class, bool)
|
|
||||||
if (key == "object") and value == Weapon: # if item = class`Weapon`
|
|
||||||
itemlist = [] # create blank list for items
|
itemlist = [] # create blank list for items
|
||||||
for i, itemlist in enumerate(zip(itemlist)):
|
print(f"You currently have equipped: \n"
|
||||||
print(f'{i}) {item_key.name}\n' # print items and their positions
|
f"{view_equippedweapon()}")
|
||||||
f' {item_key.description}')
|
|
||||||
itemlist.append(item_key) # add inventory items to the list
|
for item_key, item_value in Inventory.items(): # item, attribute
|
||||||
selection = input(">> ") # take a number
|
for key, value in item_value.items(): # attribute, stat
|
||||||
selectionint = int(selection) # turn into int
|
if key == "object" and value == Weapon: # if item = class `Weapon`
|
||||||
user.inventory[user.weapon] # add equipped item to inventory
|
itemlist.append(item_key)
|
||||||
user.weapon = itemcount[selectionint] # change equipped item to selection
|
if item_key in Equipment.values():
|
||||||
if selection not in itemcount:
|
itemlist.remove(item_key)
|
||||||
print("That number wasn't an option.")
|
|
||||||
|
print("What would you like to equip?\n")
|
||||||
|
option = ""
|
||||||
|
for i, name in enumerate(itemlist, start=1):
|
||||||
|
option = f'{i}) {name}'
|
||||||
|
print(option)
|
||||||
|
|
||||||
|
selection = int(input(">> "))
|
||||||
|
for i, name in enumerate(itemlist, start=1):
|
||||||
|
if selection == i:
|
||||||
|
if name.itemType == "Weapon":
|
||||||
|
if name.slot == Slots.MainHand:
|
||||||
|
equip = {"Main Hand": name}
|
||||||
|
Equipment.update(equip)
|
||||||
|
elif not name.versatile:
|
||||||
|
equip = {"Off Hand": name}
|
||||||
|
Equipment.update(equip)
|
||||||
else:
|
else:
|
||||||
break
|
equip = {"Main Hand": None, "Off Hand": None, "Two Handed": name}
|
||||||
|
Equipment.update(equip)
|
||||||
|
else:
|
||||||
|
print("That wasn't an option.")
|
||||||
|
print(f"You currently have equipped: \n"
|
||||||
|
f"{view_equippedweapon()}")
|
||||||
|
|
||||||
|
|
||||||
|
def view_equippedweapon():
|
||||||
|
equipped = ""
|
||||||
|
for slot, equipment in Equipment.items():
|
||||||
|
if slot in ("Main Hand", "Off Hand", "Two Handed") and equipment is not None:
|
||||||
|
equipped = equipped + f'{slot}: {equipment} \n'
|
||||||
|
return equipped
|
||||||
|
else:
|
||||||
|
return "You have nothing equipped!"
|
||||||
|
|
||||||
|
|
||||||
def get_instructions():
|
def get_instructions():
|
||||||
@@ -317,6 +300,50 @@ def parse(input_text):
|
|||||||
return command, object1
|
return command, object1
|
||||||
|
|
||||||
|
|
||||||
def skillCheck(skill):
|
def skill_check(skill):
|
||||||
"""Roll a d20 and add modifier
|
"""Roll a d20 and add modifier
|
||||||
Return value to compare vs a hardcoded DC"""
|
Return value to compare vs a hardcoded DC"""
|
||||||
|
|
||||||
|
|
||||||
|
def parse_input(input_string):
|
||||||
|
if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
|
||||||
|
return int(input_string)
|
||||||
|
else:
|
||||||
|
print("Please enter a number from 1 to 6.")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def roll_dice(num_dice):
|
||||||
|
roll_results = []
|
||||||
|
for _ in range(num_dice):
|
||||||
|
roll = randint(*user.hitDie)
|
||||||
|
roll_results.append(roll)
|
||||||
|
return roll_results
|
||||||
|
|
||||||
|
|
||||||
|
def current_inventory():
|
||||||
|
"""Prints a list of items in your backpack that aren't equipped to your person."""
|
||||||
|
print(f'In your rucksack you have:')
|
||||||
|
for item_key, item_value in Inventory.items(): # string, dictionary
|
||||||
|
if item_key not in Equipment.values():
|
||||||
|
values = item_value.values() # make a variable of all values
|
||||||
|
values_list = list(values) # turn variable into a list
|
||||||
|
count_value = values_list[0] # take the first value from list - why?
|
||||||
|
print(f'{item_key.name} x {count_value}\n'
|
||||||
|
f' {item_key.description}')
|
||||||
|
|
||||||
|
|
||||||
|
def current_equipment():
|
||||||
|
"""Prints a list of items that you have equipped in your slots."""
|
||||||
|
print(f'You are currently wearing:')
|
||||||
|
for item_key, item_value in Equipment.items(): # string, dictionary
|
||||||
|
if item_value is not None:
|
||||||
|
if item_value.itemType == "Weapon":
|
||||||
|
print(f'{item_key}: {item_value.name} - {item_value.damage1H}\n'
|
||||||
|
f' {item_value.description}')
|
||||||
|
elif item_value.itemType == "Armor":
|
||||||
|
print(f'{item_key}: {item_value.name} - AC {item_value.ac}\n'
|
||||||
|
f' {item_value.description}')
|
||||||
|
|
||||||
|
def calc_weight():
|
||||||
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user