11) User Input and String Formatting Lesson

Python Project: Command Line Game

4 min to complete · By Martin Breuss

You've already collected a lot of programming knowledge in this course. It's time to put it to the test and build a command-line game. Ready? Do you have all your game pieces in place?

Command Line Text Game

about to start the game - Photo by Michał Parzuchowski https://unsplash.com/@mparzuchowski

_ Photo by Michał Parzuchowski https://unsplash.com/@mparzuchowski_

For this project, you'll build a text-based command-line game. Your players will get the chance to make decisions by entering text in their terminals. Your game will respond with different outcomes, depending on what they choose.

The first version of this game won't be very advanced and probably only fun to play once or twice. However, you'll have the chance to keep expanding on it when moving ahead in the course. By the end, you might have a fun and immersive game in your hands.

Game Mechanics

The game will be a classic text-based Dungeons and Dragons game:

Photo by Clint Bustrillos https://unsplash.com/@clintbustrillos

Photo by Clint Bustrillos https://unsplash.com/@clintbustrillos

List of Tasks

Here are the first tasks that you'll code up in this lesson. You'll read them below in a list form, and as a first step, you can copy that content over into a new Python file as pseudocode:

  • Ask the player for their name.
  • Display a message that greets them and introduces them to the game world.
  • Present them with a choice between two doors.
  • If they choose the left door, they'll see an empty room.
  • If they choose the right door, then they encounter a dragon.
  • In both cases, they have the option to return to the previous room or interact further.
  • When in the seemingly empty room, they can choose to look around. If they do so, they will find a sword. They can choose to take it or leave it.
  • When encountering the dragon, they have the choice to fight it.
  • If they have the sword from the other room, then they will be able to defeat it and win the game.
  • If they don't have the sword, then they will be eaten by the dragon and lose the game.

Translate these English instructions into Python code logic and make your command-line game playable. Communicate with your players by printing out messages that help them immerse themselves into your game's world.

Monster chasing figure - Photo by Robert Coelho https://unsplash.com/@robert_coelho

Photo by Robert Coelho https://unsplash.com/@robert_coelho

To make it easier for you to write text communications that are neatly formatted and display the content you want, check out the following lessons about string formatting before diving into building your game.