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.
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
- #08☆☆☆
Text Adventure Game in Python
Learn Dictionaries, Strings and while loops.
- #04☆☆☆
Hangman in Python
Learn Strings, Lists and while loops.
- #01☆☆☆
Tic Tac Toe in Python
Learn Lists, Functions and if, elif and else.
- #09☆☆☆
Connect Four-Style Game in Python
Learn 2D lists and grids, Lists and Functions.
- #10☆☆☆
Minesweeper-Style Game in Python
Learn 2D lists and grids, Functions and for loops.
- #12☆☆☆
Battleship-Style Game in Python
Learn 2D lists and grids, The random module and Strings.
- #13☆☆☆
Maze Game in Python
Learn 2D lists and grids, Functions and Lists.
- #11☆☆☆
Sudoku in Python
Learn 2D lists and grids, Functions and for loops.
- #15☆☆☆
Word Guessing Game in Python
Learn Strings, Lists and Dictionaries.
- #18☆☆☆
Python Quiz Game
Learn Dictionaries, Lists and for loops.
- #07☆☆☆
Mad Libs-Style Story Game in Python
Learn input() and print(), Strings and Variables.
- #14☆☆☆
Higher or Lower Game in Python
Learn The random module, while loops and if, elif and else.
Make it feel like a game
- Print a title and short instructions first.
- Use blank lines and simple borders to separate turns.
- Give feedback for every action, even invalid ones.
- End with a clear win or lose message and offer to play again.
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.