我正在Python Tkinter中开发一款内存游戏,每当有比赛时,我都会尝试销毁卡片。就像当两张牌被翻转并匹配时,它们需要被销毁。这些卡片是用纽扣做成的。我代码中的check函数检查是否匹配,如果匹配则销毁卡片。问题是它没有这么做。有人能帮我做吗?我的代码:
from tkinter import *
from random import shuffle
from tkinter import messagebox
screen = Tk()
screen.title("Disney Princesses Memory Game")
width = screen.winfo_screenwidth()
height = screen.winfo_screenheight()
screen.geometry("%dx%d" % (width, height))
screen.configure(bg="#e0bce5")
title = Label(screen, text="Memory Game", font=("David", 50, "underline", "bold"), bg="#e0bce5")
title.place(x=400, y=20)
rules = Label(screen, text="Single click to flip the first card, double click to flip the second card",
font=("David", 30, "bold"), bg="#e0bce5")
rules.place(x=100, y=100)
images_list = [
PhotoImage(file="images/aurora.png"),
PhotoImage(file="images/belle.png"),
PhotoImage(file="images/cinderella.png"),
PhotoImage(file="images/jasmine.png"),
PhotoImage(file="images/mulan.png"),
PhotoImage(file="images/rapunzel.png"),
PhotoImage(file="images/snow white.png"),
PhotoImage(file="images/tiana.png")
]
buttons_list = []
chosen_images = []
flipped = []
num_list = []
card = PhotoImage(file="images/card.png")
count = 0
no_press = False
turn = 0
player1_points = 0
player2_points = 0
def choose_images():
while len(chosen_images) < 16:
duplicate = images_list.copy()
images_list.extend(duplicate)
shuffle(images_list)
for i2 in images_list:
chosen_images.append(i2)
def turns():
global turn
turn += 1
if turn % 2 == 0:
turn_label.configure(text="Player 1's turn")
else:
turn_label.configure(text="Player 2's turn")
def replace_card(c, d):
global flipped, count, no_press
if no_press is True:
return
count += 1
if count > 2:
count = 0
no_press = True
check()
else:
buttons_list[c][d].configure(image=chosen_images[(c*7)+d])
flipped.append(buttons_list[c][d])
def reset():
global card, no_press
for element in flipped:
element.configure(image=card)
flipped.clear()
no_press = False
turns()
def sleep_secs():
global no_press
screen.after(2000, reset)
def check():
global player1_points, player2_points
if flipped[0] == flipped[1]:
flipped[0].destroy()
flipped[1].destroy()
flipped.clear()
if turn_label["text"] == "Player 1's turn":
player1_points += 1
else:
player2_points += 1
else:
sleep_secs()
if player1_points + player2_points == 8:
if player1_points > player2_points:
messagebox.showinfo("Disney Princesses Memory Game", "Player 1 is the winner!")
elif player2_points > player1_points:
messagebox.showinfo("Disney Princesses Memory Game", "Player 2 is the winner!")
else:
messagebox.showinfo("Disney Princesses Memory Game", "The game ended in a draw!")
choose_images()
turn_label = Label(screen, text="Player 1's turn!", font=("David", 20), fg="red", bg="#e0bce5")
turn_label.place(x=520, y=180)
x = 100
y = 250
for i in range(2):
buttons_list.append([])
for j in range(8):
a = Button(screen, image=card, command=lambda i=i, j=j: replace_card(i, j))
x += 100
a.place(x=x, y=y)
buttons_list[i].append(a)
y += 100
x = 100
screen.mainloop()
非常感谢PCM再次帮助我!更新代码:
from tkinter import *
from random import shuffle
from tkinter import messagebox
screen = Tk()
screen.title("Disney Princesses Memory Game")
width = screen.winfo_screenwidth()
height = screen.winfo_screenheight()
screen.geometry("%dx%d" % (width, height))
screen.configure(bg="#e0bce5")
title = Label(screen, text="Memory Game", font=("David", 50, "underline", "bold"), bg="#e0bce5")
title.place(x=400, y=20)
rules = Label(screen, text="Single click to flip the first card, double click to flip the second card",
font=("David", 30, "bold"), bg="#e0bce5")
rules.place(x=100, y=100)
images_list = [
PhotoImage(file="images/aurora.png"),
PhotoImage(file="images/belle.png"),
PhotoImage(file="images/cinderella.png"),
PhotoImage(file="images/jasmine.png"),
PhotoImage(file="images/mulan.png"),
PhotoImage(file="images/rapunzel.png"),
PhotoImage(file="images/snow white.png"),
PhotoImage(file="images/tiana.png")
]
buttons_list = []
chosen_images = []
flipped = []
num_list = []
card = PhotoImage(file="images/card.png")
count = 0
no_press = False
turn = 0
player1_points = 0
player2_points = 0
def choose_images():
while len(chosen_images) < 16:
duplicate = images_list.copy()
images_list.extend(duplicate)
shuffle(images_list)
for i2 in images_list:
chosen_images.append(i2)
def turns():
global turn
turn += 1
if turn % 2 == 0:
turn_label.configure(text="Player 1's turn")
else:
turn_label.configure(text="Player 2's turn")
def replace_card(c, d):
global flipped, count, no_press
flipped.append(buttons_list[c][d])
if no_press is True:
return
count += 1
if count > 2:
no_press = True
check()
count = 0
else:
buttons_list[c][d].configure(image=chosen_images[(c * 7) + d])
def reset():
global card, no_press
for element in flipped:
element.configure(image=card)
flipped.clear()
no_press = False
turns()
def sleep_secs():
global no_press
screen.after(2000, reset)
def check():
global player1_points, player2_points
if flipped[0]["image"] == flipped[1]["image"]:
flipped[0].destroy()
flipped[1].destroy()
flipped.clear()
if turn_label["text"] == "Player 1's turn":
player1_points += 1
else:
player2_points += 1
else:
sleep_secs()
if player1_points + player2_points == 8:
if player1_points > player2_points:
messagebox.showinfo("Disney Princesses Memory Game", "Player 1 is the winner!")
elif player2_points > player1_points:
messagebox.showinfo("Disney Princesses Memory Game", "Player 2 is the winner!")
else:
messagebox.showinfo("Disney Princesses Memory Game", "The game ended in a draw!")
choose_images()
turn_label = Label(screen, text="Player 1's turn!", font=("David", 20), fg="red", bg="#e0bce5")
turn_label.place(x=520, y=180)
x = 100
y = 250
for i in range(2):
buttons_list.append([])
for j in range(8):
a = Button(screen, image=card, command=lambda i=i, j=j: replace_card(i, j))
x += 100
a.place(x=x, y=y)
buttons_list[i].append(a)
y += 100
x = 100
screen.mainloop()
除了我的评论之外,还有很多事情需要更改。
完整代码:
from tkinter import *
from random import shuffle
from tkinter import messagebox
screen = Tk()
screen.title("Disney Princesses Memory Game")
width = screen.winfo_screenwidth()
height = screen.winfo_screenheight()
screen.geometry("%dx%d" % (width, height))
screen.configure(bg="#e0bce5")
title = Label(screen, text="Memory Game", font=("David", 50, "underline", "bold"), bg="#e0bce5")
title.place(x=400, y=20)
rules = Label(screen, text="Single click to flip the first card, double click to flip the second card",
font=("David", 30, "bold"), bg="#e0bce5")
rules.place(x=100, y=100)
images_list = [
PhotoImage(file="images/aurora.png"),
PhotoImage(file="images/belle.png"),
PhotoImage(file="images/cinderella.png"),
PhotoImage(file="images/jasmine.png"),
PhotoImage(file="images/mulan.png"),
PhotoImage(file="images/rapunzel.png"),
PhotoImage(file="images/snow white.png"),
PhotoImage(file="images/tiana.png")
]
buttons_list = []
chosen_images = []
flipped = []
num_list = []
card = PhotoImage(file="images/card.png")
count = 0
no_press = False
turn = 0
player1_points = 0
player2_points = 0
def choose_images():
while len(chosen_images) < 16:
duplicate = images_list.copy()
images_list.extend(duplicate)
shuffle(images_list)
for i2 in images_list:
chosen_images.append(i2)
def turns():
global turn
turn += 1
if turn % 2 == 0:
turn_label.configure(text="Player 1's turn")
else:
turn_label.configure(text="Player 2's turn")
def replace_card(c, d):
global flipped, count, no_press
flipped.append(buttons_list[c][d])
if no_press is True:
return
count += 1
if count > 2:
no_press = True
check()
count = 0
else:
buttons_list[c][d].configure(image=chosen_images[(c*7)+d])
def reset():
global card, no_press
for element in flipped:
element.configure(image=card)
flipped.clear()
no_press = False
turns()
def sleep_secs():
global no_press
screen.after(2000, reset)
def check():
global player1_points, player2_points
if flipped[0]['image'] == flipped[1]['image']:
flipped[0].destroy()
flipped[1].destroy()
flipped.clear()
if turn_label["text"] == "Player 1's turn":
player1_points += 1
else:
player2_points += 1
else:
sleep_secs()
if player1_points + player2_points == 8:
if player1_points > player2_points:
messagebox.showinfo("Disney Princesses Memory Game", "Player 1 is the winner!")
elif player2_points > player1_points:
messagebox.showinfo("Disney Princesses Memory Game", "Player 2 is the winner!")
else:
messagebox.showinfo("Disney Princesses Memory Game", "The game ended in a draw!")
choose_images()
turn_label = Label(screen, text="Player 1's turn!", font=("David", 20), fg="red", bg="#e0bce5")
turn_label.place(x=520, y=180)
x = 100
y = 250
for i in range(2):
buttons_list.append([])
for j in range(8):
a = Button(screen, image=card, command=lambda i=i, j=j: replace_card(i, j))
x += 100
a.place(x=x, y=y)
buttons_list[i].append(a)
y += 100
x = 100
screen.mainloop()