Skip to content

Instantly share code, notes, and snippets.

@taybenlor
Created July 18, 2021 07:32
Show Gist options
  • Save taybenlor/4ef08abdfafe66297bf9b97210d47829 to your computer and use it in GitHub Desktop.
Save taybenlor/4ef08abdfafe66297bf9b97210d47829 to your computer and use it in GitHub Desktop.
A way to do text adventures using modules.
"""
Each module can import the player module to get their name and their items.
Here we add boots to their items if they write "pickup boots".
"""
import main
import player
def enter():
if 'boots' in player.items:
print('The house is entirely empty.')
else:
print('You enter a small dark hovel.')
print('You search it and see only a pair of boots.')
action = input('What would you like to do? ').lower()
while action not in ['pickup boots', 'go south']:
print(f"I don't understand that {player.name}, you can PICKUP or GO.")
action = input('What would you like to do? ').lower()
if action == 'go south':
main.enter()
else:
pickup()
def pickup():
print('You put on the boots.')
player.items.append('boots')
enter()
"""
This is the entrypoint for the game. You start it with `$ python main.py`.
Each "area" in the game is its own Python module. You enter the area with the `enter()` function.
Each area can have more functions inside it, but I've kept it simple here.
"""
import river
import house
import player
def enter():
print('To your North is a house, to your South a river.')
print('In the distance beyond the river you see a glowing green sign.')
# This could be abstracted out to a helper like
# command = get_command(['go north', 'go south'])
direction = input('What would you like to do? ').lower()
while direction not in ['go north', 'go south']:
print(
f"I don't understand that {player.name}, try 'go north' or 'go south'.")
direction = input('What would you like to do? ').lower()
if direction == 'go north':
house.enter()
else:
river.enter()
if __name__ == '__main__':
print('Welcome to this adventure!')
player.name = input('What is your name? ')
print(f'{player.name}, you find yourself trapped in a strange land.')
print('You can GO places and PICKUP things.')
enter()
"""
The player.py file stores global variables.
You can use this for items the player picks up.
You could also store health here, or other details about the player.
"""
name = "Player"
items = []
import main
import player
import sys
def enter():
if 'boots' in player.items:
print('You cross the river in your wading boots and leave this strange land.')
print(f'Congratulations {player.name}!')
sys.exit()
print('You come to a river, across it is a green sign with the letters')
print('E X I T')
print('upon it. Oh it\'s an EXIT sign.')
print()
print("You cannot cross the river, your bare feet will get cold and wet.")
direction = input('What would you like to do? ').lower()
while direction not in ['go north']:
print(
f"I don't understand that {player.name}, try 'go north'.")
direction = input('What would you like to do? ').lower()
main.enter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment