diff --git a/Project1/.ipynb_checkpoints/Escape_room_with_images-checkpoint.ipynb b/Project1/.ipynb_checkpoints/Escape_room_with_images-checkpoint.ipynb new file mode 100644 index 00000000..0276587d --- /dev/null +++ b/Project1/.ipynb_checkpoints/Escape_room_with_images-checkpoint.ipynb @@ -0,0 +1,410 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'cv2'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_27200/3933605323.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 114\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 115\u001b[0m \u001b[1;31m#Images for the rooms\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 116\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 117\u001b[0m \u001b[0mimage_start_game\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr\"C:\\Users\\suparna\\Desktop\\Project 1\\images\\open game.jpg\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 118\u001b[0m \u001b[0mimage_game_room\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr\"C:\\Users\\Afee\\Desktop\\Project 1\\images\\game room.jpg\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'cv2'" + ] + } + ], + "source": [ + "# define rooms and items\n", + "# gameroom\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", + "\n", + "\n", + "# bedroom 1\n", + "\n", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "queen_bed = {\n", + " \"name\": \"queen bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_b = {\n", + " \"name\": \"door b\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "key_b = {\n", + " \"name\": \"key for door b\",\n", + " \"type\": \"key\",\n", + " \"target\": door_b,\n", + "}\n", + "\n", + "# bedroom 2\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "double_bed = {\n", + " \"name\": \"double bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "dresser = {\n", + " \"name\": \"dresser\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_b = {\n", + " \"name\": \"door b\",\n", + " \"type\": \"door\",\n", + "}\n", + "door_d = {\n", + " \"name\": \"door d\",\n", + " \"type\": \"door\",\n", + "}\n", + "key_c = {\n", + " \"name\": \"key for door c\",\n", + " \"type\": \"key\",\n", + " \"target\": door_c,\n", + "}\n", + "\n", + "key_d = {\n", + " \"name\": \"key for door d\",\n", + " \"type\": \"key\",\n", + " \"target\": door_d,\n", + "}\n", + "\n", + "# living room\n", + "\n", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\",\n", + "}\n", + "dining_table= {\n", + " \"name\": \"dining table\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "#Images for the rooms\n", + "import cv2\n", + "image_start_game = cv2.imread(\"open game.jpg\")\n", + "image_game_room = cv2.imread(\"game room.jpg\")\n", + "image_bedroom1 = cv2.imread(\"bedroom 1.jpg\")\n", + "image_bedroom2 = cv2.imread(\"bedroom 2.jpg\")\n", + "image_living_room = cv2.imread(\"living room.jpg\")\n", + "image_outside = cv2.imread(\"outside.jpg\")\n", + "\n", + "#Images for the objects\n", + "import cv2\n", + "image_piano = cv2.imread(\"piano.jpg\")\n", + "image_couch = cv2.imread(\"couch.jpg\")\n", + "image_door_a = cv2.imread(\"door a.jpg\")\n", + "image_key_a = cv2.imread(\"key a.jpg\")\n", + "image_queen_bed = cv2.imread(\"queen bed.jpg\")\n", + "image_door_b = cv2.imread(\"door b.jpg\")\n", + "image_door_c = cv2.imread(\"door c.jpg\")\n", + "image_double_bed = cv2.imread(\"double bed.jpg\")\n", + "image_dresser = cv2.imread(\"dresser.jpg\")\n", + "image_dining_table = cv2.imread(\"dining table.jpg\")\n", + "image_key_d = cv2.imread(\"door d.jpg\")\n", + "image_door_d = cv2.imread(\"key d.jpg\")\n", + "image_key_b = cv2.imread(\"key b.jpg\")\n", + "image_key_c = cv2.imread(\"key c.jpg\")\n", + "\n", + "\n", + "\n", + "all_images_rooms = [image_game_room, image_bedroom1, image_bedroom2, image_living_room,image_outside]\n", + "\n", + "all_rooms = [game_room, bedroom_1, bedroom_2, living_room]\n", + "\n", + "all_doors = [door_a, door_b, door_c, door_d]\n", + "\n", + "#define which images are related to which room\n", + "\n", + "image_relations_rooms = {\n", + " \"game room\": image_game_room,\n", + " \"living room\": image_living_room,\n", + " \"bedroom 1\": image_bedroom1,\n", + " \"bedroom 2\": image_bedroom2,\n", + " \"living room\": image_living_room\n", + "}\n", + "\n", + "# define which items/rooms are related\n", + "\n", + "object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"piano\": [key_a],\n", + " \"living room\": [door_c], \n", + " \"door a\": [game_room, bedroom_1], \n", + " \"bedroom 1\": [queen_bed, door_a, door_b, door_c],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"queen bed\": [key_b],\n", + " \"door c\": [bedroom_1, living_room],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"door d\": [living_room, outside], \n", + " \"living room\":[dining_table,door_d],\n", + " \"outside\": [door_d]\n", + "}\n", + "\n", + "object_picture_relations = {\n", + " \"couch\": image_couch,\n", + " \"piano\": image_piano,\n", + " \"door a\": image_door_a,\n", + " \"key a\" : image_key_a,\n", + " \"queen bed\": image_queen_bed,\n", + " \"door b\": image_door_b,\n", + " \"door c\": image_door_c,\n", + " \"key b\": image_key_b,\n", + " \"double bed\": image_double_bed,\n", + " \"dresser\": image_dresser,\n", + " \"key c\": image_key_c,\n", + " \"key d\": image_key_d,\n", + " \"door d\": image_door_d,\n", + " \"dining table\": image_dining_table\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", + "def play_image_room(x):\n", + " cv2.imshow('image',x)\n", + " cv2.waitKey(4000) #milliseconds\n", + " cv2.destroyAllWindows()\n", + " return x\n", + "\n", + "def play_image_object(picture):\n", + " cv2.imshow('image',picture)\n", + " cv2.waitKey(2000) #milliseconds\n", + " cv2.destroyAllWindows()\n", + " return picture\n", + "\n", + "\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", + "\n", + " print(\"You wake up on a couch and find yourself in an abandoned house which you have never been to before. You don't remember why you are here and what had happened before. You so something and hope you are dreaming. You must get out of the house, NOW!\")\n", + " play_image_room(image_start_game)\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", + " play_image_room(image_outside)\n", + " print(\"Congrats! You are finally free!\")\n", + " else:\n", + " print(\"You are now in \" + room[\"name\"])\n", + " intended_action = input(\"What would you like to do? Type 'explore' or 'check'?\").strip()\n", + " if intended_action == \"explore\":\n", + " explore_room(room)\n", + " play_room(room)\n", + " elif intended_action == \"check\":\n", + " check_item(input(\"What would you like to check?\").strip())\n", + " else:\n", + " print(\"I do not understand what you mean. Type 'explore' or 'check'.\")\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\"])\n", + " play_image_room(image_relations_rooms[room[\"name\"]])\n", + " print(\"You find \" + \", \".join(items))\n", + " \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 check_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 check \" + item_name + \". \"\n", + " play_image_object(object_picture_relations[item_name]) ################important\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 the door with a key you have.\"\n", + " next_room = get_next_room_of_door(item, current_room)\n", + " else:\n", + " output += \"The door 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 new to find.\"\n", + " print(output)\n", + " break\n", + "\n", + " if(output is None):\n", + " print(\"The item you asked is not found in the current room.\")\n", + " \n", + " if(next_room and input(\"Do you want to go to the next room? Enter '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": [ + { + "ename": "NameError", + "evalue": "name 'INIT_GAME_STATE' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_27200/1920013307.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgame_state\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mINIT_GAME_STATE\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mstart_game\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: name 'INIT_GAME_STATE' is not defined" + ] + } + ], + "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 (ipykernel)", + "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.9.7" + }, + "vscode": { + "interpreter": { + "hash": "be84b5ffa8a37ab11574afbdc8d52b1d8f0f0e56b1ef9e14008d1f1b8a7a8b73" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Project1/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/Project1/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 00000000..363fcab7 --- /dev/null +++ b/Project1/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Project1/Escape_room_with_images.ipynb b/Project1/Escape_room_with_images.ipynb new file mode 100644 index 00000000..dc41a56b --- /dev/null +++ b/Project1/Escape_room_with_images.ipynb @@ -0,0 +1,498 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#!pip3 install opencv-python\n", + "# define rooms and items\n", + "# gameroom\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", + "\n", + "\n", + "# bedroom 1\n", + "\n", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "queen_bed = {\n", + " \"name\": \"queen bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_b = {\n", + " \"name\": \"door b\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "key_b = {\n", + " \"name\": \"key for door b\",\n", + " \"type\": \"key\",\n", + " \"target\": door_b,\n", + "}\n", + "\n", + "# bedroom 2\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "double_bed = {\n", + " \"name\": \"double bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "dresser = {\n", + " \"name\": \"dresser\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_b = {\n", + " \"name\": \"door b\",\n", + " \"type\": \"door\",\n", + "}\n", + "door_d = {\n", + " \"name\": \"door d\",\n", + " \"type\": \"door\",\n", + "}\n", + "key_c = {\n", + " \"name\": \"key for door c\",\n", + " \"type\": \"key\",\n", + " \"target\": door_c,\n", + "}\n", + "\n", + "key_d = {\n", + " \"name\": \"key for door d\",\n", + " \"type\": \"key\",\n", + " \"target\": door_d,\n", + "}\n", + "\n", + "# living room\n", + "\n", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\",\n", + "}\n", + "dining_table= {\n", + " \"name\": \"dining table\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "#Images for the rooms\n", + "import cv2\n", + "image_start_game = cv2.imread(\"open game.jpg\")\n", + "image_game_room = cv2.imread(\"game room.jpg\")\n", + "image_bedroom1 = cv2.imread(\"bedroom 1.jpg\")\n", + "image_bedroom2 = cv2.imread(\"bedroom 2.jpg\")\n", + "image_living_room = cv2.imread(\"living room.jpg\")\n", + "image_outside = cv2.imread(\"outside.jpg\")\n", + "\n", + "#Images for the objects\n", + "import cv2\n", + "image_piano = cv2.imread(\"piano.jpg\")\n", + "image_couch = cv2.imread(\"couch.jpg\")\n", + "image_door_a = cv2.imread(\"door a.jpg\")\n", + "image_key_a = cv2.imread(\"key a.jpg\")\n", + "image_queen_bed = cv2.imread(\"queen bed.jpg\")\n", + "image_door_b = cv2.imread(\"door b.jpg\")\n", + "image_door_c = cv2.imread(\"door c.jpg\")\n", + "image_double_bed = cv2.imread(\"double bed.jpg\")\n", + "image_dresser = cv2.imread(\"dresser.jpg\")\n", + "image_dining_table = cv2.imread(\"dining table.jpg\")\n", + "image_key_d = cv2.imread(\"door d.jpg\")\n", + "image_door_d = cv2.imread(\"key d.jpg\")\n", + "image_key_b = cv2.imread(\"key b.jpg\")\n", + "image_key_c = cv2.imread(\"key c.jpg\")\n", + "\n", + "\n", + "\n", + "all_images_rooms = [image_game_room, image_bedroom1, image_bedroom2, image_living_room,image_outside]\n", + "\n", + "all_rooms = [game_room, bedroom_1, bedroom_2, living_room]\n", + "\n", + "all_doors = [door_a, door_b, door_c, door_d]\n", + "\n", + "#define which images are related to which room\n", + "\n", + "image_relations_rooms = {\n", + " \"game room\": image_game_room,\n", + " \"living room\": image_living_room,\n", + " \"bedroom 1\": image_bedroom1,\n", + " \"bedroom 2\": image_bedroom2,\n", + " \"living room\": image_living_room\n", + "}\n", + "\n", + "# define which items/rooms are related\n", + "\n", + "object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"piano\": [key_a],\n", + " \"living room\": [door_c], \n", + " \"door a\": [game_room, bedroom_1], \n", + " \"bedroom 1\": [queen_bed, door_a, door_b, door_c],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"queen bed\": [key_b],\n", + " \"door c\": [bedroom_1, living_room],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"door d\": [living_room, outside], \n", + " \"living room\":[dining_table,door_d],\n", + " \"outside\": [door_d]\n", + "}\n", + "\n", + "object_picture_relations = {\n", + " \"couch\": image_couch,\n", + " \"piano\": image_piano,\n", + " \"door a\": image_door_a,\n", + " \"key a\" : image_key_a,\n", + " \"queen bed\": image_queen_bed,\n", + " \"door b\": image_door_b,\n", + " \"door c\": image_door_c,\n", + " \"key b\": image_key_b,\n", + " \"double bed\": image_double_bed,\n", + " \"dresser\": image_dresser,\n", + " \"key c\": image_key_c,\n", + " \"key d\": image_key_d,\n", + " \"door d\": image_door_d,\n", + " \"dining table\": image_dining_table\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", + "def play_image_room(x):\n", + " cv2.imshow('image',x)\n", + " cv2.waitKey(4000) #milliseconds\n", + " cv2.destroyAllWindows()\n", + " return x\n", + "\n", + "def play_image_object(picture):\n", + " cv2.imshow('image',picture)\n", + " cv2.waitKey(2000) #milliseconds\n", + " cv2.destroyAllWindows()\n", + " return picture\n", + "\n", + "\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", + "\n", + " print(\"You wake up on a couch and find yourself in an abandoned house which you have never been to before. You don't remember why you are here and what had happened before. You so something and hope you are dreaming. You must get out of the house, NOW!\")\n", + " play_image_room(image_start_game)\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", + " play_image_room(image_outside)\n", + " print(\"Congrats! You are finally free!\")\n", + " else:\n", + " print(\"You are now in \" + room[\"name\"])\n", + " intended_action = input(\"What would you like to do? Type 'explore' or 'check'?\").strip()\n", + " if intended_action == \"explore\":\n", + " explore_room(room)\n", + " play_room(room)\n", + " elif intended_action == \"check\":\n", + " check_item(input(\"What would you like to check?\").strip())\n", + " else:\n", + " print(\"I do not understand what you mean. Type 'explore' or 'check'.\")\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\"])\n", + " play_image_room(image_relations_rooms[room[\"name\"]])\n", + " print(\"You find \" + \", \".join(items))\n", + " \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 check_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 check \" + item_name + \". \"\n", + " play_image_object(object_picture_relations[item_name]) ################important\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 the door with a key you have.\"\n", + " next_room = get_next_room_of_door(item, current_room)\n", + " else:\n", + " output += \"The door 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 new to find.\"\n", + " print(output)\n", + " break\n", + "\n", + " if(output is None):\n", + " print(\"The item you asked is not found in the current room.\")\n", + " \n", + " if(next_room and input(\"Do you want to go to the next room? Enter '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 an abandoned house which you have never been to before. You don't remember why you are here and what had happened before. You so something and hope you are dreaming. 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 'check'?explore\n", + "You explore the room. This is game room\n", + "You find couch, piano, door a\n", + "You are now in game room\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?piano\n", + "You check piano. You find key for door a .\n", + "You are now in game room\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?door a\n", + "You check door a. You unlock the door with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'check'?explore\n", + "You explore the room. This is bedroom 1\n", + "You find queen bed, door a, door b, door c\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?queen bed\n", + "You check queen bed. You find key for door b.\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?door b\n", + "You check door b. You unlock the door with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'check'?explore\n", + "You explore the room. This is bedroom 2\n", + "You find double bed, dresser, door b\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?double bed\n", + "You check double bed. You find key for door c.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?dresser\n", + "You check dresser. You find key for door d.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?door b\n", + "You check door b. You unlock the door with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'check'?yes\n", + "I do not understand what you mean. Type 'explore' or 'check'.\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?door c\n", + "You check door c. You unlock the door with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in living room\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?dining table\n", + "You check dining table. There isn't anything new to find.\n", + "You are now in living room\n", + "What would you like to do? Type 'explore' or 'check'?check\n", + "What would you like to check?door d\n", + "You check door d. You unlock the door with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "Congrats! You are finally free!\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\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 (ipykernel)", + "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.9.7" + }, + "vscode": { + "interpreter": { + "hash": "be84b5ffa8a37ab11574afbdc8d52b1d8f0f0e56b1ef9e14008d1f1b8a7a8b73" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Project1/Untitled.ipynb b/Project1/Untitled.ipynb new file mode 100644 index 00000000..7087be08 --- /dev/null +++ b/Project1/Untitled.ipynb @@ -0,0 +1,66 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "fa33543f", + "metadata": {}, + "outputs": [], + "source": [ + "#!pip3 install opencv-python\n", + "import cv2\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "8963e8ce", + "metadata": {}, + "outputs": [], + "source": [ + "#import numpy as np\n", + "image_piano = cv2.imread(\"piano.jpg\") #use an absolute path\n", + "cv2.imshow('image', image_piano)\n", + "cv2.waitKey(0)\n", + "cv2.destroyAllWindows()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae0f1116", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "391eab8d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Project1/bedroom 1.jpg b/Project1/bedroom 1.jpg new file mode 100644 index 00000000..c04e3a6c Binary files /dev/null and b/Project1/bedroom 1.jpg differ diff --git a/Project1/bedroom 2.jpg b/Project1/bedroom 2.jpg new file mode 100644 index 00000000..0c99bf72 Binary files /dev/null and b/Project1/bedroom 2.jpg differ diff --git a/Project1/couch.jpg b/Project1/couch.jpg new file mode 100644 index 00000000..c52db279 Binary files /dev/null and b/Project1/couch.jpg differ diff --git a/Project1/dining table.jpg b/Project1/dining table.jpg new file mode 100644 index 00000000..70ddda23 Binary files /dev/null and b/Project1/dining table.jpg differ diff --git a/Project1/door a.jpg b/Project1/door a.jpg new file mode 100644 index 00000000..9abab443 Binary files /dev/null and b/Project1/door a.jpg differ diff --git a/Project1/door b.jpg b/Project1/door b.jpg new file mode 100644 index 00000000..5cf25c59 Binary files /dev/null and b/Project1/door b.jpg differ diff --git a/Project1/door c.jpg b/Project1/door c.jpg new file mode 100644 index 00000000..fff0f2c1 Binary files /dev/null and b/Project1/door c.jpg differ diff --git a/Project1/door d.jpg b/Project1/door d.jpg new file mode 100644 index 00000000..ac835d9f Binary files /dev/null and b/Project1/door d.jpg differ diff --git a/Project1/door1.jpg b/Project1/door1.jpg new file mode 100644 index 00000000..47fb36f5 Binary files /dev/null and b/Project1/door1.jpg differ diff --git a/Project1/door3.jpg b/Project1/door3.jpg new file mode 100644 index 00000000..516242bd Binary files /dev/null and b/Project1/door3.jpg differ diff --git a/Project1/double bed.jpg b/Project1/double bed.jpg new file mode 100644 index 00000000..0c99bf72 Binary files /dev/null and b/Project1/double bed.jpg differ diff --git a/Project1/dresser.jpg b/Project1/dresser.jpg new file mode 100644 index 00000000..976019ab Binary files /dev/null and b/Project1/dresser.jpg differ diff --git a/Project1/game room.jpg b/Project1/game room.jpg new file mode 100644 index 00000000..5889d55f Binary files /dev/null and b/Project1/game room.jpg differ diff --git a/Project1/game room5.jpg b/Project1/game room5.jpg new file mode 100644 index 00000000..d79dc732 Binary files /dev/null and b/Project1/game room5.jpg differ diff --git a/Project1/key a.jpg b/Project1/key a.jpg new file mode 100644 index 00000000..ceef8c40 Binary files /dev/null and b/Project1/key a.jpg differ diff --git a/Project1/key b.jpg b/Project1/key b.jpg new file mode 100644 index 00000000..3dced7e7 Binary files /dev/null and b/Project1/key b.jpg differ diff --git a/Project1/key c.jpg b/Project1/key c.jpg new file mode 100644 index 00000000..e71a1b95 Binary files /dev/null and b/Project1/key c.jpg differ diff --git a/Project1/key d.jpg b/Project1/key d.jpg new file mode 100644 index 00000000..5f82edf6 Binary files /dev/null and b/Project1/key d.jpg differ diff --git a/Project1/living room.jpg b/Project1/living room.jpg new file mode 100644 index 00000000..9253d536 Binary files /dev/null and b/Project1/living room.jpg differ diff --git a/Project1/open game.jpg b/Project1/open game.jpg new file mode 100644 index 00000000..8fc631d0 Binary files /dev/null and b/Project1/open game.jpg differ diff --git a/Project1/outside.jpg b/Project1/outside.jpg new file mode 100644 index 00000000..c11003d6 Binary files /dev/null and b/Project1/outside.jpg differ diff --git a/Project1/piano.jpg b/Project1/piano.jpg new file mode 100644 index 00000000..c81dedcb Binary files /dev/null and b/Project1/piano.jpg differ diff --git a/Project1/queen bed.jpg b/Project1/queen bed.jpg new file mode 100644 index 00000000..ddfa5ad8 Binary files /dev/null and b/Project1/queen bed.jpg differ