![](/img/trans.png)
[英]How can I add items and avoid duplicates to my inventory in a text based adventure game?
[英]How would I fix my inventory if/else loop system? [Python GUI Based Adventure Game]
我試圖重寫我的一個基於GUI的舊冒險游戲,並且遇到了麻煩。 我不過是一個謙虛的初學者,所以我無法正確診斷這里發生了什么。 在我的代碼中,我使用if語句來查看他們的庫存( inv
)中是否有skull
。 即使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)我強烈建議在嘗試使用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()函數。 我真的很喜歡我在某處讀到的這行代碼:“一個類或多或少是一個對象屬性的字典。 這應該為您指明檢查庫存中是否存在某項物品的正確方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.