From f7efd5be56cd98a2897b27d0ecd6422fac5e2852 Mon Sep 17 00:00:00 2001 From: Ahmed Yusuf <yusufa7@cardiff.ac.uk> Date: Tue, 25 Oct 2022 20:35:27 +0100 Subject: [PATCH] changes to lock and closet --- Group_game/game.py | 62 +++++++++++++++---------- Group_game/hangman.py | 105 ++++++++++++++++++++---------------------- Group_game/items.py | 18 ++++++++ Group_game/level_1.py | 15 ++++++ Group_game/level_2.py | 22 ++++++--- Group_game/map.py | 5 +- 6 files changed, 140 insertions(+), 87 deletions(-) diff --git a/Group_game/game.py b/Group_game/game.py index 0d23045..15cfca4 100644 --- a/Group_game/game.py +++ b/Group_game/game.py @@ -2,6 +2,7 @@ from map import * from player import * from gameparser import * +from hangman import * from datetime import datetime from items import * from level_1 import * @@ -56,12 +57,11 @@ def print_room(room): def exit_leads_to(exits, direction): - time.sleep(0.1) return locations[exits[direction]]["name"] def print_exit(direction, leads_to): - time.sleep(0.8) + time.sleep(0.2) print("GO " + direction.upper() + " to " + leads_to + ".") @@ -75,57 +75,61 @@ def print_menu(exits, room_items, inv_items): print_exit(direction, exit_leads_to(exits, direction)) for items in room_items: if items["pick-up"]: - time.sleep(0.8) + time.sleep(0.2) print("TAKE", items["id"], "to take", items["name"] + ".") for items in inv_items: - time.sleep(0.8) + time.sleep(0.2) print("DROP", items["id"], "to drop", items["name"] + ".") for items in inv_items: if "open" in items: if not items["open"]: - time.sleep(0.8) + time.sleep(0.2) print("OPEN", items["id"], "to open", items["name"] + ".") if not location_boileroom["valve_open"]: - time.sleep(0.8) + time.sleep(0.2) print("CHECK TIMER to check the time remaining") if current_room == location_door and location_door["locked"]: if current_level == "level 1": - time.sleep(0.8) + time.sleep(0.2) print("UNLOCK DOOR to attempt to unlock the escape door") else: - time.sleep(0.8) + time.sleep(0.2) print("GO UPSTAIRS to go back to level 1") if item_note in inv_items: - time.sleep(0.8) + time.sleep(0.2) print("READ NOTE to read the hidden note") if item_safe in room_items: if not item_safe["open"]: - time.sleep(0.8) + time.sleep(0.2) print("OPEN SAFE to attempt to open the safe") else: "You've already opened the safe" if item_batteries in inventory and unpowered_torch in inventory: - time.sleep(0.8) + time.sleep(0.2) print("USE BATTERIES for TORCH to place the batteries in the torch") # if item_batteries in inventory and item_lock in inventory: # print("USE BATTERIES for LOCK to place the batteries in the torch") if (powered_torch in inv_items) or (unpowered_torch in inv_items): - time.sleep(0.8) + time.sleep(0.2) print("USE TORCH to use your torch") if current_room == location_boileroom and location_boileroom["valve_open"]: - time.sleep(0.8) + time.sleep(0.2) print("CLOSE valve to close the valve and stop the leakage") if location_boileroom["valve_open"]: - time.sleep(0.8) + time.sleep(0.2) print("CHECK GAS TIMER to see how long left before the room fills with gas") if item_winebottle in inventory: - time.sleep(0.8) + time.sleep(0.2) print('DRINK WINE to have a glass of wine') if powered_torch in inventory: print("REMOVE BATTERIES from TORCH to remove the batteries from the torch") - time.sleep(0.8) - time.sleep(0.8) + time.sleep(0.2) + if current_room==desk: + print("PLAY HANGMAN to play a game of hangman") + time.sleep(0.2) + if current_room==trap_door and item_batteries in inventory: + print("USE BATTERIES for LOCK to place the batteries in the electrical lock") print("What do you want to do?") @@ -135,18 +139,20 @@ def is_valid_exit(exits, chosen_exit): def execute_go(direction): - time.sleep(0.1) global current_room if is_valid_exit(current_room["exits"], direction): new_room = move(current_room["exits"], direction) - current_room = new_room - return current_room + if new_room == closet and not closet["open"]: + print("The closet is locked, complete an activity to open the closet") + time.sleep(0.8) + else: + current_room = new_room + return current_room else: print("you cannot go there") - + time.sleep(0.8) def execute_take(item_id): - time.sleep(0.1) found = False for items in current_room["items"]: if item_id == items["id"]: @@ -156,8 +162,10 @@ def execute_take(item_id): current_room["items"].remove(items) else: print("Your inventory is full, Drop an item to free some space.") + time.sleep(0.8) if not found: print("You cannot take that.") + time.sleep(0.4) def execute_drop(item_id): @@ -297,8 +305,6 @@ def execute_command(command): if len(command) > 2: if command[2] == "torch": remove_battery_torch() - elif command[2] == "lock": - "" # remove_battery_lock() else: print("remove batteries from what?") elif command[1] == "torch": @@ -310,6 +316,11 @@ def execute_command(command): drink_wine() else: print("drink what?") + elif command[0] == "play": + if command[1] == "hangman": + hangman("closet") + else: + print("drink what?") else: print("This makes no sense.") @@ -358,7 +369,8 @@ def main(): if current_room == location_boileroom: if not location_boileroom["entered"]: location_boileroom["valve_open"] = True - print("(gas warning message 2 min)") + print("You immediately start hearing a high pitched hissing sound. You look around and see a valve needs to be closed. If only you had come sort of utensil to shut it.") + print("You've got 2 minutes to close the valve before the room fills with gas") time.sleep(5) gas_starttime = datetime.now() location_boileroom["entered"] = True diff --git a/Group_game/hangman.py b/Group_game/hangman.py index 27033d5..3e58003 100644 --- a/Group_game/hangman.py +++ b/Group_game/hangman.py @@ -1,80 +1,77 @@ -Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32 -Type "help", "copyright", "credits" or "license()" for more information. ->>> -... from collections import Counter -... -... word = "closet" -... -... def hangman(word): -... print('Guess the word! HINT: you may find this thing in your bedroom') -... print("You will have 15 chances to guess the word. ") -... -... for i in word: -... # For printing the empty spaces for letters of the word -... print('_', end = ' ') -... print() -... -... playing = True -... # list for storing the letters guessed by the player -... letterGuessed = '' -... chances = 15 -... correct = 0 -... flag = 0 -... try: -... while (chances != 0) and flag == 0: #flag is updated when the word is correctly guessed -... print() -... print("you have "+str(chances)+" guesses left!") -... chances -= 1 -... -... try: -... guess = str(input('Enter a letter to guess: ')) -... except: -... print('Enter only a letter!') -... continue -... -... # Validation of the guess -... if not guess.isalpha(): -... print('Enter only a LETTER') -... continue +import time +from collections import Counter +from map import * + +def hangman(word): + print('Guess the word! HINT: you may find this thing in your bedroom') + print("You will have 15 chances to guess the word. ") + + for i in word: + # For printing the empty spaces for letters of the word + print('_', end=' ') + print() + + playing = True + # list for storing the letters guessed by the player + letterGuessed = '' + chances = 15 + correct = 0 + flag = 0 + try: + while (chances != 0) and flag == 0: # flag is updated when the word is correctly guessed + print() + print("you have " + str(chances) + " guesses left!") + chances -= 1 + + try: + guess = str(input('Enter a letter to guess: ')) + except: + print('Enter only a letter!') + continue + # Validation of the guess + if not guess.isalpha(): + print('Enter only a LETTER') + continue elif len(guess) > 1: print('Enter only a SINGLE letter') continue elif guess in letterGuessed: print('You have already guessed that letter') continue - - + # If letter is guessed correctly if guess in word: - k = word.count(guess) #k stores the number of times the guessed letter occurs in the word - for _ in range(k): - letterGuessed += guess # The guess letter is added as many times as it occurs - + k = word.count(guess) # k stores the number of times the guessed letter occurs in the word + for _ in range(k): + letterGuessed += guess # The guess letter is added as many times as it occurs + # Print the word for char in word: if char in letterGuessed and (Counter(letterGuessed) != Counter(word)): - print(char, end = ' ') + print(char, end=' ') correct += 1 # If user has guessed all the letters - elif (Counter(letterGuessed) == Counter(word)): # Once the correct word is guessed fully, - # the game ends, even if chances remain + elif (Counter(letterGuessed) == Counter(word)): # Once the correct word is guessed fully, + # the game ends, even if chances remain print("The word is: ", end=' ') print(word) flag = 1 print('Congratulations, You won!') - break # To break out of the for loop - break # To break out of the while loop + print() + time.sleep(1) + print("You hear a strange click sound from the closet in the storage room") + closet["open"] = True + break # To break out of the for loop + break # To break out of the while loop else: - print('_', end = ' ') - - - + print('_', end=' ') + # If user has used all of his chances if chances <= 0 and (Counter(letterGuessed) != Counter(word)): print() print('You lost! Try again..') print('The word was {}'.format(word)) - + except KeyboardInterrupt: print() print('Bye! Try again.') diff --git a/Group_game/items.py b/Group_game/items.py index edb8325..4d2b1be 100644 --- a/Group_game/items.py +++ b/Group_game/items.py @@ -79,4 +79,22 @@ item_winebottle = { "pick-up": True, +} +key_card = { + "id": "keycard", + + "name": "a key card", + + "description": "()", + + "pick-up": True, +} +item_lock = { + "id": "lock", + + "name": "an electric lock", + + "pick-up": False, + + "batteries": False } \ No newline at end of file diff --git a/Group_game/level_1.py b/Group_game/level_1.py index 5e53bf4..953420a 100644 --- a/Group_game/level_1.py +++ b/Group_game/level_1.py @@ -1,3 +1,5 @@ +import time + from items import * from player import * from map import * @@ -6,10 +8,12 @@ from map import * def open_diary(room_items): if item_diary in inventory: print("You opened the diary and found a hidden note.") + time.sleep(0.8) room_items.append(item_note) item_diary.update({"open": True}) else: print("You cannot open that.") + time.sleep(0.8) def open_safe(room_items): @@ -19,17 +23,23 @@ def open_safe(room_items): if passcode == "042": print("THE PASSCODE WAS CORRECT!") print("inside the safe you found a pair of batteries") + time.sleep(0.8) room_items.append(item_batteries) item_safe.update({"open": True}) else: print("you try the code", passcode, "but the safe wouldn't open") print("TRY AGAIN") + time.sleep(0.8) + + def remove_battery_torch(): if powered_torch in inventory: inventory.remove(powered_torch) inventory.append(item_batteries) inventory.append(unpowered_torch) print("You removed the batteries from the torch") + time.sleep(0.5) + def use_battery_torch(): if item_batteries in inventory: @@ -38,6 +48,8 @@ def use_battery_torch(): inventory.remove(unpowered_torch) inventory.append(powered_torch) print("You placed the batteries in the torch.") + time.sleep(0.5) + def use_torch(room): if powered_torch in inventory: @@ -45,9 +57,11 @@ def use_torch(room): room["items"].append(item_key) else: print("There is no use for the torch in this room") + time.sleep(0.8) elif unpowered_torch in inventory: print("You flick the on/off button but the torch wouldn't turn on, " "(try finding some batteries to power the torch)") + time.sleep(0.8) else: print("you cannot do that") @@ -60,5 +74,6 @@ def read_note(): print("206 >> two numbers are correct but wrong placed") print("738 >> nothing is correct") print("870 >> one number is correct but wrong placed") + time.sleep(5) else: print("There is no note to read") diff --git a/Group_game/level_2.py b/Group_game/level_2.py index 12efbaf..ffca428 100644 --- a/Group_game/level_2.py +++ b/Group_game/level_2.py @@ -58,16 +58,26 @@ def close_valve(): print("You need a screwdriver to close the valve") + def open_trapdoor(): - pass + if current_room == trap_door: + if item_lock["batteries"]: + if item_keycard in inventory: + trap_door["open"] = True + else: + print("You need a key card to open the lock") + else: + print("The electrical lock requires batteries to open") -def play_hangman(): - pass -def open_closet(): - pass def use_battery_lock(): - pass + if current_room == trap_door: + if item_batteries in inventory: + item_lock["batteries"] = True + inventory.remove(item_batteries) + print("You placed the batteries in the electrical lock") + time.sleep(1) + def gas_timer(timer): current_time = datetime.now() time_taken = current_time - timer diff --git a/Group_game/map.py b/Group_game/map.py index 0c7efdd..e6731d6 100644 --- a/Group_game/map.py +++ b/Group_game/map.py @@ -112,13 +112,14 @@ trap_door = { "name": "trap door", "description": "The trap door is locked! There is an electrocated lock, you could use your key again but there is no guarantee it would work and there is no power in this lock. ", "exits": {"west": "entrance"}, - "items": [] + "open": False + "items": [item_lock] } closet = { "name": "closet", "description": "You are amazed that the door is now mysteriously open, wondering how it happened you inspect it further. The closet is wooden with dusty shelves and old literary texts. There is a key card here as well.", "exits": {}, - "items": [] + "items": [key_card] } locations = { "fireplace": location_Fireplace, -- GitLab