Note
This repo was created for for teaching purposes only.
This repo is set up to bootstrap your basic Python text adventure game to function as a web page that's shareable with friends.
- If you don't have one already, create your own github account.
- Click the "Fork" button at the top right of this page
- This creates your own copy of the repository under your GitHub account
- Go to your forked repository
- Click on "Settings" (in the repository navigation)
- Scroll down to the "Pages" section in the left sidebar
- Under "Source", select "Deploy from a branch"
- Under "Branch", select "main" and "/ (root)"
- Click "Save"
- Wait a few minutes for GitHub to build your site
- Your game will be available at:
https://[your-username].github.io/text-adventure-game/
- Click on
game.py
in your repository - Click the pencil icon to edit the file
- Replace the sample game with your own Python code
- Commit your changes (scroll down, add a commit message, and click "Commit Changes")
Your game should be written in standard Python. The web terminal supports:
print()
statements for outputinput()
for getting player input- Functions (
def
) and variables - Conditional statements (
if
/elif
/else
) - Loops (
while
/for
/break
)
Once your game is working, share the link with friends! They can play directly in their browser without installing Python.
- Your game must be contained in a single
game.py
file. - No external Python libraries are allowed, only those supported by Pyodide 1.
- Because standard Python
input
is not fully supported in Pyodide 2, the built-ininput
function is replaced with an asynchronous version 3. All code is preprocessed so that calls toinput
becomeawait input
, all functions are defined withasync def
, and all function calls are prefixed withawait
4. - This approach is very error-prone and will break if you use lambdas, define functions inside other functions, or reassign functions to new variable names (such as when using callbacks).
- If your game doesn't load, check for Python syntax errors
- Make sure your
game.py
file is in the root of your repository - Allow a few minutes for GitHub Pages to update after making changes
- Check the browser console 5 for the preprocessed code
Here are some helpful functions you may want to add into your text adventure game.
You may want to create a function for getting a menu option.
def get_menu_option(option_1, option_2, option_3):
print("Menu:")
print(f"1. {option_1}")
print(f"2. {option_2}")
print(f"3. {option_3}")
while True:
option = input("Enter option (1,2,3): ")
if option == "1" or option == option_1:
return 1
elif option == "2" or option == option_2:
return 2
elif option == "3" or option == option_3:
return 3
else:
# Loop back to top of while-loop
print(f"Invalid input: {option}")
This is reusable in your code and lets your write your if-statements to only check a number instead of the full string.
action = get_menu_option("go north", "go south", "open door")
if action == 1:
# Code for going north
elif action == 2:
# Code for going south
elif action == 3:
# Code for opening door
else:
print(f"BAD ACTION {action}")
You might prefer to clear the screen at the start of every new room. This can be done (in both a Google Colab notebook and when hosted as a webpage with xterm.js) using the following function:
def clear_screen():
import os
import sys
# Check if running in Jupyter notebook
try:
from IPython import get_ipython
if get_ipython() is not None:
from IPython.display import clear_output
clear_output(wait=True)
return
except ImportError:
pass
# For xterm.js in browser
print("\033[2J\033[H", end="")
# Fallback for terminal environments
if os.name == 'posix': # For Unix/Linux/MacOS
os.system('clear')
elif os.name == 'nt': # For Windows
os.system('cls')
def print_with_color(text, color, bold=False):
colors = ["red", "green", "yellow", "blue", "magenta", "cyan"]
if not color in colors:
raise ValueError(f"Invalid color: {color}; Choose one of: {colors}")
ansi_code = colors.index(color) + 31 # https://en.wikipedia.org/wiki/ANSI_escape_code
print(f"\033[{ansi_code}m{text}\033[0m")
This will let you print specific colors:
print_with_color("this is red", "red")
print_with_color("this is cyan", "cyan")