Creating A Text Based Adventure Game
PYer (2386)

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
MATTHEWBECHTEL (122)

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