Skip to content
Sign upLog in

Creating A Text Based Adventure Game

Profile icon
PYer

Creating A Text Based Adventure Game

This is based on a game I created ( Aliens Adventure Game ).

Knowledge Required

Before starting this tutorial, make sure you have a basic understanding of the following.

  • Basic classes
  • Basic inheritance
  • Input and Printing (Obviously)
  • Dictionaries
  • While loops

Introduction

This game's code is mainly based on classes. Each room is a simple class, with a play function that plays that room. Each play function returns a string, such as "corridor" or "bomb hold". The rooms will all be stored in a dict titled engine, and with each room held and accesed from there.

Creating The Game

Now it is time to actually create the game. The file for the code, if you want to copy and paste it or play around with it can be found at

@Pyer
Aliens Adventure Game Repl
Create a python repl and open main.py

Creating the Room class

In the file main.py type in this:

class Room(): def __init__(self): pass def play(self): print('This room hasnt been created yet.')

There we go. If you meet the knowledge requirments of above, you should understand this. Basically a class called Room() is being created, and does nothing when it is created (initialized). The play function does nothing yet.

Creating rooms

In the file main.py again create a room
This example here is from my aliens adventure game:

from replit import clear as c import sys # Code before here class Intro(Room): def play(self): c() print('Welcome To Aliens Adventure Game!\nObjective: \n\tFind the bomb to be planted on Earth, \n\tPlace it on the bridge, and escape\n\tbefore the spaceship blows up.') if not input('Ready? [y/n]: ') == 'y': sys.exit() c() print('You are onboard an alien spaceship. You have managed to reach the main room, without spotting any aliens. Hopefully, this streak of good luck continues until you escape the ship. But as any good space invader knows, that is never the case. You look around the room deciding what to do when an alien pops out of a corridor. You put a hand on your blaster.\nYour options are:\n\t1. Shoot\n\t2. Tell A Joke\n\t3. Run') o = input('Move? [1, 2, 3]: ') c() if o == '1': print('You pull out your blaster and shoot the alien. Apparently the alien has enabled a force field as the bullet rebounds off of him. He is angered at your threating gesture,pulls out his own blaster, and shoots you in the head. You have determined the fate of thousands of inoccent people on earth.') return 'game over' elif o == '2': print('You remeber your old school training on the alien language. You say the first joke that pops in your mind:\n\tAyay anmay andyay ishay orsehay alkedway \n\tintoyay ayay arbay. Ouyay ouldway \n\tavehay oughtthay eythay ouldway avehay \n\tuckedday.\nThe alien doubles over laughing and you quickly rush past him and through the door into the corridor.') return 'corridor' elif o == '3': print('You quickly scan your surroundings, for an exit. You turn around and attempt to exit the way you came in, but the aline quickly pulss out his blaster and shoots your moving shape. You have determined the fate of thousands of inoccent people on earth.') return 'game over'

There we go! Continue creating rooms. The basic template is like this:

class AnotherRoom(Room): def play(): # Text return 'next room'
Creating the engine

This is based on a while True loop.
First, define a dict called engine.
In my game this looks like:

engine = { 'game over': Game_Over(), 'intro': Intro(), 'corridor': Corridor(), 'bomb hold': Bomb_Hold(), 'bridge': Bridge(), 'escape room': EscapeRoom(), 'win': Win() }

The basic template is this:

engine = {'mainroom': AnotherRoom() }

Now we have to define the first room like this:

room = engine['intro'].play()

Then add this while True:

while True: room = engine[room] room = room.play()

There we go!
Now you can add other rooms. A recommendation would be to add a Win() and Lose() class, so that the user goes to those rooms when they die or win.
Here is an example repl. https://repl.it/@PYer/Aliens-Adventure-Game

You are viewing a single comment. View All
Profile icon
MATTHEWBECHTEL

Cool. I wonder why there are no other comments though.

Profile icon
PYer

@MATTHEWBECHTEL
Thanks! Share your adventure game here if you create one!

Profile icon
MATTHEWBECHTEL

Yeah, I’m sort of making an adventure game like your using your repl but changing things up. When I post it I’ll give the format credit to you.

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
k. i can't wait to see it!

Profile icon
MATTHEWBECHTEL

k. It’s like half done.

Profile icon
MATTHEWBECHTEL

Also I might like need your help with something later.

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
k. what is it?

Profile icon
MATTHEWBECHTEL

Indenting. Indenting.... is stupid.

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
where? can you give me a link?

Profile icon
MATTHEWBECHTEL

Yes. Help, save me, god, the lord, the giver of life, my savior.

@PYer

https://repl.it/@MATTHEWBECHTEL/NEW-PROGRAM

Profile icon
PYer

@MATTHEWBECHTEL
let me look

Profile icon
MATTHEWBECHTEL

Alright.

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
can you open up a multiplayer session?

Profile icon
MATTHEWBECHTEL

No. Not currently. I am on my iPad right now.

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
ok.

Profile icon
MATTHEWBECHTEL

I can open one in about 2 hours.

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
its fine. i got it.
Indent line 86, so that it is part of the function. I have no idea what you are trying to do with the else statement on line 87. Either add an if statement, or just remove the else statement. Next, indent lines 88 and 89, and realign line 90.

Profile icon
MATTHEWBECHTEL

I meant to remove the else statement and I fixed all of them. Thanks! Now I have an engine room error on line 133 XD

@PYer

Profile icon
PYer

@MATTHEWBECHTEL
can you show me?

Profile icon
MATTHEWBECHTEL

It has been showed.

@PYer