[英]How would I fix my inventory if/else loop system? [Python GUI Based Adventure Game]
I am trying to rewrite an old GUI-based adventure game of mine and am running into trouble. 我试图重写我的一个基于GUI的旧冒险游戏,并且遇到了麻烦。 I am nothing but a humble beginner, so I can't properly diagnose what is going on here. 我不过是一个谦虚的初学者,所以我无法正确诊断这里发生了什么。 In my code I use and if statement to see if they have the item skull
in their inventory ( inv
). 在我的代码中,我使用if语句来查看他们的库存( inv
)中是否有skull
。 Even if inv==skull
, the program continues down to the last else
as if they don't have the skull in their inventory, instead of going to line 64 and seeing that inv==('skull')
. 即使inv==skull
,程序继续到最后else
因为如果他们没有在他们的库存头骨,而不是去排队64而当看到inv==('skull')
Any and all help would be greatly appreciated, including those of how to optimize by code, not just fix it :) 任何帮助都将不胜感激,包括那些如何通过代码进行优化的帮助,而不仅仅是修复它:)
Edit: I have began trying to use a list (Tuple) and then checking if the item is in the list, and it still isn't working :( 编辑:我已经开始尝试使用列表(元组),然后检查该项目是否在列表中,但仍然不起作用:(
Edited Code: 编辑代码:
inv=['skull']
def r_room():
def witch_fight():
def fgt_slap():
rand = random.randint(1, 13)
if rand <= 12:
slap_die()
else:
slap_win()
def fgt_sh():
rand = random.randint(1, 13)
if rand <= 9:
sh_die()
else:
sh_win()
def fgt_gc():
rand = random.randint(1, 13)
if rand <= 7:
gc_die()
else:
gc_win()
fgt_play()
global inv
if inv in ['skull', 'crest']:
img_loc = ('resources\\images\\witch.gif')
bg_img = tk.PhotoImage(file = img_loc)
bg_lbl = tk.Label(image = bg_img)
bg_lbl.image = bg_img
bg_lbl.place(x=0, y=0)
lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
lbl1.place(x=400, y=6)
btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), btn3.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
btn1.place(x=330, y=654)
btn2 = tk.Button(text='Use Skeleton Head?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), btn3.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_sh()])
btn2.place(x=640, y=654)
btn3 = tk.Button(text='Use Golden Crest?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), btn3.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_gc()])
btn3.place(x=950, y=654)
else:
if inv in ['skull']:
img_loc = ('resources\\images\\witch.gif')
bg_img = tk.PhotoImage(file = img_loc)
bg_lbl = tk.Label(image = bg_img)
bg_lbl.image = bg_img
bg_lbl.place(x=0, y=0)
lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
lbl1.place(x=400, y=6)
btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
btn1.place(x=330, y=654)
btn2 = tk.Button(text='Use Skeleton Head?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_sh()])
btn2.place(x=640, y=654)
else:
if inv in ['crest']:
img_loc = ('resources\\images\\witch.gif')
bg_img = tk.PhotoImage(file = img_loc)
bg_lbl = tk.Label(image = bg_img)
bg_lbl.image = bg_img
bg_lbl.place(x=0, y=0)
lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
lbl1.place(x=400, y=6)
btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
btn1.place(x=330, y=654)
btn2 = tk.Button(text='Use Golden Crest?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_gc()])
btn2.place(x=640, y=654)
else:
img_loc = ('resources\\images\\witch.gif')
bg_img = tk.PhotoImage(file = img_loc)
bg_lbl = tk.Label(image = bg_img)
bg_lbl.image = bg_img
bg_lbl.place(x=0, y=0)
lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
lbl1.place(x=500, y=6)
btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
command = lambda:[btn1.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
btn1.place(x=480, y=654)
A few tips: (1) I would strongly suggest mastering the basics before attempting to work with Tkinter. 一些提示:(1)我强烈建议在尝试使用Tkinter之前先掌握基础知识。 For example: 例如:
if rand==1 or rand==2 or rand==3 or rand==4 or rand==5 or rand==6 or rand==7 or rand==8 or rand==9 or rand==10 or rand==11 or rand==12:
Can be succinctly shortened to: 可以简化为:
if rand <= 12:
(2) For your particular work: I would strongly suggest you learn about class objects and the hasattr() function. (2)对于您的特定工作:我强烈建议您学习类对象和hasattr()函数。 I really like this line that I read somewhere: "A class is more or less a fancy wrapper for a dict of attributes to objects." 我真的很喜欢我在某处读到的这行代码:“一个类或多或少是一个对象属性的字典。 which should point you in the right direction with respect to checking if an item exists in an inventory. 这应该为您指明检查库存中是否存在某项物品的正确方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.