diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md deleted file mode 100644 index f6800627..00000000 --- a/README.md +++ /dev/null @@ -1,135 +0,0 @@ -Ironhack Logo - -# Project: Escape Room Python Game - -## Overview - -The goal of this project is for you to apply the Python programming skills you have learned this week in solving a real problem. We present you the design of a classic game that can be played via text - Escape Room. Your goal is to develop the game using Python based on the game design. - -Read the game narratives and technical requirements carefully. Plan how you will achieve your goal with pseudo codes and/or flowchart. Review the logic and process in order to avoid potential errors. Then implement with Python code. After that, test your code several times with different inputs and make sure it's rock solid. The instructional team is standing by to help you in case you run into any problems. - ---- - -## Game Design - -### Game Map - -![Game Map](escape-room-plan.jpg) - -### Game Narratives - -#### Game Room - -You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. You don't remember why you are here and what had happened before. You feel some unknown danger is approaching and you must get out of the house, NOW! - -You are now in game room. - -What would you like to do? - -You explore the room. This is Game Room. You find couch, piano, and Door A. - -You examine couch. There isn't anything interesting about it. - -You examine Door A. It is locked but you don't have the key. - -You examine piano. You find key for Door A. - -You examine Door A. Your unlock it with a key you have. - -Do you want to go to the next room? - -#### Bedroom 1 - -You are now in Bedroom 1. - -What would you like to do? - -You explore the room. This is Bedroom 1. You find queen bed, Door A, Door B, and Door C. - -You examine queen bed. You find key for Door B. - -You examine Door C. It is locked but you don't have the key. - -You examine Door B. Your unlock it with a key you have. - -Do you want to go to the next room? - -#### Bedroom 2 - -You are now in Bedroom 2. - -What would you like to do? - -You explore the room. This is Bedroom 2. You find double bed, dresser, and Door B. - -You examine double bed. You find key for Door C. - -You examine dresser. You find key for Door D. - -You examine Door B. Your unlock it with a key you have. - -Do you want to go to the next room? - -#### Bedroom 1 - -You are now in Bedroom 1. - -What would you like to do? - -You examine Door C. Your unlock it with a key you have. - -Do you want to go to the next room? - -#### Living Room - -You are now in Living Room. - -What would you like to do? - -You explore the room. This is Living Room. You find dining table, Door C, and Door D. - -You examine Door D. Your unlock it with a key you have. - -Do you want to go to the next room? - -#### Outside - -Congrats! You escaped the room! - ---- - -## Getting Started - -Overwhelmed and don't know where to start? This is a tough challenge we know. But don't worry. We have included a working example for you to reference in which only 1 room (game room) is included. Read the example and make sure you understand it. Then you can expand on top of the example to code the rest of the rooms. - -The provided example is just a Minimal Viable Product (MVP). It is fully functional but not bullet proof. You should be aware of its limitations while you are working and try to make your final product as robust as possible. - -## Technical Requirements - -* Use Python lists and dictionaries to define the rooms, items, and relations of them. - -* Use a Python dictionary to store the game state. Update the state dictionary when progresses are made such as a key is collected. - -* Use Python functions to play the game. Don't use procedural code. - -* After completing each game action, call the next function to continue playing until the winning condition is reached. - -* The winning condition is for the player to successfully make to the "Outside" room. When this happens, congrat the player and end the game. - -## Necessary Deliverables - -The following deliverables should be pushed to your Github repo. - -* `main.ipynb` that contains your solution. - -## Suggested Ways to Get Started - -1. Start Jupyter Notebook from this lab directory. - -1. Launch `sample-code.ipynb` and read through the file. Also execute the code and play the game. Make sure you understand what each line of the codes does. - -1. Create `main.ipynb` and copy the codes from `sample-code.ipynb`. Expand the code following the example to create Bedroom 1. - -1. Test the game with Game Room and Bedroom 1. Make sure everything works then work on Bedroom 2 then Living Room. - -1. Test the whole game. Try to make all kinds of inputs to make sure your game is rock solid and will not break. diff --git a/backyard.jpg b/backyard.jpg new file mode 100644 index 00000000..d1a8be98 Binary files /dev/null and b/backyard.jpg differ diff --git a/bedroom1.jpg b/bedroom1.jpg new file mode 100644 index 00000000..81bf1e4c Binary files /dev/null and b/bedroom1.jpg differ diff --git a/bedroom2.jpg b/bedroom2.jpg new file mode 100644 index 00000000..022d1388 Binary files /dev/null and b/bedroom2.jpg differ diff --git a/bedroom3.jpg b/bedroom3.jpg new file mode 100644 index 00000000..72785726 Binary files /dev/null and b/bedroom3.jpg differ diff --git a/couch.jpg b/couch.jpg new file mode 100644 index 00000000..b3f40f31 Binary files /dev/null and b/couch.jpg differ diff --git a/diningtable.jpg b/diningtable.jpg new file mode 100644 index 00000000..ad5887ce Binary files /dev/null and b/diningtable.jpg differ diff --git a/door.jpg b/door.jpg new file mode 100644 index 00000000..25c95da0 Binary files /dev/null and b/door.jpg differ diff --git a/doublebed.jpg b/doublebed.jpg new file mode 100644 index 00000000..c9274539 Binary files /dev/null and b/doublebed.jpg differ diff --git a/dresser.jpg b/dresser.jpg new file mode 100644 index 00000000..f263b008 Binary files /dev/null and b/dresser.jpg differ diff --git a/escape-room-plan.jpg b/escape-room-plan.jpg deleted file mode 100644 index a3ea42c1..00000000 Binary files a/escape-room-plan.jpg and /dev/null differ diff --git a/escape.py b/escape.py new file mode 100644 index 00000000..56555577 --- /dev/null +++ b/escape.py @@ -0,0 +1,507 @@ + +# Imports + +import pygame +import math +import random +import time + +from threading import Thread +from os import system, name + +from colorama import Fore, Style + +# Defining the timer - to check + +def countdown(): + time_sec = 180 + while time_sec: + mins, secs = divmod(time_sec,60) + timeformat = "{:02d}:{:02d}".format(mins,secs) + time.sleep(1) + time_sec -=1 + if game_state['game_won'] == False: + if time_sec == 119: + print(Fore.RED + "\n\nYou only have ",timeformat,"left!") + print(Style.RESET_ALL) + elif time_sec == 59: + print(Fore.RED + "\n\nYou're running out of time:",timeformat) + print(Style.RESET_ALL) + elif time_sec == 0: + print(Fore.RED + "\n\nOh no.. GAME OVER") + print(Style.RESET_ALL) + +countdown_thread = Thread(target = countdown) +countdown_thread.start() + +# Defining rooms + +game_room = { + "name": "game room", + "type": "room", + "image": "gameroom.jpg", +} + +bedroom_1 = { + "name": "bedroom 1", + "type": "room", + "image": "bedroom1.jpg", +} + +bedroom_2 = { + "name": "bedroom 2", + "type": "room", + "image": "bedroom2.jpg", +} + +living_room = { + "name": "living room", + "type": "room", + "image": "livingroom.jpg", +} + +backyard = { + "name": "backyard", + "type": "room", + "image": "backyard.jpg", +} + +outside = { + "name": "outside", + "image": "outside.jpg", +} + +# Defining furnishings + +couch = { + "name": "couch", + "type": "furniture", + "image": "couch.jpg", +} + +piano = { + "name": "piano", + "type": "furniture", + "image": "piano.jpg", +} + +suspicious_painting = { + "name": "suspicious painting", + "type": "safe", + "image": "painting.jpg", +} + +queen_bed = { + "name": "queen bed", + "type": "furniture", + "image": "queenbed.jpg", +} + +double_bed = { + "name": "double bed", + "type": "furniture", + "image": "doublebed.jpg", +} + +dresser = { + "name": "dresser", + "type": "furniture", + "image": "dresser.jpg", +} + +library = { + "name": "library", + "type": "furniture", + "image": "library.jpg", +} + +dining_table = { + "name": "dining table", + "type": "furniture", + "image": "diningtable.jpg", +} + +kennel = { + "name": "kennel", + "type": "dog", + "image": "kennel.jpg", +} + +tree = { + "name": "tree", + "type": "furniture", + "image": "tree.jpg", +} + +# Defining doors + +door_a = { + "name": "door a", + "type": "door", + "image": "door.jpg", +} + +door_b = { + "name": "door b", + "type": "door", + "image": "door.jpg", +} + +door_c = { + "name": "door c", + "type": "door", + "image" : "door.jpg", +} + +door_d = { + "name": "door d", + "type": "door", + "image": "door.jpg", +} + +door_e = { + "name": "door e", + "type": "door", + "image": "door.jpg", +} + +# Defining keys + +key_a = { + "name": "key for door a", + "type": "key", + "target": door_a, +} + +key_b = { + "name": "key for door b", + "type": "key", + "target": door_b, +} + +key_c = { + "name": "key for door c", + "type": "key", + "target": door_c, +} + +key_d = { + "name": "key for door d", + "type": "key", + "target": door_d, +} + +key_e = { + "name": "key for door e", + "type": "key", + "target": door_e, +} + +# Defining belongings + +dog = "Buddy, your faithful friend" + +valuables = ['wallet', 'mobile phone', 'watch'] + +all_rooms = [game_room, bedroom_1, bedroom_2, living_room, backyard, outside] + +all_doors = [door_a, door_b, door_c, door_d, door_e] + +# defining object relations + +object_relations = { + # rooms - furnishings + "game room": [couch, piano, suspicious_painting, door_a], + "bedroom 1": [queen_bed, door_a, door_b, door_c], + "bedroom 2": [double_bed, dresser, door_b], + "living room": [dining_table, library, door_c, door_d], + "backyard": [tree, kennel, door_d, door_e], + "outside": [door_d], + # furnishings - items + "piano": [key_a], + "queen bed": [key_b], + "double bed": [key_c], + "dresser": [key_d], + "library": [key_e], + # doors - rooms + "door a": [game_room, bedroom_1], + "door b": [bedroom_1, bedroom_2], + "door c": [bedroom_1, living_room], + "door d": [living_room, backyard], + "door e": [backyard, outside] +} + +# Game state + +INIT_GAME_STATE = { + "current_room": game_room, + "keys_collected": [], + "items_collected": [], + "target_room": outside, + "game_won": False +} + +def clear(): + "Clear terminal output" + if name == 'nt': + _ = system('cls') # windows + else: + _ = system('clear') # mac + linux + +def linebreak(): + """ + Print a line break + """ + print("\n\n") + +def start_game(): + play_music() + """ + Start the game + """ + print( + """ + You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. + You remember walking the dog - but you have no idea what happened. + You realize that your other valuables are also missing. + You feel some unknown danger is approaching and you must get out of the house, NOW! + """) + print(Fore.RED + 'You only have 3:00 minutes left!') + print(Style.RESET_ALL) + + pygame.init() + pygame.display.set_caption('RUN') + icon = pygame.image.load('evacuacion.png') + pygame.display.set_icon(icon) + screen = pygame.display.set_mode((800, 600)) + screen.blit(pygame.image.load(game_room["image"]),(0,0)) + pygame.display.update() + play_room(game_state["current_room"]) + +def play_room(room): + "Play a room." + curr_room = game_state["current_room"] + game_state["current_room"] = room + if(game_state["current_room"] == game_state["target_room"]): + game_state['game_won'] = True + clear() + print("Congrats! You escaped the room!") + if valuables in game_state['items_collected']: + print(f"You also managed to get back your wallet, your mobile phone and your watch!") + if dog in game_state['items_collected']: + print(f"Best of all: you freed your dog Buddy!!") + print("šŸŽ‰ GOOD JOB!! šŸŽ‰") + return + else: + print("You are now in " + room["name"]) + if curr_room != room: + draw_room(room["image"]) + intended_action = input("What would you like to do? Type 'explore' or 'examine'?").strip() + if intended_action == "explore": + explore_room(room) + play_room(room) + elif intended_action == "examine": + examine_item(input("What would you like to examine?").strip()) + else: + print("Not sure what you mean. Type 'explore' or 'examine'.") + play_room(room) + linebreak() + +def explore_room(room): + """ + Explore a room. List all items belonging to this room. + """ + items = [i["name"] for i in object_relations[room["name"]]] + print("\nYou explore the room. This is " + room["name"] + ". You find " + ", ".join(items)) + +def get_next_room_of_door(door, current_room): + """ + From object_relations, find the two rooms connected to the given door. + Return the room that is not the current_room. + """ + connected_rooms = object_relations[door["name"]] + for room in connected_rooms: + if(not current_room == room): + return room + +def examine_item(item_name): + "Examine an item which can be a door or furniture." + current_room = game_state["current_room"] + next_room = "" + output = None + clear() + + for item in object_relations[current_room["name"]]: + if(item["name"] == item_name): + output = "\nYou examine " + item_name + ". " + draw_item(item["image"]) #needs fixing, the picture only loads for a split second + if(item["type"] == "door"): + have_key = False + for key in game_state["keys_collected"]: + if(key["target"] == item): + have_key = True + if(have_key): + output += "You unlock it with a key you have." + next_room = get_next_room_of_door(item, current_room) + else: + output += "It is locked but you don't have the key." + elif (item["type"] == "safe"): + open_safe(current_room) + elif (item["type"] == "dog"): + free_dog(current_room) + else: + if(item["name"] in object_relations and len(object_relations[item["name"]])>0): + item_found = object_relations[item["name"]].pop() + game_state["keys_collected"].append(item_found) + output += "You find " + item_found["name"] + "." + else: + output += "There isn't anything interesting about it." + print(output) + break + + if(output is None): + print("The item you requested is not found in the current room.") + + if(next_room and input("Do you want to go to the next room? Enter 'yes' or 'no'").strip() == 'yes'): + # if(next_room): + play_room(next_room) + else: + play_room(current_room) + +# safe guess combination mini game + +def open_safe(current_room): + "Guess combination mini game intro" + if valuables not in game_state['items_collected']: + print( + """ + The picture folds to the side. + Behind it is a safe with a rusty 3 digit lock! + You wonder if you could figure out the right combination by intuition. + """ + ) + try_safe(current_room) + else: + print("\nYou have already emptied the safe.") + play_room(current_room) + +def try_safe(current_room): + "Guess combination mini game yes/no" + try_input = input("Do you want to give it a try? Enter 'yes' or 'no'") + if try_input == 'yes': + combination = math.floor(random.random() * 1000) + guess_digits(current_room, combination) + elif try_input == 'no': + play_room(current_room) + else: + try_safe(current_room) + +def guess_digits(current_room, combination): + "Guess combination mini game logic" + guess = input("Test your lock picking skill (3-digit number):") + if guess.isnumeric(): + guess = int(guess) + if 0 <= guess <= 999: + if guess < combination: + print(f"The heavier turning wheels tell you that the combination needs to be HIGHER than {str(guess).zfill(3)}!") + guess_digits(current_room, combination) + elif guess > combination: + print(f"The easier turning wheels tell you that the combination needs to be LOWER than {str(guess).zfill(3)}!") + guess_digits(current_room, combination) + else: + print(""" + šŸŽŠ BINGO! šŸŽŠ + Your open the safe and get back your ...\n + => šŸ’° wallet + => šŸ“± mobile phone + => ⌚ watch + """) + game_state['items_collected'].append(valuables) + play_room(current_room) + else: + guess_digits(current_room, combination) + else: + guess_digits(current_room, combination) + +# free dog switch mini game + +def free_dog(current_room): + "Switch combination mini game intro" + if dog not in game_state['items_collected']: + print( + """ + Buddy!! + You finally found your beloved dog. + Someone locked Buddy in a kennel. + """ + ) + try_kennel(current_room) + else: + print("\nYou have already opened the kennel and freed Buddy.") + play_room(current_room) + +def try_kennel(current_room): + "Switch combination mini game yes/no" + try_input = input("Do you want to try to free him? Enter 'yes' or 'no'") + if try_input == 'yes': + combination = [round(random.random()) for switch in range(4)] + user_start = [round(random.random()) for switch in range(4)] + while combination == user_start: + user_start = [round(random.random()) for switch in range(4)] + guess_switches(current_room, combination, user_start) + elif try_input == 'no': + play_room(current_room) + else: + try_kennel(current_room) + +def guess_switches(current_room, combination, user_start): + "Switch combination mini game logic" + switch_icons = ['ā­•', 'āŒ'] + while combination != user_start: + clear() + print('The kennel is locked by a combination of switches.\n\n') + print('\t\t' + ' '.join(['1', ' 2', ' 3', ' 4'])) + print('\t\t' + ' '.join([switch_icons[switch] for switch in user_start])) + switch_key = input('\n\nTry one single digit at a time!') + if switch_key == '1': + user_start[0] = 1 if user_start[0] == 0 else 0 + elif switch_key == '2': + user_start[1] = 1 if user_start[1] == 0 else 0 + elif switch_key == '3': + user_start[2] = 1 if user_start[2] == 0 else 0 + elif switch_key == '4': + user_start[3] = 1 if user_start[3] == 0 else 0 + else: + continue + clear() + print('You found the correct combination!!\n\n') + print('\t\t' + ' '.join(['1', ' 2', ' 3', ' 4'])) + print('\t\t' + ' '.join([switch_icons[switch] for switch in user_start])) + print(""" + \n\nšŸŽŠ BINGO! šŸŽŠ + Your open the kennel and free ...\n + => 🐶 Buddy + """) + game_state['items_collected'].append(dog) + play_room(current_room) + +def draw_room(room): + #creates images of the rooms in the game, that display when the player enters the room + screen = pygame.display.set_mode((800, 600)) + screen.blit(pygame.image.load(room),(0,0)) + pygame.display.update() + +def draw_item(item): + #creates images of the different items in the game that display when they are examined by the player + screen = pygame.display.set_mode((800, 600)) + screen.blit(pygame.image.load(item),(0,0)) + pygame.display.update() + +def play_music(): + pygame.init() + pygame.mixer.music.load('horror.mp3') + pygame.mixer.music.play(-1) + +game_state = INIT_GAME_STATE.copy() + +start_game() \ No newline at end of file diff --git a/evacuacion.png b/evacuacion.png new file mode 100644 index 00000000..701a7ad7 Binary files /dev/null and b/evacuacion.png differ diff --git a/gameroom.jpg b/gameroom.jpg new file mode 100644 index 00000000..432f646a Binary files /dev/null and b/gameroom.jpg differ diff --git a/horror.mp3 b/horror.mp3 new file mode 100644 index 00000000..bc9d2ab7 Binary files /dev/null and b/horror.mp3 differ diff --git a/kennel.jpg b/kennel.jpg new file mode 100644 index 00000000..b341f03d Binary files /dev/null and b/kennel.jpg differ diff --git a/library.jpg b/library.jpg new file mode 100644 index 00000000..43eb66d5 Binary files /dev/null and b/library.jpg differ diff --git a/livingroom.jpg b/livingroom.jpg new file mode 100644 index 00000000..4a26de1c Binary files /dev/null and b/livingroom.jpg differ diff --git a/livingroombackup.jpg b/livingroombackup.jpg new file mode 100644 index 00000000..e714bb5f Binary files /dev/null and b/livingroombackup.jpg differ diff --git a/outside.jpg b/outside.jpg new file mode 100644 index 00000000..e0a87111 Binary files /dev/null and b/outside.jpg differ diff --git a/painting.jpg b/painting.jpg new file mode 100644 index 00000000..a68fc717 Binary files /dev/null and b/painting.jpg differ diff --git a/piano.jpg b/piano.jpg new file mode 100644 index 00000000..95fea0b9 Binary files /dev/null and b/piano.jpg differ diff --git a/queenbed.jpg b/queenbed.jpg new file mode 100644 index 00000000..c9274539 Binary files /dev/null and b/queenbed.jpg differ diff --git a/tempCodeRunnerFile.py b/tempCodeRunnerFile.py new file mode 100644 index 00000000..6e6f4c4a --- /dev/null +++ b/tempCodeRunnerFile.py @@ -0,0 +1,2 @@ +LL) + elif time_sec == 59: \ No newline at end of file diff --git a/tree.jpg b/tree.jpg new file mode 100644 index 00000000..a56c1a4d Binary files /dev/null and b/tree.jpg differ diff --git a/your-code/sample-code.ipynb b/your-code/sample-code.ipynb deleted file mode 100644 index a6f8a94d..00000000 --- a/your-code/sample-code.ipynb +++ /dev/null @@ -1,248 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# define rooms and items\n", - "\n", - "couch = {\n", - " \"name\": \"couch\",\n", - " \"type\": \"furniture\",\n", - "}\n", - "\n", - "door_a = {\n", - " \"name\": \"door a\",\n", - " \"type\": \"door\",\n", - "}\n", - "\n", - "key_a = {\n", - " \"name\": \"key for door a\",\n", - " \"type\": \"key\",\n", - " \"target\": door_a,\n", - "}\n", - "\n", - "piano = {\n", - " \"name\": \"piano\",\n", - " \"type\": \"furniture\",\n", - "}\n", - "\n", - "game_room = {\n", - " \"name\": \"game room\",\n", - " \"type\": \"room\",\n", - "}\n", - "\n", - "outside = {\n", - " \"name\": \"outside\"\n", - "}\n", - "\n", - "all_rooms = [game_room, outside]\n", - "\n", - "all_doors = [door_a]\n", - "\n", - "# define which items/rooms are related\n", - "\n", - "object_relations = {\n", - " \"game room\": [couch, piano, door_a],\n", - " \"piano\": [key_a],\n", - " \"outside\": [door_a],\n", - " \"door a\": [game_room, outside]\n", - "}\n", - "\n", - "# define game state. Do not directly change this dict. \n", - "# Instead, when a new game starts, make a copy of this\n", - "# dict and use the copy to store gameplay state. This \n", - "# way you can replay the game multiple times.\n", - "\n", - "INIT_GAME_STATE = {\n", - " \"current_room\": game_room,\n", - " \"keys_collected\": [],\n", - " \"target_room\": outside\n", - "}" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "def linebreak():\n", - " \"\"\"\n", - " Print a line break\n", - " \"\"\"\n", - " print(\"\\n\\n\")\n", - "\n", - "def start_game():\n", - " \"\"\"\n", - " Start the game\n", - " \"\"\"\n", - " print(\"You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. You don't remember why you are here and what had happened before. You feel some unknown danger is approaching and you must get out of the house, NOW!\")\n", - " play_room(game_state[\"current_room\"])\n", - "\n", - "def play_room(room):\n", - " \"\"\"\n", - " Play a room. First check if the room being played is the target room.\n", - " If it is, the game will end with success. Otherwise, let player either \n", - " explore (list all items in this room) or examine an item found here.\n", - " \"\"\"\n", - " game_state[\"current_room\"] = room\n", - " if(game_state[\"current_room\"] == game_state[\"target_room\"]):\n", - " print(\"Congrats! You escaped the room!\")\n", - " else:\n", - " print(\"You are now in \" + room[\"name\"])\n", - " intended_action = input(\"What would you like to do? Type 'explore' or 'examine'?\").strip()\n", - " if intended_action == \"explore\":\n", - " explore_room(room)\n", - " play_room(room)\n", - " elif intended_action == \"examine\":\n", - " examine_item(input(\"What would you like to examine?\").strip())\n", - " else:\n", - " print(\"Not sure what you mean. Type 'explore' or 'examine'.\")\n", - " play_room(room)\n", - " linebreak()\n", - "\n", - "def explore_room(room):\n", - " \"\"\"\n", - " Explore a room. List all items belonging to this room.\n", - " \"\"\"\n", - " items = [i[\"name\"] for i in object_relations[room[\"name\"]]]\n", - " print(\"You explore the room. This is \" + room[\"name\"] + \". You find \" + \", \".join(items))\n", - "\n", - "def get_next_room_of_door(door, current_room):\n", - " \"\"\"\n", - " From object_relations, find the two rooms connected to the given door.\n", - " Return the room that is not the current_room.\n", - " \"\"\"\n", - " connected_rooms = object_relations[door[\"name\"]]\n", - " for room in connected_rooms:\n", - " if(not current_room == room):\n", - " return room\n", - "\n", - "def examine_item(item_name):\n", - " \"\"\"\n", - " Examine an item which can be a door or furniture.\n", - " First make sure the intended item belongs to the current room.\n", - " Then check if the item is a door. Tell player if key hasn't been \n", - " collected yet. Otherwise ask player if they want to go to the next\n", - " room. If the item is not a door, then check if it contains keys.\n", - " Collect the key if found and update the game state. At the end,\n", - " play either the current or the next room depending on the game state\n", - " to keep playing.\n", - " \"\"\"\n", - " current_room = game_state[\"current_room\"]\n", - " next_room = \"\"\n", - " output = None\n", - " \n", - " for item in object_relations[current_room[\"name\"]]:\n", - " if(item[\"name\"] == item_name):\n", - " output = \"You examine \" + item_name + \". \"\n", - " if(item[\"type\"] == \"door\"):\n", - " have_key = False\n", - " for key in game_state[\"keys_collected\"]:\n", - " if(key[\"target\"] == item):\n", - " have_key = True\n", - " if(have_key):\n", - " output += \"You unlock it with a key you have.\"\n", - " next_room = get_next_room_of_door(item, current_room)\n", - " else:\n", - " output += \"It is locked but you don't have the key.\"\n", - " else:\n", - " if(item[\"name\"] in object_relations and len(object_relations[item[\"name\"]])>0):\n", - " item_found = object_relations[item[\"name\"]].pop()\n", - " game_state[\"keys_collected\"].append(item_found)\n", - " output += \"You find \" + item_found[\"name\"] + \".\"\n", - " else:\n", - " output += \"There isn't anything interesting about it.\"\n", - " print(output)\n", - " break\n", - "\n", - " if(output is None):\n", - " print(\"The item you requested is not found in the current room.\")\n", - " \n", - " if(next_room and input(\"Do you want to go to the next room? Ener 'yes' or 'no'\").strip() == 'yes'):\n", - " play_room(next_room)\n", - " else:\n", - " play_room(current_room)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. You don't remember why you are here and what had happened before. You feel some unknown danger is approaching and you must get out of the house, NOW!\n", - "You are now in game room\n", - "What would you like to do? Type 'explore' or 'examine'?explore\n", - "You explore the room. This is game room. You find couch, piano, door a\n", - "You are now in game room\n", - "What would you like to do? Type 'explore' or 'examine'?examine\n", - "What would you like to examine?door a\n", - "You examine door a. It is locked but you don't have the key.\n", - "You are now in game room\n", - "What would you like to do? Type 'explore' or 'examine'?examine\n", - "What would you like to examine?piano\n", - "You examine piano. You find key for door a.\n", - "You are now in game room\n", - "What would you like to do? Type 'explore' or 'examine'?examine\n", - "What would you like to examine?door a\n", - "You examine door a. You unlock it with a key you have.\n", - "Do you want to go to the next room? Ener 'yes' or 'no'yes\n", - "Congrats! You escaped the room!\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" - ] - } - ], - "source": [ - "game_state = INIT_GAME_STATE.copy()\n", - "\n", - "start_game()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}