我正在学校项目,我必须以记忆匹配的形式显示卡片(即将有2个三分之三的心和2个黑桃。)
newGame中的else语句是我试图让代码显示卡片的地方。我迷失的地方,就是我需要添加的内容,以便卡片显示的方式有2个匹配。我还有文件夹中所有卡片的图像。 (包括背面图像)
我已经尝试过在github和这里查看,但我找不到我需要的东西。我知道如何显示1张或更多卡,但不能显示在" 2d列表中#34;方式。
到目前为止,这是我项目的代码:
from tkinter import *
from tkinter.messagebox import *
import random
class Card(object):
RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
SUITS = ("Spades", 'Hearts', 'Diamonds', 'Clubs')
BACK_Name = "DECK/b.gif"
def __init__(self, rank, suit):
"""Creates a card with the given rank and suit."""
self.rank = rank
self.suit = suit
self.fileName = 'DECK/' + str(rank) + suit[0] + '.gif'
def getFace(self):
return self.face
def setFace(self, value):
self.face = value
def __str__(self):
"""Returns the string representation of a card."""
if self.rank == 1:
rank = 'Ace'
elif self.rank == 11:
rank = 'Jack'
elif self.rank == 12:
rank = 'Queen'
elif self.rank == 13:
rank = 'King'
else:
rank = self.rank
return str(rank) + ' of ' + self.suit
class Deck(object):
""" A deck containing 52 cards."""
def __init__(self, numberOfPairs):
"""Creates a full deck of cards."""
self._cards = []
for suit in Card.SUITS:
for rank in Card.RANKS:
c1 = Card(rank, suit)
self._cards.append(c1)
c2 = Card(rank, suit)
self._cards.append(c2)
def shuffle(self):
"""Shuffles the cards."""
random.shuffle(self._cards)
def deal(self):
"""Removes and returns the top card or None
if the deck is empty."""
if len(self) == 0:
return None
else:
return self._cards.pop(0)
def __len__(self):
"""Returns the number of cards left in the deck."""
return len(self._cards)
def __str__(self):
"""Returns the string representation of a deck."""
self.result = ''
for c in self._cards:
self.result = self.result + str(c) + '\n'
return self.result
class TheGame(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Memory Matching Game")
self.grid()
self.deck = Deck()
self.BackImage = PhotoImage(file = Card.BACK_Name)
'''Row Entry Label and Box'''
self.rowLabel = Label(self, text = "Number of Rows")
self.rowLabel.grid(row = 0, column = 0)
self.rowVar = IntVar()
self.rowVar.set(2)
self.rowEntry = Entry(self, textvariable = self.rowVar)
self.rowEntry.grid(row = 0, column = 1)
'''Column Entry Label and Box'''
self.columnLabel = Label(self, text = "Number of Columns")
self.columnLabel.grid(row = 0, column = 2)
self.columnVar = IntVar()
self.columnVar.set(2)
self.columnEntry = Entry(self, textvariable = self.columnVar)
self.columnEntry.grid(row = 0, column = 3)
self.button1 = Button(self, text = "New Game", command = self.newGame)
self.button1.grid(row = 1, column = 0, columnspan = 2)
self.button2 = Button(self, text = "Turn Over", command = self.turnOver)
self.button2.grid(row = 1, column = 2, columnspan = 2)
def newGame(self):
numberOfPairs = self.columnVar.get() * self.rowVar.get() // 2
suit = random.choice(['s', 'c', 'd', 'h'])
deck = Deck(numberOfPairs)
deck.shuffle()
if self.columnVar.get() * self.rowVar.get() > 20:
showerror(message = "Number of rows X number of columns cannot exceed 20",
parent = self)
elif self.columnVar.get() * self.rowVar.get() %2 != 0:
showerror(message = "Number of rows X number of columns must be even",
parent = self)
else:
def turnOver(self):
pass
def main():
TheGame().mainloop()
main()
非常感谢任何帮助。