簡體   English   中英

銷毀記憶游戲中的按鈕 - Python Tkinter

[英]Destroying buttons in a memory game- Python Tkinter

我正在 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM