Text-Based Games in Python: How to Make Your Own

A text-based game runs in the console: it prints what's happening and reads what you type. No graphics means you can focus entirely on the logic, which is why they're the best first games to write in Python.

The shape of every text game

Nearly every console game follows the same loop: show the state, read a command, update the state, check if the game is over.

state = set_up_game()
while not game_over(state):
    show(state)
    command = input("> ")
    state = update(state, command)
show_result(state)

Reading commands

Clean the input with .strip().lower(), then .split() it into a verb and a noun. Always handle the unexpected: an empty line, a typo, a number where a word was expected.

example 1 · press Run, then edit and run again

Keep the world as data

Put rooms, questions, words and rules into lists and dictionaries instead of long chains of if statements. Then the game grows by adding data, not code.

Text games to play and study

Make it feel like a game

Frequently asked questions

What is a text-based game?

A game played entirely with text: the program describes what happens and the player types commands or answers. Classic examples are adventure games, quizzes and word games.

Is Python good for text-based games?

Very. input(), print() and Python's strings, lists and dictionaries are all you need, and the code stays short and readable.