2016-09-30 29 views
-1

Python 3のtkinterで一致するカードゲームを作成しようとしています。プレーヤーには4x4グリッドのグリッドがあり、各ボタンには画像が表示されます。 8組の画像があります。プレーヤーはボタンをクリックし、ボタンは画像を表示します。プレーヤーは別のボタンをクリックして画像を表示します。 2つのボタンが同じ画像を表示する場合、画像はゲームが終了するまで保持されます。そうでなければ、別のボタンをクリックすると、これらの画像は両方とも表示されなくなります。Pythonのtkinterの対戦ゲーム

グリッドを作成することはできますが、実際のゲームを動作させるには問題があります。以下は、これまでに作成したコードです。

#Import all necessary modules. 

from tkinter import * 
from tkinter import ttk 
import random 
#Imnporting shuffle allows us to shuffle a list. 
from random import shuffle 
import time 

#Create canvas and assign it to variable root. 
root = Tk() 

#Making photo accessible 
def stimage(file): 
    return PhotoImage(width=100,height=100,file=file) 

clicks=[] 

clickCount=0 
faceUp=[] 
score=0 

back = PhotoImage(width=100, height=100, file="blank space.gif") 
images = [stimage("homer.gif"), stimage("ducky.gif"), stimage("hulk.gif"), stimage("torus.gif"), stimage("ronaldo-moth.gif"), stimage("beyonce.gif"), stimage("monkey.gif"), stimage("kim.gif")] 

#Function returns a button, equipped with a command that changes its background colour. It takes the argument image. 
def button(n): 
    #Create a button with parent root, and a red colour for image. This will be the back of each card. 
    button = Button(root,height=100,width=100, image=back) 

    #Command for button. Flips over when clicked. 
    def flip(): 
     global clickCount 
     global faceUp 
     clickCount+=1 
     #Make the card display an image. 
     button.config(image=images[n]) 
     #If image of button is displayed, append to list faceUp 
     if button.cget("image")!=back: 
      #Append the button object and image index n to list faceUp 
      faceUp.append([button,n]) 
     if clickCount==2: 
      clickCount=0 
      if faceUp[0][1]==faceUp[1][1]: 
       print("Hello") 
      else: 
       button.config(image=back) 
      faceUp=[] 

    button.config(command=flip) 
    return button 

#Create a list of coordinates that the buttons will occupy. 
coord = [[a,b] for a in range(1,5) for b in range(1,5)] 
#Randomise coordinates so buttons appear in random places. 
random.shuffle(coord) 
buttons=[] 

for i in range(8): 
    buttons.append(button(i)) 
    buttons[2*i].grid(row=coord[i][0], column=coord[i][1]) 
    buttons.append(button(i)) 
    buttons[2*i+1].grid(row=coord[i+8][0], column=coord[i+8][1]) 

不一致のペアをクリックすると、その画像が表示されないため、動作しません。

助けてもらえますか?

答えて

0

あなたが合法であるかどうかをチェックするifelse文を作成する必要があります。合法的であればスクリプトを続行し、そうでない場合はメッセージをユーザーに返します。

+0

詳細を教えてください。 – jlammy

+0

大丈夫です。病気あなたが直接あなた自身でそれを把握するために、いつも楽しいコードXDを与えることなく説明しようとする。基本的には簡単な方法ですが、各カードに値を割り当て、値が一致するかどうかをテストします。元の位置に戻らない場合は、 – TerraPhase

+0

私はすでにそれを持っていますが、それでも動作しません。 – jlammy

関連する問題