Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 242 additions & 0 deletions your-code/GAME.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "ca6d6553",
"metadata": {},
"outputs": [],
"source": [
"# -*- coding: utf-8 -*-\n",
"# define rooms and items\n",
"\n",
"couch = {\n",
" \"name\": \"couch\",\n",
" \"type\": \"furniture\",\n",
"}\n",
"\n",
"queen_bed = {\n",
" \"name\": \"queen bed\",\n",
" \"type\": \"furniture\",\n",
"}\n",
"\n",
"double_bed = {\n",
" \"name\": \"double bed\",\n",
" \"type\": \"furniture\",\n",
"}\n",
"\n",
"dresser = {\n",
" \"name\": \"dresser\",\n",
" \"type\": \"furniture\",\n",
"}\n",
"door_a = {\n",
" \"name\": \"door a\",\n",
" \"type\": \"door\",\n",
"}\n",
"#door added for bedroom 2 - Ben\n",
"door_b = {\n",
" \"name\": \"door b\",\n",
" \"type\": \"door\",\n",
"}\n",
"\n",
"key_a = {\n",
" \"name\": \"key for door a\",\n",
" \"type\": \"key\",\n",
" \"target\": door_a,\n",
"}\n",
"\n",
"#Items added for bedroom 2 - Ben\n",
"key_c = {\n",
" \"name\": \"key for door c\", \n",
" \"type\": \"key\", \n",
" \"target\": door_c,\n",
"}\n",
"\n",
"#Items added for bedroom 2 -Ben\n",
"key_d = {\n",
" \"name\": \"key for door d\", \n",
" \"type\": \"key\", \n",
" \"target\": door_d,\n",
"}\n",
"\n",
"piano = {\n",
" \"name\": \"piano\",\n",
" \"type\": \"furniture\",\n",
"}\n",
"\n",
"keyboard = {\n",
" \"name\": \"keyboard\",\n",
" \"type\": \"furniture\",\n",
"}\n",
"\n",
"game_room = {\n",
" \"name\": \"game room\",\n",
" \"type\": \"room\",\n",
"}\n",
"bedroom = {\n",
" \"name\": \"bedroom 2\",\n",
" \"type\": \"room\",\n",
"}\n",
"\n",
"\n",
"outside = {\n",
" \"name\": \"outside\"\n",
"}\n",
"\n",
"all_rooms = [game_room, bed_room_1, bed_room_2, living_room, outside]\n",
"\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",
" \"outside\": [door_a],\n",
" \"door a\": [game_room, bed_room_1 outside]\n",
" \"door b\": [bedroom_1, bedroom_2]\n",
" \"door c\": [door_c]\n",
" \"bed_room_2\":[key_c,]\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",
"}\n",
"def linebreak():\n",
" \n",
" print(\"\\n\\n\")\n",
"\n",
"def start_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",
" 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",
" item = (input(\"What would you like to examine?\").strip())\n",
"#here I added the special way to the function ex_piano, that will work the entire new situation with the keyboard and the key\n",
"#to make it work in other rooms it would be basically the same: create a function about the new item and its reactions and linking it here \n",
" if (item == \"piano\"):\n",
" ex_piano(item)\n",
" else: \n",
" examine_item(item)\n",
" else:\n",
" print(\"Not sure what you mean. Type 'explore' or 'examine'.\")\n",
" play_room(room)\n",
" linebreak()\n",
"\n",
"#this next function is new. It makes the keyboard in the piano works and makes you find the key only if you play the piano and you haven't already picked the key\n",
"\n",
"def ex_piano(piano):\n",
" room = game_state[\"current_room\"]\n",
" print(\"The piano looks pretty unused due to the amount of dust.\")\n",
" decission = input(\"Do you want to play it? yes/no \\n\").strip()\n",
" if decission == \"no\":\n",
" print(\"You leave the piano alone\")\n",
" elif decission == \"yes\" and key_a not in game_state[\"keys_collected\"]:\n",
" print(\"Although you cannot play any instrument, you notice there's something wrong\")\n",
" print(\"Stuck under a keyboard key, you find a rusty metal key. \")\n",
" game_state[\"keys_collected\"].append(key_a)\n",
" elif decission == \"yes\" and key_a in game_state[\"keys_collected\"]:\n",
" print(\"You already have the key, there's nothing else\")\n",
" play_room(room)\n",
" \n",
" \n",
"def explore_room(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",
" 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",
" 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)\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"game_state = INIT_GAME_STATE.copy()\n",
"\n",
"start_game()\n"
]
}
],
"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
}
Loading