For our first project, we had to build something in Python using our coding skills. For my project, I created a text-based adventure game. That means that it is essentially a video game that is played by reading the sentences on the screen, and inputting some text to decide your actions.
The code is split into two main parts, which work together to create the game. For a simple explanation of how the code operates, view this flowchart:

The first section is made up of a lot of text, which is used to describe the locations to the computer, including things like what it should say when the player arrives there, the directions the player is able to travel from where they are and where they lead, and any items the player can pick up in that location. This information is used in the next section, which is the main loop.
locations = {
"forest": {
"description": "You are in a large, expansive forest. As you walk, you can hear the sounds of animals that live here.",
"options": {
"north": "lake",
"west": "village"
}
},
"village": {
"description": {
"default": "You arrive at a quiet village, populated by the locals of this land. Leaning against a nearby fence is a cluster of unlit torches.",
"torch": "At the village, a person approaches you. 'Hello there. I lost my ring over by the lake. If you find it, could you bring it back to me? I'll reward you greatly.'",
"ring": "At the village, a person approaches you. 'Ah, I see you have my ring. If you return it to me, I will give you this treasure map.'",
"quest_ring": "Returning to the village, you walk around for a bit. You see various people going about their jobs, and chatting with other locals."
},
"options": {
"east": "forest",
"north": "mountain"
},
"item": "torch"
},
"lake": {
"description": {
"default": "You come upon a serene lake. You notice a ring sitting by the shore.",
"ring": "You come upon a serene lake."
},
"options": {
"south": "forest",
"west": "mountain",
"east": "river"
},
"item": "ring"
},
"mountain": {
"description": "The ground begins to slope upward, and as the trees start to open up, you see that you are on located partway up a large mountain.",
"options": {
"south": "village",
"east": "lake",
"up": "mountain_peak"
}
},
"mountain_peak": {
"description": "You continue to climb up the mountain, and the ground begins to flatten out beneath your feet. Looking out towards the horizon, you see the wide expanse of land spread out before you.",
"options": {
"down": "mountain"
}
},
"river": {
"description": "As you follow the river, you see a school of fish struggling to swim through a small section of rapids.",
"options": {
"west": "lake",
"east": "cave_mouth"
}
},
"cave_mouth": {
"description": "Still following the river, you come across large boulders forming a cave mouth on the south side of the river.",
"options": {
"west": "river",
"in": "cave"
}
},
"cave": {
"description": {
"default": "Inside the cave, it is almost impossible to see. You stumble forward in darkness.",
"torch": "You light your torch and enter the cave. Looking around, it is difficult to see, but you can make out the shape of the cave wall by the light of your torch."
},
"options": {
"out": "cave_mouth"
}
}
}
The second section makes up the core loop that runs the actual game. This loop includes printing the description of the location the player is in, listing the directions the player can travel, and asking what the player would like to do. If the location includes it, this section of the code also tells the player if there is an item that is nearby that they can pick up, then ask if they want to grab it.
inventory = []
current_location = "forest"
# Functions for code
def lake_ring():
print(locations["lake"]["description"]["default"])
if "ring" not in inventory:
take_ring = input("Do you want to pick up the ring? (y/n) ").lower()
if take_ring == "y":
inventory.append("ring")
print("you picked up: ring")
def village_ring():
print(locations["village"]["description"]["ring"])
return_item = input("Do you give the ring back? (y/n) ").lower()
if return_item == "y":
inventory.append("treasure_map")
inventory.append("quest_ring")
inventory.remove("ring")
print("You handed over the ring.")
print("You received the treasure map.")
if "torch" not in inventory:
print("Leaning against a nearby fence is a cluster of unlit torches.")
take = input("Do you want to pick up the torch? (y/n) ").lower()
if take == "y":
inventory.append("torch")
print("you picked up:")
print(item)
def ask_direction():
global current_location
print("You can go: ", ", ".join(locations[current_location]["options"].keys()))
choice = input("Which direction do you want to go?: ").lower()
if choice in locations[current_location]["options"]:
current_location = locations[current_location]["options"][choice]
print()
else:
print("You can't go that way. That direction either doesn't exist, or is still in development.")
while True:
if "item" in locations[current_location]:
item = locations[current_location]["item"]
# dealing with locations having multiple descriptions
if current_location == "cave":
if "torch" in inventory:
print(locations["cave"]["description"]["torch"])
else:
print(locations["cave"]["description"]["default"])
else:
if current_location == "village":
if "ring" in inventory:
village_ring()
if "torch" in inventory:
if "quest_ring" in inventory:
print(locations["village"]["description"]["quest_ring"])
else:
print(locations["village"]["description"]["torch"])
else:
print(locations["village"]["description"]["default"])
if "torch" not in inventory:
print("Leaning against a nearby fence is a cluster of unlit torches.")
take_torch = input("Do you want to pick up the torch? (y/n) ").lower()
if take_torch == "y":
inventory.append("torch")
print("you picked up: torch")
else:
if current_location == "lake":
if "ring" in inventory or "quest_ring" in inventory:
print(locations["lake"]["description"]["ring"])
else:
lake_ring()
else:
print(locations[current_location]["description"])
ask_direction()
If you want to try it for yourself, the complete code is found here. I used CodeHS to run this code, but other programs will work, too.
locations = {
"forest": {
"description": "You are in a large, expansive forest. As you walk, you can hear the sounds of animals that live here.",
"options": {
"north": "lake",
"west": "village"
}
},
"village": {
"description": {
"default": "You arrive at a quiet village, populated by the locals of this land. Leaning against a nearby fence is a cluster of unlit torches.",
"torch": "At the village, a person approaches you. 'Hello there. I lost my ring over by the lake. If you find it, could you bring it back to me? I'll reward you greatly.'",
"ring": "At the village, a person approaches you. 'Ah, I see you have my ring. If you return it to me, I will give you this treasure map.'",
"quest_ring": "Returning to the village, you walk around for a bit. You see various people going about their jobs, and chatting with other locals."
},
"options": {
"east": "forest",
"north": "mountain"
},
"item": "torch"
},
"lake": {
"description": {
"default": "You come upon a serene lake. You notice a ring sitting by the shore.",
"ring": "You come upon a serene lake."
},
"options": {
"south": "forest",
"west": "mountain",
"east": "river"
},
"item": "ring"
},
"mountain": {
"description": "The ground begins to slope upward, and as the trees start to open up, you see that you are on located partway up a large mountain.",
"options": {
"south": "village",
"east": "lake",
"up": "mountain_peak"
}
},
"mountain_peak": {
"description": "You continue to climb up the mountain, and the ground begins to flatten out beneath your feet. Looking out towards the horizon, you see the wide expanse of land spread out before you.",
"options": {
"down": "mountain"
}
},
"river": {
"description": "As you follow the river, you see a school of fish struggling to swim through a small section of rapids.",
"options": {
"west": "lake",
"east": "cave_mouth"
}
},
"cave_mouth": {
"description": "Still following the river, you come across large boulders forming a cave mouth on the south side of the river.",
"options": {
"west": "river",
"in": "cave"
}
},
"cave": {
"description": {
"default": "Inside the cave, it is almost impossible to see. You stumble forward in darkness.",
"torch": "You light your torch and enter the cave. Looking around, it is difficult to see, but you can make out the shape of the cave wall by the light of your torch."
},
"options": {
"out": "cave_mouth"
}
}
}
print("Welcome to my text-based adventure game! In this game, you are given a description of where you are and what you can do. You are also given a list of locations you can go to from your current location, given to you as directions. When given directions that you can travel in, you have to type in your chosen direction, only including the name of that direction as given to you. Have fun!")
print()
inventory = []
current_location = "forest"
# Functions for code
def lake_ring():
print(locations["lake"]["description"]["default"])
if "ring" not in inventory:
take_ring = input("Do you want to pick up the ring? (y/n) ").lower()
if take_ring == "y":
inventory.append("ring")
print("you picked up: ring")
def village_ring():
print(locations["village"]["description"]["ring"])
return_item = input("Do you give the ring back? (y/n) ").lower()
if return_item == "y":
inventory.append("treasure_map")
inventory.append("quest_ring")
inventory.remove("ring")
print("You handed over the ring.")
print("You received the treasure map.")
if "torch" not in inventory:
print("Leaning against a nearby fence is a cluster of unlit torches.")
take = input("Do you want to pick up the torch? (y/n) ").lower()
if take == "y":
inventory.append("torch")
print("you picked up:")
print(item)
def ask_direction():
global current_location
print("You can go: ", ", ".join(locations[current_location]["options"].keys()))
choice = input("Which direction do you want to go?: ").lower()
if choice in locations[current_location]["options"]:
current_location = locations[current_location]["options"][choice]
print()
else:
print("You can't go that way. That direction either doesn't exist, or is still in development.")
while True:
if "item" in locations[current_location]:
item = locations[current_location]["item"]
# dealing with locations having multiple descriptions
if current_location == "cave":
if "torch" in inventory:
print(locations["cave"]["description"]["torch"])
else:
print(locations["cave"]["description"]["default"])
else:
if current_location == "village":
if "ring" in inventory:
village_ring()
if "torch" in inventory:
if "quest_ring" in inventory:
print(locations["village"]["description"]["quest_ring"])
else:
print(locations["village"]["description"]["torch"])
else:
print(locations["village"]["description"]["default"])
if "torch" not in inventory:
print("Leaning against a nearby fence is a cluster of unlit torches.")
take_torch = input("Do you want to pick up the torch? (y/n) ").lower()
if take_torch == "y":
inventory.append("torch")
print("you picked up: torch")
else:
if current_location == "lake":
if "ring" in inventory or "quest_ring" in inventory:
print(locations["lake"]["description"]["ring"])
else:
lake_ring()
else:
print(locations[current_location]["description"])
ask_direction()
AI was utilized in the creation of this project. To see how I incorporated AI into creating this project, click this link to see the full transcript.
Leave a Reply to mcrompton Cancel reply