当我按下“获取”按钮时,它应该从列表框中抓取项目并将该项目放入“您正在携带”列表以及更新房间和列表框。
我正在考虑制作一个 updateWidget 函数,该函数将在获取 grabbable 后清除列表框,并在移动到另一个房间后用新的 grabbable 重新加载列表框。只是我不知道该怎么做。
from tkinter import *
# the room class
# ...
# the game class
# inherits from the Frame class of Tkinter
def createWidgets(self):
# create the "Take" button
self.takeButton = Button(self, text="Take", command=self.takeItem, width=10, height=2, font=("Arial", 16))
self.takeButton.pack(side=LEFT)
# create a frame to hold the label and listbox
itemsFrame = Frame(self)
itemsFrame.pack(side=LEFT, fill=Y)
# create the label and listbox widgets
titleLabel = Label(itemsFrame, text="Grabbable Items", font=("Arial", 18))
titleLabel.pack()
self.itemListbox = Listbox(itemsFrame, font=("Arial", 14))
self.itemListbox.pack(side=LEFT, fill=Y)
# add items to the listbox
for item in self.currentRoom.grabbables:
self.itemListbox.insert(END, item)
# function that will be called when the "Take" button is clicked
def takeItem(self):
# get the currently selected item from the listbox
selected = self.itemListbox.curselection()
if len(selected) == 0:
return
item = self.itemListbox.get(selected[0])
# remove the item from the room and add it to the player's inventory
if item in self.currentRoom.grabbables:
self.currentRoom.delGrabbable(item)
self.inventory.append(item)
# plays the game
def play(self):
# add the rooms to the game
self.createRooms()
# configure the GUI
self.setupGUI()
# set the current room
self.setRoomImage()
# set the current status
self.setStatus("")
# processes the player's input
def process(self, event):
# grab the player's input from the input at the bottom of the GUI
action = Game.player_input.get()
# set the user's input to lowercase to make it easier to compare the verb and noun to known values
action = action.lower()
# set a default response
response = "I don't understand. Try verb noun. Valid verbs are go, look, and take"
# exit the game if the player wants to leave (supports quit, exit, and bye)
if (action == "quit" or action == "exit" or action == "bye" or action == "sionara!"):
exit(0)
# if the player is dead if goes/went south from room 4
if (Game.currentRoom == None):
# clear the player's input
Game.player_input.delete(0, END)
return
# split the user input into words (words are separated by spaces) and store the words in a list
words = action.split()
# the game only understands two word inputs
if (len(words) == 2):
# isolate the verb and noun
verb = words[0]
noun = words[1]
# the verb is: go
if (verb == "go"):
# set a default response
response = "Invalid exit."
# check for valid exits in the current room
if (noun in Game.currentRoom.exits):
# if one is found, change the current room to the one that is associated with the specified exit
Game.currentRoom = Game.currentRoom.exits[noun]
# set the response (success)
response = "Room changed."
# the verb is: look
elif (verb == "look"):
# set a default response
response = "I 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 = "I 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 = "Item 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)
此代码将直接在流程函数中处理“take”动作,并在状态区显示可用的物品和玩家的库存。玩家可以输入“Take [item]”来获取物品,游戏会相应地更新房间和显示的物品。
from tkinter import *
# the room class
# ...
# the game class
# inherits from the Frame class of Tkinter
# Remove createWidgets function
# Remove takeItem function
# plays the game
def play(self):
# add the rooms to the game
self.createRooms()
# configure the GUI
self.setupGUI()
# set the current room
self.setRoomImage()
# set the current status
self.setStatus("")
# processes the player's input
def process(self, event):
# the verb is: take
elif (verb == "take"):
# set a default response
response = "I 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 = "Item grabbed: {}.\nYou are carrying: {}".format(grabbable, ', '.join(Game.inventory))
# no need to check any more grabbable items
break
else:
response = "Available items in the room: {}".format(', '.join(Game.currentRoom.grabbables))
#rest of the function
我会这样做:
from tkinter import *
def createWidgets(self):
# create the label and listbox widgets
# call the updateWidget function to populate the ListBox initially
self.updateWidget()
def updateWidget(self):
# clear the ListBox
self.itemListbox.delete(0, END)
# add items to the ListBox
for item in self.currentRoom.grabbables:
self.itemListbox.insert(END, item)
def takeItem(self):
# remove the item from the room and add it to the player's inventory
if item in self.currentRoom.grabbables:
self.currentRoom.delGrabbable(item)
self.inventory.append(item)
# call the updateWidget function to update the ListBox after taking an item
self.updateWidget()
# plays the game
def play(self):
# set the current room
self.setRoomImage()
# set the current status
self.setStatus("")
# processes the player's input
def process(self, event):
# the verb is: go
if (verb == "go"):
# if one is found, change the current room to the one that is associated with the specified exit
Game.currentRoom = Game.currentRoom.exits[noun]
# set the response (success)
response = "Room changed."
# call the updateWidget function to update the ListBox when changing the room
self.updateWidget()