Files
Kanjin/Kanjin-Engine
2022-01-27 17:35:17 +13:00

552 lines
18 KiB
Plaintext

import time
from functions import startGame, wait, parse, process_command
from random import randint
from enum import Enum, auto
# pyinstaller --onefile --paths=C:\Users\kasai\PycharmProjects\pythonProject\Kanjin\KanjinEngine.py
class Player:
"""Character Creation"""
# Default Character
def __init__(self, name, gender, age, race, job):
# Personality
self.name = name
self.gender = gender
self.age = age
self.race = race
self.job = job
# Health
self.hitDie = randint(1, 6)
self.currentHP = 100
self.maxHP = 100
# Levels
self.level = 1
self.exp = 0
self.maxEXP = 100
# Magic
self.spells_known = []
self.spells_ready = []
# Equipment
self.weapon = rock
self.armor = tornRags
# Defence
self.ac = 0
self.elemental_resistance = {DamageType.FIRE: False, DamageType.WATER: False, DamageType.LIGHTNING: False}
self.physical_resistance = {DamageType.SLASHING: False, DamageType.CRUSHING: False, DamageType.PIERCING: False}
# Ability Scores
self.stats = {"Strength": 0,
"Dexterity": 0,
"Constitution": 0,
"Intelligence": 0,
"Wisdom": 0,
"Charisma": 0}
# Ability Modifiers
self.mods = {"Strength": 0,
"Dexterity": 0,
"Constitution": 0,
"Intelligence": 0,
"Wisdom": 0,
"Charisma": 0}
# Held items
self.inventory = {rock: {"Count": 1,
"object": Weapon,
"equipped": True
},
tornRags: {"Count": 1,
"object": Armor,
"equipped": True
},
GP1: {"Count": 10,
"object": Money,
"equipped": False
},
}
def getModifier(self):
self.mods["Strength"] = -5 + self.stats["Strength"] // 2
self.mods["Dexterity"] = -5 + self.stats["Dexterity"] // 2
self.mods["Constitution"] = -5 + self.stats["Constitution"] // 2
self.mods["Intelligence"] = -5 + self.stats["Intelligence"] // 2
self.mods["Wisdom"] = -5 + self.stats["Wisdom"] // 2
self.mods["Charisma"] = -5 + self.stats["Charisma"] // 2
def set_stats_race(self):
if self.race.lower() == "human":
self.stats["Strength"] += 1,
self.stats["Dexterity"] += 1,
self.stats["Constitution"] += 1,
self.stats["Intelligence"] += 1,
self.stats["Wisdom"] += 1,
self.stats["Charisma"] += 1
elif self.race.lower() == "elf":
self.stats["Dexterity"] += 2
self.stats["Intelligence"] += 1
elif self.race.lower() == "dwarf":
self.stats["Constitution"] += 2
self.stats["Wisdom"] += 1
def set_stats_job(self):
if self.job.lower() == "warrior":
self.hitDie = randint(1, 12)
self.maxHP = (12 + self.mods["Constitution"])
self.currentHP = (12 + self.mods["Constitution"])
elif self.job.lower() == "ranger":
self.hitDie = randint(1, 10)
self.maxHP = (10 + self.mods["Constitution"])
self.currentHP = (10 + self.mods["Constitution"])
elif self.job.lower() == "mage":
self.hitDie = randint(1, 8)
self.maxHP = (8 + self.mods["Constitution"])
self.currentHP = (8 + self.mods["Constitution"])
def current_stats(self):
"""Prints a display of the user's current statistics."""
print("Your current stats are:")
print(f'Hit Points: {self.currentHP}/{self.maxHP}')
print(f'Strength: {self.stats["Strength"]}')
print(f'Dexterity: {self.stats["Dexterity"]}')
print(f'Constitution: {self.stats["Constitution"]}')
print(f'Intelligence: {self.stats["Intelligence"]}')
print(f'Wisdom: {self.stats["Wisdom"]}')
print(f'Charisma: {self.stats["Charisma"]}')
def current_equip(self):
"""Prints a list of items currently equipped."""
print(f"You have equipped:\n {self.armor.name}\n"
f" {self.armor.description}"
f"\n{self.weapon.name}\n"
f" {self.weapon.description}\n")
def current_inventory(self):
"""Prints a list of items in your backpack that aren't equipped to your person."""
print(f'In your rucksack you have:\n')
for item_key, item_value in self.inventory.items():
for key, value in item_value.items():
if key == "equipped" and value == False:
values = item_value.values()
values_list = list(values)
count_value = {values_list[0]}
print(f'{item_key.name} x {count_value}\n'
f' {item_key.description}\n')
def add_inventory(self, object1):
self.inventory[object1] = {"Count": object1.count,
"object": "",
"equipped": False}
return f'{object1} has been added to your rucksack.'
def new_char(self):
"""Outputs final stats, armour, and inventory"""
self.allocation()
self.set_stats_race()
self.set_stats_job()
self.getModifier()
self.current_stats()
print(' ')
time.sleep(1)
self.current_equip()
print(' ')
self.current_inventory()
print(' ')
def health_check(self):
"""Print out current/max HP"""
print(f'You have {self.currentHP}/{self.maxHP} HP.')
def take_damage(self, DMGtype, size):
"""Define the damage type and dice size"""
damage = randint(1, size)
if DMGtype in self.elemental_resistance or DMGtype in self.physical_resistance:
damage //= 2
self.currentHP -= damage
return damage
def allocation(self):
rolls = []
stats = []
attributes = ["Strength", "Dexterity", "Constitution", "Intelligence", "Wisdom", "Charisma"]
while len(stats) != 6:
# roll 4d6
for roll in range(4):
r = randint(1, 6)
if r < 6:
r = randint(1, 6)
rolls.append(r)
# Drop the lowest number and then add to single value
del rolls[rolls.index(min(rolls))]
val = sum(rolls)
stats.append(str(val))
rolls = []
print("Please assign a stat to a selected attribute by entering the number then the attribute.\n"
"For example, '10 strength' will assign the stat 10 to your strength, if you have a 10 showing.\n")
print("Please select a stat and an attribute.\n\n")
print("Your rolled stats are:\n" + ', '.join(stats) + "\n\n")
print("Your attributes are:\n" + ', '.join(attributes) + "\n\n")
# Allocate
while len(stats) != 0:
input_text = input(">> ").title()
words = input_text.split()
if words[0] in stats and words[1] in attributes:
self.stats[words[1]] += int(words[0])
stats.remove(words[0])
attributes.remove(words[1])
if len(stats) == 0:
print("")
break
print("The remaining options are:\n")
print(', '.join(stats))
print(', '.join(attributes))
else:
print("Error. Input was not recognised. Please try again with 'Number' + 'Attribute'.")
continue
class Scene(object):
def __init__(self):
self.objects = []
# def enter(self):
# print(self.name) # works
# print(self.descrip) # works
# # this applies to all classes under Scene, but Zed does it differently
class Engine(object):
def __init__(self, scene_map):
self.name = "name"
self.descrip = "descrip"
self.scene_map = scene_map
# gets the game's map from instance "mymap" at bottom of this file
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('Finished')
# see the Map object: runs function named opening_scene()
# this runs only once
# this STARTS THE GAME
# this (below) is supposed to be an infinite loop (but mine is not)
while not last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
print("\n--------")
current_scene.enter()
# note: will throw error if no new scene passed in by next line:
next_scene_name = current_scene.action()
# get the name of the next scene from the action() function that
# runs in the current scene - what it returns
current_scene = self.scene_map.next_scene(next_scene_name)
# here we use that val returned by current scene to go to
# the next scene, running function in Map
class DamageType(Enum):
SLASHING = auto()
CRUSHING = auto()
PIERCING = auto()
FIRE = auto()
WATER = auto()
LIGHTNING = auto()
class DamageMod(Enum):
STRENGTH = auto()
DEXTERITY = auto()
CONSTITUTION = auto()
INTELLIGENCE = auto()
WISDOM = auto()
CHARISMA = auto()
class Item:
"""The base class for all items"""
def __init__(self):
self.name = ""
self.description = "Not much to look at."
self.use_description = "Nothing happens."
self.aliases = []
self.value = ""
self.magical = None
def __repr__(self):
return f"{self.name}\n=====\n{self.description}\nValue: {self.value}\n"
class Money(Item):
"""The currency item used in the world of Kanjin"""
def __init__(self):
super().__init__()
self.name = ""
self.description = f"A small round coin made of {self.name.lower()} " \
f"with the imperial city logo stamped on the face."
self.amt = ""
self.magical = None
class Weapon(Item):
"""The base class for all weapons"""
def __init__(self):
super().__init__()
self.damage1H = None
self.damage2H = None
self.dmgType = None
self.dmgMod = None
self.versatile = None
self.thrown = None
def __repr__(self):
return f"{self.name}\n=====\n{self.description}\nValue: {self.value}" \
f"\nDamage: One Handed - {self.damage1H}" \
f"\nDamage: Two Handed - {self.damage2H}\n" \
f"Damage Type: {self.dmgType}\nMagical: {self.magical}.\n"
class Armor(Item):
"""The base class for all armor"""
def __init__(self):
self.grade = None
self.ac = None
self.stealthDis = None
self.dmgRes = None
super().__init__()
def __repr__(self):
if self.stealthDis:
return f"{self.name}\n=====\n{self.description}\nValue: {self.value}\n" \
f"AC: {self.ac}\nMagical: {self.magical}"
elif not self.stealthDis:
return f"{self.name}\n=====\n{self.description}\nValue: {self.value}\n" \
f"AC: {self.ac}\nDisadvantage on Stealth checks: {self.stealthDis}\nMagical: {self.magical}"
# Different types of coins
GP1 = Money()
GP1.name = "Gold"
GP1.amt = 1
GP1.magical = False
SP1 = Money()
SP1.name = "Silver"
SP1.amt = 1
SP1.magical = False
CP1 = Money()
CP1.name = "Copper"
CP1.amt = 1
CP1.magical = False
# starting equip
rock = Weapon()
rock.name = "Rock"
rock.description = "A fist-sized rock, suitable for bludgeoning."
rock.value = "No value"
rock.damage1H = randint(1, 6)
rock.damage2H = 0
rock.dmgType = DamageType.CRUSHING
rock.dmgMod = DamageMod.STRENGTH
rock.vers = False
rock.thrown = True
rock.magical = False
tornRags = Armor()
tornRags.name = "Torn Rags"
tornRags.description = "A ripped and worn-out outfit."
tornRags.value = 0
tornRags.grade = "Light"
tornRags.ac = 0
tornRags.disadvantage = True
tornRags.dmgRes = "None"
tornRags.magical = False
user = Player("Jamie", "Male", 29, "Elf", "Mage")
# user = Player(*startGame())
# enter() needs to return a map name.
class Begin(Scene):
name = "Kanjin - An RPG Text Adventure"
desc = "Welcome to Kanjin.\nThis game is based off of the D&D SRD ruleset, and will see you, our adventurer, " \
"exploring, adventuring, and plundering.\n\nTo play Kanjin is very simple.\nRead the instructions, " \
"understand the scene, and take control.\nAll directional queues will be given and clear but you " \
"can also use specific commands at almost anytime when you give your input.\n" \
"After character creation, type 'Help' to view a list of commands and explanations."
def enter(self):
print(f'\n===========\n'
f'{self.name}')
print(f'===========\n'
f'{self.desc}')
wait()
user.new_char()
return "Cave entrance"
class CaveEntrance(Scene):
name = "Cave Entrance"
descTrue = "A small broken statue at the mouth of a cave marks the entrance to a dungeon.\n" \
"Beyond the broken statue lies a massive, rugged room, too dark to see into.\n" \
"You make out the silhouette of broken pottery sprawled around.\n" \
"\nBehind you is the forest you traversed through to get here, about 1500m away from the road.\n" \
"To the east, a canyon. Too wide to cross and too deep to see the bottom.\n" \
"On the west is a mountain face. You are not prepared to take that on.\n"
descFalse = "You return to the cave entrance with the road behind you, a canyon to the east, " \
"and a mountain face on the right."
has_visited = False
def enter(self):
print(f'\n===========\n'
f'{self.name}')
if not self.has_visited:
print(self.descTrue)
else:
print(self.descTrue)
action = input(">> ")
if action == "shoot!":
return "death"
elif action == "dodge":
return "pod"
elif action == "start game":
return "begin"
else:
print(process_command(*parse(action)))
# def action(self):
# pass
class A1(Scene):
name = "Laser Weapon Armory"
descrip = "Shelves and cases line the walls of this room. Weapons of every description " \
"fill the shelves and cases. There is a digital keypad set into the wall."
def enter(self):
print(f'\n===========\n'
f'{self.name}'
f'\n===========\n'
f'{self.descrip}')
def action(self):
pass
# class Death(Scene):
#
# def __init__(self):
# self.name = "You have died!"
# self.descrip = "Your spirit leaves swiftly as your body collapses."
#
# quips = [
# "You died. You kinda suck at this.",
# "Your Mom would be proud...if she were smarter.",
# "Such a loser.",
# "I have a small puppy that's better at this.",
# "You're worse than your Dad's jokes."
# ]
#
# def enter(self):
# print(Death.quips[randint(0, len(self.quips) - 1)])
# exit(1)
#
# def action(self):
# pass
#
#
# class TheBridge(Scene):
#
# def __init__(self):
# self.name = "The Bridge"
# self.descrip = "Clearly this is a central command station of the spaceship. A wide view screen shows the" \
# " stars against a black curtain of empty space."
#
# def enter(self):
# print(f'\n===========\n'
# f'{self.name}'
# f'\n===========\n'
# f'{self.descrip}')
#
# def action(self):
# pass
class Tutorial(Scene):
name = "Tutorial Level"
descrip = ("There are certain commands that will be available almost anytime you are able to type,\n"
"such as viewing your inventory, checking your equipped items, and also changing them.\n"
"You can also view your stats including your current and max hp.\n"
"Give it a go starting with 'Check inventory', 'Check equipment', and 'View stats'."
"See what else you can do, then enter 'continue' to move to the next step.")
def enter(self):
print(f'\n===========\n'
f'{self.name}'
f'\n===========\n'
f'{self.descrip}')
action = input(">> ")
if action != "continue":
pass
else:
process_command(*parse(action))()
# if action == "shoot!":
# print("result")
# return "death"
# elif action == "dodge":
# print("result")
# return "pod"
# else:
# print("error")
# return "central corridor"
# Map tells us where we are and where we can go
# it does not make us move - Engine does that
class Map(object):
scenes = {
'cave entrance': CaveEntrance(),
'A1': A1(),
# 'death': Death(),
# 'bridge': TheBridge(),
'tutorial': Tutorial(),
"begin": Begin()
}
def __init__(self, start_scene_key):
self.start_scene_key = start_scene_key
# above we make a local var named start_scene_key
# start_scene_key remains unchanged throughout the game
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
# above is how we get value out of the dictionary named scenes
return val
# this function can be called repeatedly in the game,
# unlike opening_scene, which is called only ONCE
def opening_scene(self):
return self.next_scene(self.start_scene_key)
seed = "tutorial" # starts the game
myMap = Map(seed) # instantiate a new Map object w/ one arg
myGame = Engine(myMap) # instantiate a new Engine object w/ one arg
myGame.play() # call function from that Engine instance