如果/ else循环系统,我如何修复我的库存? [基于Python GUI的冒险游戏]

时间:2018-06-18 08:26:44

标签: python user-interface tkinter

我正在尝试重写一个旧的基于GUI的冒险游戏,我遇到了麻烦。我只是一个谦虚的初学者,所以我无法正确诊断这里发生了什么。在我的代码中,我使用if和if语句来查看他们的库存中是否有skull项(inv)。即使inv==skull,程序也会继续下一个else,就像他们的库存中没有头骨一样,而不是去第64行并看到inv==('skull')

非常感谢任何和所有帮助,包括如何通过代码进行优化,而不仅仅是修复它:)

编辑:我已经开始尝试使用列表(元组),然后检查项目是否在列表中,但它仍然无效:(

编辑代码:

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)

1 个答案:

答案 0 :(得分:0)

一些提示: (1)在尝试使用Tkinter之前,我强烈建议掌握基础知识。例如:

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:

可以简洁地缩写为:

if rand <= 12:

(2)对于您的特定工作:我强烈建议您了解类对象和hasattr()函数。我真的很喜欢我在某处阅读的这一行:&#34;一个类或多或少是一个花哨的包装器,用于属性对象的字典。&#34;这应该指向正确的方向,检查物品是否存在于库存中。