textbasedgame/TextBasedGame.py
2022-10-16 17:46:11 +00:00

249 lines
8.5 KiB
Python

# Cody Cook
# Southern New Hampshire University
# IT-140 Introduction to Scripting
# Project 2 - Text Based Game
# Professor Lisa Shannon
# 2022-10-16
import random
separator = "##################################"
creepy_undertones = [
"[???] You hear a strange noise coming from an adjacent room...",
"[???] You thought you heard something, but as you look around, you can't see anything.",
"[???] You hear thunder rumbling in the distance.",
"[???] You saw something move in the corner of the room, but looking again, there's nothing there."
]
system = {
"name": 'Noises in the Dark',
"version": '1.0',
"author": 'Cody Cook',
"game": {
# Store the current game's state
"current_room": "foyer",
"items": [],
"completed": False,
"villain": False
}
}
rooms = {
"foyer": {
"name": "Foyer",
"item": -1,
"item_name": None,
"item_location": None,
"locked": False,
"paths": {
"north": "basement_stairwell",
"west": "dining_hall",
"east": "closet"}
},
"closet": {
"name": "Closet",
"item": 0,
"item_name": "Greenhouse key",
"item_location": "in the pocket of a puffy coat",
"locked": False,
"paths": {
"west": "foyer"}
},
"basement_stairwell": {
"name": "Basement Stairwell",
"item": -1,
"item_name": None,
"item_location": None,
"locked": False,
"paths": {
"south": "foyer",
"east": "wine_cellar",
"north": "master_bedroom"}
},
"wine_cellar": {
"name": "Wine Cellar",
"item": 0,
"item_name": "damp paper bag",
"item_location": "at the foot of the stairs",
"locked": False,
"paths": {
"west": "basement_stairwell"}
},
"master_bedroom": {
"name": "Master Bedroom",
"item": 0,
"item_name": "laser pointer",
"item_location": "on a nightstand",
"locked": False,
"paths": {
"south": "basement_stairwell",
"west": "library"}
},
"library": {
"name": "Library",
"item": 0,
"item_name": "bird feather",
"item_location": "in front of a bookcase",
"locked": False,
"paths": {
"east": "master_bedroom",
"south": "dining_hall",
"west": "children_bedroom"}
},
"children_bedroom": {
"name": "Children's Bedroom",
"item": -1,
"item_name": None,
"item_location": None,
"locked": True,
"paths": {
"east": "library"}
},
"dining_hall": {
"name": "Dining Hall",
"item": 0,
"item_name": "mouse toy",
"item_location": "below the table",
"locked": False,
"paths": {
"north": "library",
"east": "foyer",
"west": "kitchen"},
},
"kitchen": {
"name": "Kitchen",
"item": 0,
"item_name": "mangled water-bottle",
"item_location": "near the garbage can",
"locked": False,
"paths": {
"east": "dining_hall",
"north": "delivery_area"}
},
"delivery_area": {
"name": "Delivery Area",
"item": -1,
"item_name": None,
"item_location": None,
"locked": False,
"paths": {
"south": "kitchen",
"north": "greenhouse"}
},
"greenhouse": {
"name": "Greenhouse",
"item": 0,
"item_name": "black hairball",
"item_location": "near an open window",
"locked": True,
"paths": {
"south": "delivery_area"}
}
}
def show_instructions():
print(separator, "\n# Welcome to " + system['name'] + "! #\n# Version: " + system["version"] + " by " + system[
'author'] + " #\n" + separator + separator,
"\nYou are investigating strange noises in a house and must locate the source.",
"\nExplore the house to uncover clues and items that will help you solve the mystery.",
"\nMove using 'north', 'south', 'east', or 'west', where possible.",
"\nYou can 'search' for items, and ask for 'help' to see this message again.",
"\nCheck your inventory using 'items' to see what you have found.",
"\nGood luck!\n" + separator + separator)
def get_item(room):
if rooms[room]["item"] == -1: # -1 = No Item in Room
return False
elif rooms[room]["item"] == 1: # 1 = Item Taken
return None
elif rooms[room]["item"] == 0: # 0 = Item Available
rooms[room]["item"] = 1 # Set item to taken
return True
def list_items():
if len(system["game"]["items"]) >= 1:
print("[Items] " + ', '.join(system["game"]["items"]))
else:
print("[Items] You have no items.")
def print_strings(string):
if string == "greenhouse_locked":
print("[Move] The greenhouse door is locked... if only you could find a key.")
elif string == "children_bedroom_locked":
print("[Move] The children's bedroom door is unlocked but there's something heavy behind the door...")
elif string == "middle_of_the_house":
print("[!!!] You hear a loud noise coming from the children's bedroom... better investigate!")
def get_new_state(direction, current_room=system["game"]["current_room"]):
if direction in rooms[current_room]["paths"]:
if rooms[rooms[current_room]["paths"][direction]]["locked"]:
print_strings(rooms[system["game"]["current_room"]]["paths"][direction] + "_locked")
else:
system["game"]["current_room"] = rooms[current_room]["paths"][direction]
else:
print("[Move] You can't go that way.")
def show_status():
print("[Current Room] " + rooms[system["game"]["current_room"]]["name"])
list_items()
if rooms[system["game"]["current_room"]]["item"] == 0:
print("[???] There's a", rooms[system["game"]["current_room"]]["item_name"] + " here somewhere.")
directions = ""
for direction in rooms[system["game"]["current_room"]]["paths"]:
directions += direction + " "
print("[Actions]", "quit", "help", "search", "items", directions)
if random.randint(1, 5) == 1:
print(random.choice(creepy_undertones))
def main():
show_instructions()
while system["game"]["completed"] is False:
show_status()
print("What would you like to do?")
user_action = (input('> ').lower())
if user_action == "search":
if get_item(system["game"]["current_room"]):
print("[Search] You found a", rooms[system["game"]["current_room"]]["item_name"],
rooms[system["game"]["current_room"]]["item_location"] + "!")
system["game"]["items"].append(rooms[system["game"]["current_room"]]["item_name"])
else:
print("[Search] You found nothing.")
elif user_action == "items":
list_items()
elif user_action == "help":
show_instructions()
elif user_action == "quit" or user_action == "exit":
system["game"]["completed"] = True
elif user_action in ['west', 'north', 'south', 'east']:
get_new_state(user_action, system["game"]["current_room"])
else:
print("[Error] Invalid action; please try again.")
if "Greenhouse key" in system["game"]["items"] and rooms["greenhouse"]["locked"]:
rooms["greenhouse"]["locked"] = False
if len(system["game"]["items"]) == 7 and rooms["children_bedroom"]["locked"]:
rooms["children_bedroom"]["locked"] = False
print_strings("middle_of_the_house")
if system["game"]["current_room"] == "children_bedroom" and len(system["game"]["items"]) == 7:
system["game"]["villain"] = True
system["game"]["completed"] = True
if system["game"]["villain"]:
print(separator + separator,
"\n[!!!] You enter the children's room. This room is an absolute mess..." +
"\n[!!!] Inside, you discover a black cat; around the cat's neck, you see a name-tag." +
"\n[!!!] Scratchy is the cat's name. Could this have been the troublemaker all along?" +
"\n[!!!] You take Scratchy to the owners of the house." +
"\n[!!!] The family is ecstatic to see their cat after it ran away a week ago."
)
print("\n[Game Over] The game is exiting. Thank you for playing!")
if __name__ == "__main__":
main()