2014-06-19 88 views
0

好的,所以我必须为学校做这个项目,并使用python 2.7制作匹配的纸牌游戏。这是我的代码。Python 2.7 tkinter卡匹配游戏

import sys 
import Tkinter 
import random 
from Tkinter import * 
#==== Main Window ====# 
root=Tk() 
root.title('Matching Game') 
root.geometry('750x770+400+20') 
#===== Load Images =====# 
back= PhotoImage(file='images/back.gif') 
card1=PhotoImage(file='images/card_front_01.gif') 
card2=PhotoImage(file='images/card_front_02.gif') 
card3=PhotoImage(file='images/card_front_03.gif') 
card4=PhotoImage(file='images/card_front_04.gif') 
card5=PhotoImage(file='images/card_front_05.gif') 
card6=PhotoImage(file='images/card_front_06.gif') 
card7=PhotoImage(file='images/card_front_07.gif') 
card8=PhotoImage(file='images/card_front_08.gif') 
card9=PhotoImage(file='images/card_front_09.gif') 
cards=[card1,card2,card3,card4,card5,card6,card7,card8,card9] 
useable=[] 
useable.append(cards[random.randrange(0,9)]) 
useable.append(cards[random.randrange(0,9)]) 
useable.append(cards[random.randrange(0,9)]) 
useable.append(cards[random.randrange(0,9)]) 
useable.append(cards[random.randrange(0,9)]) 
useable.append(cards[random.randrange(0,9)]) 

f1=useable[random.randrange(0,6)] 
f2=useable[random.randrange(0,6)] 
f3=useable[random.randrange(0,6)] 
f4=useable[random.randrange(0,6)] 
f5=useable[random.randrange(0,6)] 
f6=useable[random.randrange(0,6)] 
f7=useable[random.randrange(0,6)] 
f8=useable[random.randrange(0,6)] 
f9=useable[random.randrange(0,6)] 
f10=useable[random.randrange(0,6)] 
f11=useable[random.randrange(0,6)] 
f12=useable[random.randrange(0,6)] 
#===========Front=======# 
front1=Label(image=f1) 
front1.grid(row=1,column=1) 
front2=Label(image=f2) 
front2.grid(row=1,column=2) 
front3=Label(image=f3) 
front3.grid(row=1,column=3) 
front4=Label(image=f4) 
front4.grid(row=1,column=4) 
front5=Label(image=f5) 
front5.grid(row=2,column=1) 
front6=Label(image=f6) 
front6.grid(row=2,column=2) 
front7=Label(image=f7) 
front7.grid(row=2,column=3) 
front8=Label(image=f8) 
front8.grid(row=2,column=4) 
front9=Label(image=f9) 
front9.grid(row=3,column=1) 
front10=Label(image=f10) 
front10.grid(row=3,column=2) 
front11=Label(image=f11) 
front11.grid(row=3,column=3) 
front12=Label(image=f12) 
front12.grid(row=3,column=4) 


#========Functions======# 
def flip(y): 
global step,card1,card2 
if step==1: 
Button.config(image=All[the_cards][y][0]) 
card1=x 
step=2 
elif step==2: 
Button.config(image=All[the_cards][y][0]) 
card2=x 
step=3 
if card[card1][0]==card[card2][0]: 
card[card1][1]=1 
card[card2][1]=1 
step=0 







#=======Back======# 
button=Button(image=back,command=lambda:flip(1)) 
button.grid(row=1,column=1) 
button=Button(image=back,command=lambda:flip(2)) 
button.grid(row=1,column=2) 
button=Button(image=back,command=lambda:flip(3)) 
button.grid(row=1,column=3) 
button=Button(image=back,command=lambda:flip(4)) 
button.grid(row=1,column=4) 
button=Button(image=back,command=lambda:flip(5)) 
button.grid(row=2,column=1) 
button=Button(image=back,command=lambda:flip(6)) 
button.grid(row=2,column=2) 
button=Button(image=back,command=lambda:flip(7)) 
button.grid(row=2,column=3) 
button=Button(image=back,command=lambda:flip(8)) 
button.grid(row=2,column=4) 
button=Button(image=back,command=lambda:flip(9)) 
button.grid(row=3,column=1) 
button=Button(image=back,command=lambda:flip(10)) 
button.grid(row=3,column=2) 
button=Button(image=back,command=lambda:flip(11)) 
button.grid(row=3,column=3) 
button=Button(image=back,command=lambda:flip(12)) 
button.grid(row=3,column=4) 

#=========Main Program=======# 
the_cards=[[0,0]] 
All=[1,2,3,4,5,6,7,8,9] 
random.shuffle(All) 
All=All[0:6] 
All=All*2 
random.shuffle(All) 
for i in range(0,12): 
the_cards.append([the_cards[i],0]) 

card1=100 
card2=101 
step=1 


root.mainloop() 

我的错误是 “线106,在翻盖 Button.config(图像=所有[the_cards] [Y] [0]) 类型错误:列表索引必须是整数,而不是列出”

有人可以告诉我为什么这不起作用,以及如何解决它?谢谢!!!

+2

错误是因为'All'和'the_cards'都是列表,所以后者不能用来索引前者。 – jonrsharpe

回答

0

那么,错误信息就会立即给出问题。

相信错误信息:它告诉你到底是什么问题。 “列表索引必须是整数,没有列出

当你这样做All[the_cards],问自己“什么是the_cards?”

0

也许在地方的

All[ the_cards ][y][0] 

你应该使用

All[ the_cards[y] ][0] 

All[ the_cards[y][0] ] 

但我没有测试它。