如何使一个游戏库存存储物品的顺序,他们收到?

2024-10-03 00:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用一个工作GUI制作一个基于文本的冒险游戏。GUI已经完成,故事即将完成,所以我只是做了一些小调整来尝试改进它

enter image description here

现在,我有一个库存[在右边],在那里,新的物品会在提货时添加。它由禁用的输入框组成,当项目即将添加到输入框/从输入框中删除时,将启用这些输入框,然后再次禁用

如果要拿起绳子,代码如下所示:

rope = 0

#The next part isn't actually in a separate `def` in my full code, it is a part of the same `def` which also loads the text
def addRope():
    global rope
    rope = 1
    Item2TextBox.configure(state='normal')
    Item2TextBox.delete(0, END)
    Item2TextBox.insert(0, 'Rope')
    Item2TextBox.configure(state='disabled')

rope = 0是检查用户在可以使用该项时是否携带该项。与中一样,我使用if/elif/else检查该项是否设置为0或1

现在,我这样做是为了让用户能够处理的每一个项目都不会像其他项目一样插入到同一个文本框中。更准确地说,我通读了这个故事,并检查了当用户获得一个新项目时,是否有一个项目在该库存点。如果答案是肯定的“否”,我会使用该插槽,如果答案是“是”,或者我可能会检查下一个插槽,依此类推。我是手工做的,不是用代码

这个系统主要起作用,但在故事的某些部分,用户可以根据自己的选择来保存或丢失物品。所以我的问题的答案是文本框中有一个项目可能意味着我使用下一个框。但这会导致出现这样的情况:有一个空框,但项目被输入到它下面的框中。下面有一个例子

enter image description here

我需要什么: 我需要一个系统来检查库存槽是否为空,将文本插入其中,然后他们记住项目插入的框中,因此当用户松开或丢弃项目时,它将从他们的库存中删除。有人知道这是怎么回事吗

我在下面添加了一段修改过的代码,以复制我目前所做的工作:

from tkinter import *

#This is the GUI
GUI = Tk()
GUI.geometry('500x300')
GUI.title("Login Screen")

#These are the entry boxes. They all disabled.
ItemsLabel = Label(GUI, font=('arial', 14, 'bold'), text='Inventory:', bg='black', fg='white', bd=5, justify=LEFT)
ItemsLabel.grid(row=4, column=0)
Item1TextBox = Entry(GUI, font=('arial', 14, 'bold'), bg='#7501A5', fg='Black', bd=5, width=20, borderwidth=4,
                     justify=CENTER)
Item1TextBox.grid(row=5, column=0, pady=10)
Item1TextBox.configure(state="disabled")
Item2TextBox = Entry(GUI, font=('arial', 14, 'bold'), bg='#7501A5', fg='Black', bd=5, width=20, borderwidth=4,
                     justify=CENTER)
Item2TextBox.grid(row=6, column=0, pady=10)
Item2TextBox.configure(state="disabled")
Item3TextBox = Entry(GUI, font=('arial', 14, 'bold'), bg='#7501A5', fg='Black', bd=5, width=20, borderwidth=4,
                     justify=CENTER)
Item3TextBox.grid(row=7, column=0, pady=10)
Item3TextBox.configure(state="disabled")

#These are the items the user can get.
item1 = 0
item2 = 0
item3 = 0

#This adds Item 1 and 2 into the top two entry boxes.
def addItem1Item2():
    global item1, item2
    item2 = 1
    item1 = 1
    Item1TextBox.configure(state='normal')
    Item1TextBox.delete(0, END)
    Item1TextBox.insert(0, 'Item 1')
    Item1TextBox.configure(state='disabled')
    Item2TextBox.configure(state='normal')
    Item2TextBox.delete(0, END)
    Item2TextBox.insert(0, 'Item 2')
    Item2TextBox.configure(state='disabled')

#This removes Item 1 and 2 from the top two boxes.
def removeItem1Item2():
    global item1, item2
    item2 = 0
    item1 = 0
    Item1TextBox.configure(state='normal')
    Item1TextBox.delete(0, END)
    Item1TextBox.configure(state='disabled')
    Item2TextBox.configure(state='normal')
    Item2TextBox.delete(0, END)
    Item2TextBox.configure(state='disabled')

#This adds Item 3 into the bottom entry box.
def addItem3():
    global item3
    item3 = 1
    Item3TextBox.configure(state='normal')
    Item3TextBox.delete(0, END)
    Item3TextBox.insert(0, 'Item 3')
    Item3TextBox.configure(state='disabled')

#This removes Item 3 from the bottom box.
def removeItem3():
    global item3
    item3 = 0
    Item3TextBox.configure(state='normal')
    Item3TextBox.delete(0, END)
    Item3TextBox.configure(state='disabled')

#These are the buttons.
button1 = Button(GUI, command=addItem1Item2, text='Add Item 1 and Item 2')
button1.grid()

button2 = Button(GUI, command=addItem3, text='Add Item 3')
button2.grid()

button3 = Button(GUI, command=removeItem1Item2, text='Remove Item 1 a Item 2')
button3.grid()

button4 = Button(GUI, command=removeItem3, text='Remove Item 3')
button4.grid()

GUI.mainloop()

有人能想出一个解决方案,使项目总是插入第一个空槽,然后从正确的框中删除吗


Tags: the项目configuredefguiitemdeletegrid