Question
URGENT! I cant seem to get my key to work properly. the key is a grabbable object but i want the west exit in room
URGENT! I cant seem to get my key to work properly. the key is a grabbable object but i want the west exit in room one to not be accessible unless you have the key which is grabbed off desk. here is code i AM ABLE TO GO THROUGH EVERY EXIT RIGHT NOW I WANT THE WEST EXIT IN ROOM 1 TO BE LOCKED UNLESS I HAVE GRABBED THE KEY. THE WEST EXIT IS SET AS FALSE BUT LATER ON IN CODE IT STATES "Game.currentRoom is False: Game.text.insert(END, "Fresh air finally. The only thing you can do now is quit. ")" MEANING YOUVE MADE IT OUTSIDE. but i want that door only to be locked until key is obtained
"from tkinter import *
class Room: def __init__(self, name, image, unlocked = True): self.name = name self.unlocked = unlocked self.image = image self.exits = {} self.items = {} self.grabbables = [] self.required_items = {}
@property def name(self): return self._name
@name.setter def name(self, value): self._name = value
@property def unlocked(self): return self._unlocked
@unlocked.setter def unlocked(self, value): if(type(value) == bool): self._unlocked = value
@property def image(self): return self._image
@image.setter def image(self, value): self._image = value
@property def exits(self): return self._exits
@exits.setter def exits(self, value): self._exits = value
@property def items(self): return self._items
@items.setter def items(self, value): self._items = value
@property def grabbables(self): return self._grabbables
@grabbables.setter def grabbables(self, value): self._grabbables = value
@property def required_items(self): return self._required_items
@required_items.setter def required_items(self, value): self._required_items = value
# additional methods
def addExit(self, exit, room, required_item = None): self._exits[exit] = room self._required_items[exit] = required_item
def canExit(self, exit, inventory): if self._required_items[exit] is None: return True if self._required_items[exit] in inventory: return True return False
def addItem(self, item, desc): self._items[item] = desc
def addGrabbable(self, item): self._grabbables.append(item)
def delGrabbable(self, item): self._grabbables.remove(item)
def __str__(self): s = f"You are in {self.name}"
s += " You see: " for item in self.items.keys(): s += item + " "
s += " Exits: " for exit in self.exits.keys(): s += exit + " "
return s
class Game(Frame):
# the constructor def __init__(self, parent): Frame.__init__(self, parent) # creates the rooms def createRooms(self): r1 = Room("Room 1", "room1.gif", True) r2 = Room("Room 2", "room2.gif", True) r3 = Room("Room 3", "room3.gif", True) r4 = Room("Room 4", "room4.gif", True) r5 = Room("Walk-in Closet", "closet.gif", True) r1.addExit("west", False, "key") r1.addExit("east", r2) r1.addExit("south", r3) r1.addGrabbable("key") r1.addGrabbable("eviction-notice") r1.addItem("table", "Why in the world is this table upside down.") r1.addItem("chair", "It is warm as if someone sat in it all night trying to code.") r1.addItem("desk", "There appears to be a pool of drool with a key in it.") r1.addItem("front-door", "Why is there an eviction-notice on the inside of the door?")
r2.addExit("west", r1) r2.addExit("south", r4) r2.addItem("Rug", "Its a shag rug...nice.") r2.addItem("clock", "Its 1:45, I hope no one here has class at 2.") r2.addItem("fireplace", "Clearly has never been used.")
r3.addExit("north", r1) r3.addExit("east", r4) r3.addExit("west", r5) r3.addGrabbable("computer") r3.addGrabbable("book") r3.addItem("statue", "So this is where the missing bulldog statue went.") r3.addItem("bookshelves", "Hope someone uses this book one day.") r3.addItem("computer", "I see someone is logged into RuneScape, it seems oldschool too. The best.") r3.addItem("desk", "There is computer on this desk.")
r4.addExit("north", r2) r4.addExit("west", r3) r4.addExit("south", None) r4.addGrabbable("dumbbell") r4.addGrabbable("6-pack") r4.addItem("weight-rack", "Seems a bit dusty. Maybe I could throw this dumbbell at the window.") r4.addItem("brew-rig", "Looks like a freshly brewed 6-pack is right here.")
r5.addExit("east", r3) r5.addGrabbable("shirts") r5.addGrabbable("pants") r5.addGrabbable("shoes") r5.addItem("hangers", "Man there are a bunch of shirts and pants on these hangers.") r5.addItem("shoe-Rack", "These are some nice shoes") Game.currentRoom = r1 Game.inventory = [] # sets up the GUI def setupGUI(self): self.pack(fill=BOTH, expand=1)
Game.player_input = Entry(self, bg="white") Game.player_input.bind("
img = None Game.image = Label(self, width=WIDTH // 2, image=img) Game.image.image = img Game.image.pack(side=LEFT, fill=Y) Game.image.pack_propagate(False)
text_frame = Frame(self, width=WIDTH // 2) Game.text = Text(text_frame, bg="lightgrey", state=DISABLED) Game.text.pack(fill=Y, expand=1) text_frame.pack(side=RIGHT, fill=Y) text_frame.pack_propagate(False) # set the current room image def setRoomImage(self): if Game.currentRoom is None: Game.img = PhotoImage(file="skull.gif")
elif Game.currentRoom is False: Game.img = PhotoImage(file="outside.gif") else: Game.img = PhotoImage(file=Game.currentRoom.image)
Game.image.config(image=Game.img) Game.image.image = Game.img # sets the status displayed on the right of the GUI def setStatus(self, status): Game.text.config(state=NORMAL) Game.text.delete("1.0", END)
if Game.currentRoom is None: Game.text.insert(END, "You are dead. The only thing you can do now is quit. ")
elif Game.currentRoom is False: Game.text.insert(END, "Fresh air finally. The only thing you can do now is quit. ") else: message = f"{Game.currentRoom} " \ f"You are carrying: {Game.inventory} " \ f"{status}" Game.text.insert(END, message)
Game.text.config(state=DISABLED) # play the game def play(self): self.createRooms() self.setupGUI() self.setRoomImage() self.setStatus("")
def process(self, event): response = "I don't understand. Try verb noun. Valid verbs are go, look, and take."
action = Game.player_input.get() if action in ["quit", "exit", "bye", "sionara!"]: exit(0)
if Game.currentRoom == None: Game.player_input.delete(0, END) return
words = action.split() if len(words) == 2: verb = words[0] noun = words[1]
if verb == "go": response = "Invalid exit" if noun in Game.currentRoom.exits: Game.currentRoom = Game.currentRoom.exits[noun]
elif verb == "look": response = "I don't see that item" if noun in Game.currentRoom.items: response = Game.currentRoom.items[noun]
elif verb == "take": response == "I don't see that item" for grabbable in Game.currentRoom.grabbables: if noun == grabbable: Game.inventory.append(grabbable) Game.currentRoom.delGrabbable(grabbable) response = "Item Grabbed" break self.setStatus(response) self.setRoomImage() Game.player_input.delete(0, END) ########################################################## # the default size of the GUI is 800x600 WIDTH = 800 HEIGHT = 600 # create the window window = Tk() window.title("Room Adventure") # create the GUI as a Tkinter canvas inside the window g = Game(window) # play the game g.play() # wait for the window to close window.mainloop()"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started