Table of Contents
- Beginning The Boa
- I'm Going To Need A Sssslithering Storyboard
- Charming The Cobra
- Scale-ing This Story Into My Narrative
Beginning The Boa
The next step of my career dev journey was to tame the terrific Python and savor the swift, slithering satisfaction of solving complex coding needs.
As a hobby I am a Dungeon Master in the co-operative tabletop roleplaying game Pathfinder which traces its roots back to the classic Dungeons & Dragons.
The game involves imagination, improv and a group of your best friends to not laugh too hard while you put on silly voices. The aim of the game is for the players to interact with the world and narrative the Game Master creates, allowing them the agency to make a choice which will impact the world in meaningful ways.
Coupling this hobby with learning Python seemed like a no-brainer.
The idea was simple enough - allow the player to input commands which would influence the flow of the story.
By combining my passion for storytelling and ambition to learn Python my next project idea was born: a classic choose your own adventure game!
I'm Going To Need A Sssstoryboard
"The scariest moment is always just before you start" - Stephen King
First thing I needed was a story - the writing wouldn't be too much of a challenge as I'd lean on my experience writing fantasy to cook up something with a bit of depth that didn't take itself too seriously.
However writing a branching narrative was challenging as I'd have to be able to keep track of all the paths and options leading onto the next choice. After a bit of googling I found the helpful online tool Twinery. It allowed me to create paths with many different choices, displaying it in a clear visual format, which helped plan out the big picture of the story.
Ironically enough once I had laid out my paths and choices, I realised Twinery had the option to proof read and even play it straight away with no additional work required! However it was javascript-based and the backend was done for you. My eyes were on the prize with Python - perhaps I'll save javascript for another day...
Charming The Cobra
My story was set and I could finally begin writing out the Python code.
My IDE of choice was vscode so I opened up a new python file and began the learning journey.
On one screen a black void opened up endlessly before me - the other screen a man patiently walking through a tutorial on how to make your own adventure game.
With the help of a few video tutorials I picked up some best practices when handling user text inputs, such as the lower() and strip() functions which standardised what the user entered to ensure the story could continue successfully. I also learnt of the 'time' module in Python to add a bit of dramatic flair to the story outputs. An essential tip was the use of loops via 'while True' and nested if/elif functions to ensure certain choices would start from the beginning and allow the player to use their new found knowledge to try a different path from the beginning.
The main code logic I settled on was to define every story choice as a variable and then return any other connected story choice variable based on user input. This resulted in an interweaved web of story choices leading to a next step, conclusion or new beginning.
726 lines of code and a long period of testing later and my game was complete.
Scale-ing This Story Into My Narrative
The final piece of the Python-puzzle was integrating the game into my existing website.
After a quick google I identified the 'replit' service, which allowed for code to run live in a browser with an IDE, compiler and interpreter.
It was simple enough to generate a link to share the code and embed it on my front page to provide a quick link to play the game.
Feel free to check it out here or play directly below!
Resources
Writing a Text-Based Adventure Game in Python
Choose Your Own Adventure Game in Python (Beginners
https://inventwithpython.com/invent4thed/chapter5.html
ChatGPT