Question

convert to python 3 from python 2 from Tkinter import * # the blueprint for a...

convert to python 3 from python 2

from Tkinter import *

# the blueprint for a room
class Room(object):
# the constructor
def __init__(self,name,image):
# rooms have a name, exits (e.g., south), exit locations (e.g., to the south is room n),
# items (e.g., table), item descriptions (for each item), and grabbables (things that can
# be taken and put into the inventory)
self.name = name
self.image = image
self.exits = {}
self.items = {}
self.grabbables = []

# getters and setters for the instance variables
@property
def name(self):
return self._name

@name.setter
def name(self, value):
self._name = 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

# adds an exit to the room
# the exit is a string (e.g., north)
# the room is an instance of a room
def addExit(self, exit, room):
# append the exit and room to the appropriate lists
self.exits[exit] = room

# adds an item to the room
# the item is a string (e.g., table)
# the description is a string that describes the item (e.g., it is made of wood)
def addItem(self,item, desc):
# append the tiem and description to the appropriate lists
self._items[item] = desc

# removes a grabbable item from the room
# the item is a string (e.g., key)
def addGrabbable(self, item):
# append the item to the list
self._grabbables.append(item)

# removes a grabbable item from the room
# the item is a string (e.g., key)
def delGrabbable(self, item):
# remove the item from the list
self._grabbables.remove(item)

# returns a string description of the room
def __str__(self):
# first, the room name
s = "You are in {}.\n".format(self.name)

# next, the items in the room
s += "You see: "
for item in self.items.keys():
s += item + " "

s += "\n"

# next, the grabbables in the room
## Added Print Statement ##
s += "You can take: "
for grabbables in self.grabbables:
s += grabbables + " "
s += "\n"

# next, the exits for the room
s += "Exits: "
for exit in self.exits.keys():
s += exit + " "
s += "\n"
return s

#####################################
class Game(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)

def createRooms(self):
# r1 through r9 are the nine rooms in the mansion
# currentRoom is the room the player is currently in
# (which # can be one of r1 through r4)
# create the rooms and give them meaningful names and an
# image in the current directory
  
r1 = Room("the Red Room", "red.gif")
r2 = Room("the Orange Room", "orange.gif")
r3 = Room("the Yellow Room", "yellow.gif")
r4 = Room("the Green Room", "green.gif")
r5 = Room("the Blue Room", "blue.gif")
r6 = Room("the Purple Room", "purple.gif")
r7 = Room("the Brown Room", "brown.gif")
r8 = Room("the Pink Room", "pink.gif")
r9 = Room("the White Room", "white.gif")

######## Red Room ################
  
r1.addExit("east", r2)
r1.addExit("south", r3)

r1.addItem(" red_chair", "It is made of red suede and no one is sitting on it.")
r1.addItem("table", "It is made of oak. A golden key rests on it.")
r1.addItem("chandelier", "There are two chandeliers, both of which are lit.")
  
r1.addGrabbable("key")

######## Orange Room ################
  
r2.addExit("west", r1)
r2.addExit("south", r4)
r2.addExit("east", r5)
  
r2.addItem("rug", "It is nice and fluffy. It also needs to be vacuumed.")
r2.addItem("lamp", "It is off; maybe you should turn it on.")
r2.addItem("couch", "There are butt grooves in the cushions.")

r2.addGrabbable("pillow")
r2.addGrabbable("bowl")
  
####### Yellow Room ##################
  
r3.addExit("north", r1)
r3.addExit("east", r4)
r3.addExit("west", r6)
  
r3.addItem("picture", "It is a simple picture in a white picture frame.")
r3.addItem("chair", "There is no back to this chair... It is an unusual chair. There\
is a jacket laying on the chair.")

r3.addGrabbable("jacket")
  
############# Green Room #############
  
r4.addExit("north", r2)
r4.addExit("west", r3)
r4.addExit("south", None)
r4.addExit("east", r9)
  
r4.addItem("couch", "It has a couple pillows on it.. It's also black... Nothing special.")
r4.addItem("light", "I don't even know if it has a light bulb in it.")
r4.addItem("plant", "It is green and leafy.")
r4.addItem("table", "There are two tables. One has drinks on it, and the other has\
a bowl and two books.")
  
r4.addGrabbable("pillow")
r4.addGrabbable("watering_can")
r4.addGrabbable("bowl")


########## Blue Room ####################
  
r5.addExit("west", r2)
r5.addExit("east", r7)
r5.addExit("south", r9)

r5.addItem("chair", "It is blue and looks very comfy.")
r5.addItem("painting", "It has an interesting face on it.")
r5.addItem("nightstand", "It is a light beige color. It has some magazines on it.")

r5.addGrabbable("magazine")
  
############ Purple Room ##################

r6.addExit("east", r3)
r6.addExit("south", r8)

r6.addItem("picture", "There is a pretty fox drawn inside of a white picture frame.")
r6.addItem("dresser", "It is a white dresser with 6 drawers. A couple of \
different items are on top of the dresser.")
r6.addItem("window", "It looks bright outside.")

r6.addGrabbable("alarm_clock")
r6.addGrabbable("silicon_mushroom")

############## Brown Room #################

r7.addExit("west", r5)

r7.addItem("stairs", "These stairs look very elegant.")
r7.addItem("carpet", "This carpet feels so good on bare feet.")
r7.addItem("statue", "This horse staue is made out of bronze.")
r7.addItem("fireplcae", "There is a nice plant placement on the shelf\
above the fireplace. But... it is cold.. someone should put some firewood inside\
and light a fire.")
r7.addItem("window", "Look out the window, there might be a fluffy kitty in the yard.")

r7.addGrabbable("blanket")


###### Pink Room ####################
  
r8.addExit("north", r6)

r8.addItem("chair", "There are two pink charis. They look super comfy.")
r8.addItem("table", "There is a pink table. It has a couple of items on it.")
r8.addItem("decorations", "What are these interesting pink decorations on the ceiling??")

r8.addGrabbable("plate")
r8.addGrabbable("fluff")


############# White Room ##################

r9.addExit("north", r5)
r9.addExit("west", r4)

r9.addItem("couch","THere are two couches facing eachother. They are grey with\
white pand grey pillows on them.")
r9.addItem("lamp", "It is a normal, modern lamp that is white.")
r9.addItem("vase", "There are white tulips inside of this skinny vase.")

r9.addGrabbable("tulip")   

###################################
  
# set room 1 as the current room at the beginning of the game
Game.currentRoom = r1
# initialize the player's inventory
Game.inventory = []

def setUpGui(self):
self.pack(fill = BOTH, expand = 1)

Game.player_input = Entry(self, bg="white")
Game.player_input.bind("<Return>", self.process)
Game.player_input.pack(side=BOTTOM, fill=X)
Game.player_input.focus()

img = None

Game.image = Label(self,width=2*WIDTH/3, image = img)
Game.image.image = img
Game.image.pack(side=LEFT, fill=Y)
Game.image.pack_propagate(False)

text_frame = Frame(self,width=WIDTH/3)
Game.text = Text(text_frame, bg="white", state=DISABLED)
Game.text.pack(fill=Y, expand=1)
text_frame.pack(side=RIGHT, fill=Y)
text_frame.pack_propagate(False)
  

def setRoomImage(self):
if (Game.currentRoom == None):
Game.img = PhotoImage(file="skull.gif")
else:
Game.img = PhotoImage(file=Game.currentRoom.image)
Game.image.config(image=Game.img)
Game.image.image = Game.img

def setStatus(self, status):
Game.text.config(state = NORMAL)
Game.text.delete("1.0", END)

if (Game.currentRoom == None):
Game.text.insert(END, "You are dead. Quit now")
else:
Game.text.insert(END, str(Game.currentRoom) + \
"You are carrying: " + str(Game.inventory)\
+ status + "\n")
Game.text.config(state = DISABLED)
  

def play(self):
self.createRooms()
self.setUpGui()
self.setRoomImage()
self.setStatus("")

def process(self, event):
action = Game.player_input.get()
action = action.lower()

response = "I don't understand. Try verb noun. Valid verbs are go, look, and take"

if (action == "quit" or action == "exit" or action == "bye" or action == "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 = "\nInvalid exit."

if (noun in Game.currentRoom.exits):
Game.currentRoom = Game.currentRoom.exits[noun]
response = "\nRoom changed."
elif (verb == "look"):
# set a default response
response = "\nI don't see that item."
# check for valid items in the current room
if (noun in Game.currentRoom.items):
# if one is found, set the response to the
# item's description
response = Game.currentRoom.items[noun]
# the verb is: take
elif (verb == "take"):
# set a default response
response = "\nI don't see that item."
# check for valid grabbable items in the current room
for grabbable in Game.currentRoom.grabbables:
# a valid grabbable item is found
if (noun == grabbable):
# add the grabbable item to the player's
# inventory
Game.inventory.append(grabbable)
# remove the grabbable item from the
# room
Game.currentRoom.delGrabbable(grabbable)
# set the response (success)
response = "\nItem grabbed."
# no need to check any more grabbable
# items break
# display the response on the right of the GUI
# display the room's image on the left of the GUI
# clear the player's input
self.setStatus(response)
self.setRoomImage()
Game.player_input.delete(0, END)
  

################MAIN################3
WIDTH = 800
HEIGHT = 600

window = Tk()
window.title("Room Adventure....Reloaded")

g = Game(window)
g.play()

window.mainloop()

Homework Answers

Answer #1

********************************************************************************

NOTE-

Only format of the code was changed i.e no logical or syntax errors were removed (if any).

The changes were only made in the lines 87 and 101.

Respective identations were added.

********************************************************************************

from tkinter import *

# the blueprint for a room
class Room(object):
# the constructor
def __init__(self,name,image):
# rooms have a name, exits (e.g., south), exit locations (e.g., to the south is room n),
# items (e.g., table), item descriptions (for each item), and grabbables (things that can
# be taken and put into the inventory)
self.name = name
self.image = image
self.exits = {}
self.items = {}
self.grabbables = []

# getters and setters for the instance variables
@property
def name(self):
return self._name

@name.setter
def name(self, value):
self._name = 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

# adds an exit to the room
# the exit is a string (e.g., north)
# the room is an instance of a room
def addExit(self, exit, room):
# append the exit and room to the appropriate lists
self.exits[exit] = room

# adds an item to the room
# the item is a string (e.g., table)
# the description is a string that describes the item (e.g., it is made of wood)
def addItem(self,item, desc):
# append the tiem and description to the appropriate lists
self._items[item] = desc

# removes a grabbable item from the room
# the item is a string (e.g., key)
def addGrabbable(self, item):
# append the item to the list
self._grabbables.append(item)

# removes a grabbable item from the room
# the item is a string (e.g., key)
def delGrabbable(self, item):
# remove the item from the list
self._grabbables.remove(item)

# returns a string description of the room
def __str__(self):
# first, the room name
s = "You are in {}.\n".format(self.name)

# next, the items in the room
s += "You see: "
for item in list(self.items.keys()):
s += item + " "

s += "\n"

# next, the grabbables in the room
## Added Print Statement ##
s += "You can take: "
for grabbables in self.grabbables:
s += grabbables + " "
s += "\n"

# next, the exits for the room
s += "Exits: "
for exit in list(self.exits.keys()):
s += exit + " "
s += "\n"
return s

#####################################
class Game(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)

def createRooms(self):
# r1 through r9 are the nine rooms in the mansion
# currentRoom is the room the player is currently in
# (which # can be one of r1 through r4)
# create the rooms and give them meaningful names and an
# image in the current directory
  
r1 = Room("the Red Room", "red.gif")
r2 = Room("the Orange Room", "orange.gif")
r3 = Room("the Yellow Room", "yellow.gif")
r4 = Room("the Green Room", "green.gif")
r5 = Room("the Blue Room", "blue.gif")
r6 = Room("the Purple Room", "purple.gif")
r7 = Room("the Brown Room", "brown.gif")
r8 = Room("the Pink Room", "pink.gif")
r9 = Room("the White Room", "white.gif")

######## Red Room ################
  
r1.addExit("east", r2)
r1.addExit("south", r3)

r1.addItem(" red_chair", "It is made of red suede and no one is sitting on it.")
r1.addItem("table", "It is made of oak. A golden key rests on it.")
r1.addItem("chandelier", "There are two chandeliers, both of which are lit.")
  
r1.addGrabbable("key")

######## Orange Room ################
  
r2.addExit("west", r1)
r2.addExit("south", r4)
r2.addExit("east", r5)
  
r2.addItem("rug", "It is nice and fluffy. It also needs to be vacuumed.")
r2.addItem("lamp", "It is off; maybe you should turn it on.")
r2.addItem("couch", "There are butt grooves in the cushions.")

r2.addGrabbable("pillow")
r2.addGrabbable("bowl")
  
####### Yellow Room ##################
  
r3.addExit("north", r1)
r3.addExit("east", r4)
r3.addExit("west", r6)
  
r3.addItem("picture", "It is a simple picture in a white picture frame.")
r3.addItem("chair", "There is no back to this chair... It is an unusual chair. There\
is a jacket laying on the chair.")

r3.addGrabbable("jacket")
  
############# Green Room #############
  
r4.addExit("north", r2)
r4.addExit("west", r3)
r4.addExit("south", None)
r4.addExit("east", r9)
  
r4.addItem("couch", "It has a couple pillows on it.. It's also black... Nothing special.")
r4.addItem("light", "I don't even know if it has a light bulb in it.")
r4.addItem("plant", "It is green and leafy.")
r4.addItem("table", "There are two tables. One has drinks on it, and the other has\
a bowl and two books.")
  
r4.addGrabbable("pillow")
r4.addGrabbable("watering_can")
r4.addGrabbable("bowl")


########## Blue Room ####################
  
r5.addExit("west", r2)
r5.addExit("east", r7)
r5.addExit("south", r9)
  
r5.addItem("chair", "It is blue and looks very comfy.")
r5.addItem("painting", "It has an interesting face on it.")
r5.addItem("nightstand", "It is a light beige color. It has some magazines on it.")

r5.addGrabbable("magazine")
  
############ Purple Room ##################

r6.addExit("east", r3)
r6.addExit("south", r8)

r6.addItem("picture", "There is a pretty fox drawn inside of a white picture frame.")
r6.addItem("dresser", "It is a white dresser with 6 drawers. A couple of \
different items are on top of the dresser.")
r6.addItem("window", "It looks bright outside.")

r6.addGrabbable("alarm_clock")
r6.addGrabbable("silicon_mushroom")

############## Brown Room #################

r7.addExit("west", r5)

r7.addItem("stairs", "These stairs look very elegant.")
r7.addItem("carpet", "This carpet feels so good on bare feet.")
r7.addItem("statue", "This horse staue is made out of bronze.")
r7.addItem("fireplcae", "There is a nice plant placement on the shelf\
above the fireplace. But... it is cold.. someone should put some firewood inside\
and light a fire.")
r7.addItem("window", "Look out the window, there might be a fluffy kitty in the yard.")

r7.addGrabbable("blanket")


###### Pink Room ####################
  
r8.addExit("north", r6)

r8.addItem("chair", "There are two pink charis. They look super comfy.")
r8.addItem("table", "There is a pink table. It has a couple of items on it.")
r8.addItem("decorations", "What are these interesting pink decorations on the ceiling??")

r8.addGrabbable("plate")
r8.addGrabbable("fluff")


############# White Room ##################

r9.addExit("north", r5)
r9.addExit("west", r4)

r9.addItem("couch","THere are two couches facing eachother. They are grey with\
white pand grey pillows on them.")
r9.addItem("lamp", "It is a normal, modern lamp that is white.")
r9.addItem("vase", "There are white tulips inside of this skinny vase.")

r9.addGrabbable("tulip")   

###################################
  
# set room 1 as the current room at the beginning of the game
Game.currentRoom = r1
# initialize the player's inventory
Game.inventory = []

def setUpGui(self):
self.pack(fill = BOTH, expand = 1)

Game.player_input = Entry(self, bg="white")
Game.player_input.bind("<Return>", self.process)
Game.player_input.pack(side=BOTTOM, fill=X)
Game.player_input.focus()

img = None

Game.image = Label(self,width=2*WIDTH/3, image = img)
Game.image.image = img
Game.image.pack(side=LEFT, fill=Y)
Game.image.pack_propagate(False)

text_frame = Frame(self,width=WIDTH/3)
Game.text = Text(text_frame, bg="white", state=DISABLED)
Game.text.pack(fill=Y, expand=1)
text_frame.pack(side=RIGHT, fill=Y)
text_frame.pack_propagate(False)
  

def setRoomImage(self):
if (Game.currentRoom == None):
Game.img = PhotoImage(file="skull.gif")
else:
Game.img = PhotoImage(file=Game.currentRoom.image)
Game.image.config(image=Game.img)
Game.image.image = Game.img

def setStatus(self, status):
Game.text.config(state = NORMAL)
Game.text.delete("1.0", END)

if (Game.currentRoom == None):
Game.text.insert(END, "You are dead. Quit now")
else:
Game.text.insert(END, str(Game.currentRoom) + \
"You are carrying: " + str(Game.inventory)\
+ status + "\n")
Game.text.config(state = DISABLED)
  

def play(self):
self.createRooms()
self.setUpGui()
self.setRoomImage()
self.setStatus("")

def process(self, event):
action = Game.player_input.get()
action = action.lower()

response = "I don't understand. Try verb noun. Valid verbs are go, look, and take"

if (action == "quit" or action == "exit" or action == "bye" or action == "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 = "\nInvalid exit."

if (noun in Game.currentRoom.exits):
Game.currentRoom = Game.currentRoom.exits[noun]
response = "\nRoom changed."
elif (verb == "look"):
# set a default response
response = "\nI don't see that item."
# check for valid items in the current room
if (noun in Game.currentRoom.items):
# if one is found, set the response to the
# item's description
response = Game.currentRoom.items[noun]
# the verb is: take
elif (verb == "take"):
# set a default response
response = "\nI don't see that item."
# check for valid grabbable items in the current room
for grabbable in Game.currentRoom.grabbables:
# a valid grabbable item is found
if (noun == grabbable):
# add the grabbable item to the player's
# inventory
Game.inventory.append(grabbable)
# remove the grabbable item from the
# room
Game.currentRoom.delGrabbable(grabbable)
# set the response (success)
response = "\nItem grabbed."
# no need to check any more grabbable
# items break
# display the response on the right of the GUI
# display the room's image on the left of the GUI
# clear the player's input
self.setStatus(response)
self.setRoomImage()
Game.player_input.delete(0, END)
  

################MAIN################3
WIDTH = 800
HEIGHT = 600

window = Tk()
window.title("Room Adventure....Reloaded")

g = Game(window)
g.play()

window.mainloop()

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions