How to code your own adventure game in Python

The Raspberry Pi is a wonderful piece of technology, but many people don’t use it to its full potential. With your Raspberry Pi you can create anything you want – a robot that senses its environment, a media centre to watch movies, or a world of fantasy and adventure created from some simple lines of code and a lot of imagination from yourself.

In the 1980s, computer graphics were still in their infancy, with blocky game characters and a limited palette of colours to work with. It was very common for adventure and role-playing games to be completely text-driven, with the player using their imagination to create visions of the game world.

To get In depth Knowledge on Python you can enroll for free live demo Python Online Course

Games such as Zork created rich worlds, with engaging stories and characters, but with very few graphics to illustrate the environments. This remained the case until the late 1980s and early 90s, and only changed due to some fantastic work by LucasArts, which created a collection of classic graphic adventure games such as Loom, Monkey Island and Full Throttle.

For this tutorial, we will be using our Raspberry Pi and a programming language called Python to create our very own text adventure, with our own game world and some characters to inhabit that world. And all of this will be created using some Python code and a few programming concepts.

Python is an easy-to-learn programming language, which has become a firm favourite for Raspberry Pi users and schools across the UK.

So what is Python?
Python is a textual programming language, and by that we mean that it is typed into an editor. Python uses a very forgiving syntax and is very easy to read, which makes learning to code a really enjoyable experience.

On the Raspberry Pi, we already have a great code editor installed as standard – it’s called IDLE and we will be using it to build our game. You can find a link to IDLE on the Raspberry Pi desktop.

Python comes pre-installed on every Raspberry Pi that is running the Raspbian operating system. If you haven’t got Raspbian installed on your Pi, you can grab a copy from the Raspberry Pi website.

It is part of the easy-to-use NOOBS (New Out Of the Box Software) archive, which can be downloaded and then extracted to a blank 4GB (or greater) SD card.

Currently there are two versions of Python available: versions 2.7 and 3. For this tutorial, we will be using 2.7, because it has the greatest amount of support and documentation. Python 3 is an excellent language to learn and it is certainly the future of the language, but it is currently in a state of flux and should only be used by experienced Python programmers.

Creating a narrative
Our game needs two things: a story to keep the player entertained, and logic to control how the story unfolds. For our story we’re creating the world of Narule, where magic and adventure are around every corner. And we’re creating a hero – you – who must travel the land, visiting new villages and settlements, and talking to people to learn more about this land and the dark shadow cast upon it.

To start our project off, we’ve created some narrative for you to expand upon. Feel free to make the story your own – that’s the whole point of this tutorial. This is your game. To get you started, we’ve created some code to act as a starter template. You can download a free copy here.

Download the code, then open it using IDLE (‘File > Open’ and navigate to where you downloaded the code). Now take a look at the code and pay particular attention to any lines that start with a #, because these are comments in the code, which have been added to help you understand what the code is doing at that point.

Currently our code has a basic story for us to expand upon, and we will do that during the course of this tutorial. Our story unfolds via blocks of text that form our narrative, and you will see that each block looks similar to this:

chapter1 = "It was a cold night, and the rain swept in from

the west with a ferocity known only to the gods"

These are called variables, and they enable us to store lots of text or numbers. We use them to contain our story, and then when we want to use them, we use the print function like this:

print chapter1

The print function looks inside the variable and prints its contents on the screen, which is really handy and means that we only have to write the story once and we can re-use it as many times as we wish.

#Pythonprogramming #programming #learnpython #pythoncertification #pythontraining

How to code your own adventure game in Python
8.75 GEEK