2016-06-15 3 views
1

저는 Tkinter에서 메모리 게임을 만들고 있습니다. 그러나 나는 카드를 뒤집으려고하고있다. 첫 번째 카드가 작동하면 뒤집습니다. 그런 다음 두 번째 카드가 뒤집히지 않고 곧바로 뒤집습니다. 당신은/당신이 flippedTiles을 삭제하지 않는 상태를 아래로 향하게하면 타일의 얼굴을 변화에도 불구하고 나에게tkinter 메모리 게임 만들기

import tkinter as tk 
import time 
from random import randint 
from random import shuffle 
win = tk.Tk() 
canvas = tk.Canvas(win, width = 500, height = 500) 
canvas.pack() 
class Tile(object): 
    def __init__(self, x, y, text): 
     self.y = y 
     self.x = x 
     self.text = text 
    def drawFaceDown(self): 
     canvas.create_rectangle(self.x, self.y, self.x + 70, self.y + 70, fill = "blue") 
     self.isFaceUp = False 
    def drawFaceUp(self): 
     canvas.create_rectangle(self.x, self.y, self.x + 70, self.y + 70, fill = "blue") 
     canvas.create_text(self.x + 35, self.y + 35, text = self.text, width = 70) 
     self.isFaceUp = True 
    def isUnderMouse(self, event): 
     if(event.x > self.x and event.x < self.x + 70): 
      if(event.y > self.y and event.y < self.y + 70): 
       return True 

tiles = [] 
colors = [ 
    "Red", 
    "Orange", 
    "Yellow", 
    "Green", 
    "Blue", 
    "Purple", 
    "Pink", 
    "Brown", 
    "Black", 
    "Gray", 
    "Magenta", 
    "Maroon" 
] 

selected = [] 
for i in range(10): 
    randomInd = randint(0, len(colors) - 1) 
    color = colors[randomInd] 
    selected.append(color) 
    selected.append(color) 
    del colors[randomInd] 
shuffle(selected) 

flippedTiles = [] 

def mouseClicked(self): 
    global numFlipped 
    global flippedTiles 
    for i in range(len(tiles)): 
     if tiles[i].isUnderMouse(self): 
      if (len(flippedTiles) < 2 and not(tiles[i].isFaceUp)) : 
       tiles[i].drawFaceUp() 
       flippedTiles.append(tiles[i]) 
      if (len(flippedTiles) == 2): 
       if not(flippedTiles[0].text == flippedTiles[1].text): 
        time.sleep(1) 
        flippedTiles[0].drawFaceDown() 
        flippedTiles[1].drawFaceDown() 

NUM_COLS = 5 
NUM_ROWS = 4 

for x in range(0,NUM_COLS): 
    for y in range(0,NUM_ROWS): 
      tiles.append(Tile(x * 78 + 10, y * 78 + 40, selected.pop())) 

for i in range(len(tiles)): 
    tiles[i].drawFaceDown() 

win.bind("<Button-1>", mouseClicked) 


win.mainloop() 
+0

, 프로그램은 하나의 카드를 누르면, 또는 제 2 매칭 카드가 발견되면 후 중지합니다. 재미있는 ... – Zizouz212

답변

0

도와주세요 수 있습니다. 둘 다 정확하다면, 올바른 것들은 flippedTiles에 머물러야합니다. 따라서 일치 후, len(flippedTiles)은 2 이하가되지 않을 것입니다.이를 해결하기 위해, 뒤집힌 타일 수를 추적하여 두 번 클릭 한 후에 0으로 설정해야합니다.

By 덧붙여 you should not use sleep with tkinter. 대신 after 메서드가 있습니다. 프로그램과 함께 주위에 조금 재생

flippedThisTurn = 0 
def mouseClicked(event): 
    global flippedTiles 
    global flippedThisTurn 
    for tile in tiles: 
     if tile.isUnderMouse(event): 
      if (not(tile.isFaceUp)) : 
       tile.drawFaceUp() 
       flippedTiles.append(tile) 
       flippedThisTurn += 1 

      if (flippedThisTurn == 2): 
       win.after(1000, checkTiles) 
       flippedThisTurn = 0 

def checkTiles(): 
    if not(flippedTiles[-1].text == flippedTiles[-2].text): #check last two elements 
     flippedTiles[-1].drawFaceDown() #facedown last two elements 
     flippedTiles[-2].drawFaceDown() 
     del flippedTiles[-2:] #remove last two elements