Project 1
Project 1
import os
from decision_tree import DecisionTree
from save_system import SaveSystem
class StoryEngine:
def __init__(self):
self.decision_tree = DecisionTree()
self.save_system = SaveSystem()
self.load_user_data()
def load_user_data(self):
"""Loads user-defined characters, settings, and themes."""
if os.path.exists("data/characters.json"):
with open("data/characters.json", "r") as file:
self.characters = json.load(file)
else:
self.characters = {}
if os.path.exists("data/settings.json"):
with open("data/settings.json", "r") as file:
self.settings = json.load(file)
else:
self.settings = {}
if os.path.exists("data/themes.json"):
with open("data/themes.json", "r") as file:
self.themes = json.load(file)
else:
self.themes = {}
def start_story(self):
"""Starts the interactive story."""
print("\nWelcome to the Procedural Story Generator!")
# Begin storytelling
while self.decision_tree.current_node:
self.decision_tree.display_node()
self.decision_tree.make_choice()
self.save_system.save_progress(self.decision_tree.current_node)
def main():
engine = StoryEngine()
engine.start_story()
if __name__ == "__main__":
main()
class DecisionTree:
def __init__(self):
# Example decision tree structure
self.story_tree = {
"start": {
"text": "You find yourself in a dark forest. Two paths lie ahead.",
"choices": {
"Take the left path": "left_path",
"Take the right path": "right_path"
}
},
"left_path": {
"text": "The path leads to a hidden village. The villagers seem
friendly.",
"choices": {
"Talk to the village elder": "village_elder",
"Steal from a shop": "caught_stealing"
}
},
"right_path": {
"text": "You walk into a cave filled with glowing crystals.",
"choices": {
"Mine the crystals": "treasure_found",
"Explore deeper": "monster_attack"
}
},
"village_elder": {
"text": "The elder shares a secret about an ancient treasure. You
embark on a quest.",
"choices": {}
},
"caught_stealing": {
"text": "You are caught and thrown out of the village.",
"choices": {}
},
"treasure_found": {
"text": "You discover a hidden treasure and become rich!",
"choices": {}
},
"monster_attack": {
"text": "A monster appears! You barely escape with your life.",
"choices": {}
}
}
self.current_node = "start"
def display_node(self):
"""Displays the current story segment and choices."""
print("\n" + self.story_tree[self.current_node]["text"])
for i, choice in enumerate(self.story_tree[self.current_node]
["choices"].keys(), 1):
print(f"{i}. {choice}")
def make_choice(self):
"""Allows the player to make a decision."""
choices = list(self.story_tree[self.current_node]["choices"].keys())
if not choices:
self.current_node = None # End of story
return
while True:
try:
choice = int(input("\nChoose an option: ")) - 1
if 0 <= choice < len(choices):
self.current_node = self.story_tree[self.current_node]
["choices"][choices[choice]]
break
else:
print("Invalid choice. Try again.")
except ValueError:
print("Please enter a number.")
class SaveSystem:
SAVE_FILE = "save_data.json"
def load_progress(self):
"""Loads saved progress if available."""
if os.path.exists(self.SAVE_FILE):
with open(self.SAVE_FILE, "r") as file:
return json.load(file)
return None
def reset_progress(self):
"""Deletes the save file to start fresh."""
if os.path.exists(self.SAVE_FILE):
os.remove(self.SAVE_FILE)