0% found this document useful (0 votes)
3 views3 pages

Project 1

The document outlines a Python program for a procedural story generator that utilizes a decision tree structure for interactive storytelling. It includes classes for managing the story engine, decision tree, and save system, allowing users to create and navigate through a story while saving their progress. The program prompts users to make choices that influence the narrative and can resume from saved points.

Uploaded by

Lovely Laddu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Project 1

The document outlines a Python program for a procedural story generator that utilizes a decision tree structure for interactive storytelling. It includes classes for managing the story engine, decision tree, and save system, allowing users to create and navigate through a story while saving their progress. The program prompts users to make choices that influence the narrative and can resume from saved points.

Uploaded by

Lovely Laddu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import json

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!")

# Check for saved progress


progress = self.save_system.load_progress()
if progress:
resume = input("Would you like to continue from your last save?
(yes/no): ").strip().lower()
if resume == "yes":
self.decision_tree.current_node = progress["current_node"]
else:
self.save_system.reset_progress()

# 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)

print("\nThe story has concluded. Thank you for playing!")

from story_engine import StoryEngine

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 save_progress(self, current_node):


"""Saves the user's current position in the story."""
with open(self.SAVE_FILE, "w") as file:
json.dump({"current_node": current_node}, file)

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)

You might also like