Coding Grace Text-Based Game with Python

👉 Related to event: Make a text-based game in Python

After a wee delay in starting the workshop, we got going. At least it was an excuse to get folks make them selves a cuppa and take a biccie or two.

There were a few things to set up which I forgot to ask people to install, a text editor. PyCharm IDE and Sublime Text Editor are my recommended editors to use. PyCharm IDE is handy as it has the terminal and Python interpreter all in one place. Personally I just use Sublime Text Editor and a seperate terminal.

Once we got going, I started to show how to get user input via command line. Showing some examples of strings, and got folks comfortable in using basic command line, and the Python interpreter.

Reference: Coding Grace Basic Command Line Workshop

Once people know how to create a Python file, run it, I pretty much walked through by typing a few lines of code and running it. Mick and Ursula were fantastic, they went around helping people. I've introduced strings (touching a little on string manipulation), printing out text, escaping characters, new string formatting, numbers, if statements (various ways of comparing text input), calling other functions from a function, exiting the program, lists (accessing its elements), for loops, while loops (caution using this), booleans (people were starting to get tired here), and finally returning something after calling a function.

And in the process of learning Python, this is the simple story I wanted to base the game on:

Here's a sample run through of the game when I run the game:

batou:text_based_adventure_game whykay$ python 10_game.py What's your name? > vicky Your name is RAINBOW UNICORN, is that correct? [Y|N] > y You are fun, RAINBOW UNICORN! Let's begin our adventure! You enter a room, and you see a red door to your left and a blue door to your right. Do you pick the red door or blue door? > red There you see the great evil Cthulhu. He, it, whatever stares at you and you go insane. Do you flee for your life or eat your head? > flee You enter a room, and you see a red door to your left and a blue door to your right. Do you pick the red door or blue door? > blue You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door What do you do? > left Oooh, treasure! Open it? Press '1' Leave it alone. Press '2' > 1 Let's see what's in here... /grins The chest creaks open, and the guard is still sleeping. That's one heavy sleeper! You find some diamonds gold silver sword What do you want to do? Take all 4 treasure, press '1' Leave it, press '2' > 1 Woohoo! Bounty and a shiney new sword. /drops your crappy sword in the empty treasure chest. You just received [diamonds, gold, silver, sword] You approach the guard, he's still sleeping. Suddenly you knocked a wooden cask with a mug on it... CRASSH!

Oi, what you doing 'ere? [run | door] > run Guard jumps up and looks the other way, missing you entirely. [run | door] > door You just slipped through the door before the guard realised it. You are now outside, home free! Huzzah!

The end

Thanks for playing, RAINBOW UNICORN

You can find all the steps to the final game here: Python Project Workshop - Make text-based game in Python

There are follow-on suggested activities for tweaking the game to do more stuff.

Questions/Suggestions

Feel free to join in our Coding Grace mailing list: https://groups.google.com/forum/#!forum/coding-grace

Finally

Thanks to Adverts.ie for hosting our event.

A big thank you to the mentors, Mick and Ursula.

And a final big thank you to all who came along and participated in the workshop.

Resources

About Author

Vicky Twomey-Lee

Pythonista, but normally found organising various tech events, and now heavily involved in diversity initiatives like Coding Grace, PyLadies Dublin, and Women in Technology and Science (WITS Ireland).
👉 http://about.me/whykay

💖 Our Sponsor/Partner(s)

A huge thanks to following sponsor(s). If you like to support us, get in touch with us.

fourTheorem
Get involved

Interested? Talk to us.

Dublin City  Council PPN

Partner