How to Create a Text Explorer Game in Python

Submitted by: 
Language: 
Visitors have accessed this post 132 times.

Introduction:
This tutorial will cover how to create a simple text based exploration game.

How It Works:
What we are going to do is;
Give the user instructions.
Show the current place where the user is within the game.
Give the user options.
Take them in the direction they enter.
Restart loop from step 3.

The Instructions:
So the first thing we do is give the user instructions on how to play the game...

  1. print("\tWelcome to my text exploration game!\nTo play, simply enter north, east, south or west at each point.");

The Variables:
This is where we will give the game locations, options and directions.

First we want to set the title of each place, the description, and the locations of the paths, we do this in capitals and using a tuple because tuples are immutable and constant.

  1. TITLES = ("Home", "Bakery", "Gym", "Park");
  2. DESCRIPTIONS = ("Your luxury home, not much to do...", "Yummy cakes.", "Should we work out? Or go to the bakery...?", "Not much happening, is there something lurking in the shadows?");
  3. NORTHS = ("Bakery", "Park", "Home", "Gym");
  4. SOUTHS = ("Gym", "Home", "Park", "Bakery");
  5. EASTS = ("Nowhere", "Home", "Park", "Nowhere");
  6. WESTS = ("Park", "Nowhere", "Nowhere", "Home");

Mine doesn't really make sense in terms of directional logic, but you should get the point. Look all the tuples as columns of information, when we are at Home, we can go north to get to the Bakery, south Gym, east leads to Nowhere, or west to get to the Park.

We also want to hold the current index of locations...

  1. playerIndex = 0;

Game Loop:
Now we want to create another variable named playing and set it to 'y' standing for yes. Then we will run a while loop while playing is y and the player wants to continue playing the game...

  1. playing = "y";
  2. while (playing):

Current Location:
While the player wants to continue playing, we first want to tell them where they currently are, then we can give them the description.

To give them the description (and locate the next location once the user has chosen their path) we need the current index of the players location from playerIndex...

  1. while (playing):
  2. print("You are currently at " + TITLES[playerIndex]);
  3. print(DESCRIPTIONS[playerIndex]);

User Choice:
Next we want to let the user choose where to go...

  1. while (playing):
  2. print("You are currently at " + TITLES[playerIndex]);
  3. print(DESCRIPTIONS[playerIndex]);
  4. player = input("Where do you want to explore? (North/South/East/West):\n");
  5. if (player.lower() == "north"):
  6. goTo = NORTHS[playerIndex];
  7. elif (player.lower() == "south"):
  8. goTo = SOUTHS[playerIndex];
  9. elif (player.lower() == "east"):
  10. goTo = EASTS[playerIndex];
  11. elif (player.lower() == "west"):
  12. goTo = WESTS[playerIndex];
  13. playing = "n";
  14. going = 0;
  15. for location in TITLES:
  16. if (location == goTo):
  17. playerIndex = going;
  18. going += 1;

The above script outputs the current title and description, gets the users new path, and sets the new index to wherever the new location is in TITLES.

Finally we want a way for the user to exit the game...

  1. elif (player.lower() == "exit"):
  2. playing = "n";
  3.  
  4.  
  5.  
  6. print("Thank you for playing!");

Full Script:

  1. print("\tWelcome to my text exploration game!\nTo play, simply enter north, east, south or west at each point.");
  2. TITLES = ("Home", "Bakery", "Gym", "Park");
  3. DESCRIPTIONS = ("Your luxury home, not much to do...", "Yummy cakes.", "Should we work out? Or go to the bakery...?", "Not much happening, is there something lurking in the shadows?");
  4. NORTHS = ("Bakery", "Park", "Home", "Gym");
  5. SOUTHS = ("Gym", "Home", "Park", "Bakery");
  6. EASTS = ("Nowhere", "Home", "Park", "Nowhere");
  7. WESTS = ("Park", "Nowhere", "Nowhere", "Home");
  8. playerIndex = 0;
  9. playing = "y";
  10. while (playing):
  11. print("You are currently at " + TITLES[playerIndex]);
  12. print(DESCRIPTIONS[playerIndex]);
  13. player = input("Where do you want to explore? (North/South/East/West):\n");
  14. if (player.lower() == "north"):
  15. goTo = NORTHS[playerIndex];
  16. elif (player.lower() == "south"):
  17. goTo = SOUTHS[playerIndex];
  18. elif (player.lower() == "east"):
  19. goTo = EASTS[playerIndex];
  20. elif (player.lower() == "west"):
  21. goTo = WESTS[playerIndex];
  22. elif (player.lower() == "exit"):
  23. playing = "n";
  24. going = 0;
  25. for location in TITLES:
  26. if (location == goTo):
  27. playerIndex = going;
  28. going += 1;
  29. print("Thank you for playing!");

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.