我正在 Tkinter 中创建一个扫雷游戏。整个游戏以炸弹和点击结束;但是我不知道如何在游戏中制作“You Win”。它不必是精确的,但我想要一种让用户能够赢得游戏的方法。我在想“如果按钮(不是炸弹)是忘记(),然后打印一个消息框,上面写着“你赢了”。非常感谢任何帮助
from tkinter import *#imports tkinter
from tkinter import messagebox#imports the module messagebox from tkinter
import pygame #imports the module Pygame
sounds = pygame.mixer #allows the import of sounds a.k.a .wav files
sounds.init()#init() is called when an object is created from a class
app=Tk()#creates the app
app.geometry('250x230')#the exact size/grid of the app
app.resizable(0,0)#0,0 allows a "lock" on the app
def destroy1():# a function that destroys buttons so user cannot click on them once they click once
b1h.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy2():# a function that destroys buttons so user cannot click on them once they click once
bf7.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy3():
b11v.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy4():
bf13t.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy5():
ba112.place_forget()#forget() is a function that makes the script 'forget' the button existed
ba11.place_forget()#forget() is a function that makes the script 'forget' the button existed
ba1.place_forget()#forget() is a function that makes the script 'forget' the button existed
b2.place_forget()#forget() is a function that makes the script 'forget' the button existed
ba.place_forget()#forget() is a function that makes the script 'forget' the button existed
bf7.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy6():
bf1io.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy7():
b287.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy71():
b111356.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy72():
b11124.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy73():
bf13245.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy74():
b211245.place_forget()#forget() is a function that makes the script 'forget' the button existed
def destroy75():
b21134.place_forget()#forget() is a function that makes the script 'forget' the button existed
def increase():#a function to increase font size
fontsize= fontStyle['size']#makes a variable call fontsize
labelExample['text'] = fontsize + 2#the label of the text is equal to the fontsize + 2
fontStyle.configure(size=fontsize+2)#configure() is the function that allows the configuration of the fontsize
def win():
s = sounds.Sound("Sound Effect (3).wav")#plays a selected .wav file from the folder
s.play()#play() is the function that plays the wav.file in the tkinter
messagebox.showinfo("Lose", "You Lose!")#displays a messagebox
app.destroy()#closes the tkinter window
def bomb():
s = sounds.Sound("C.wav")#plays a selected .wav file from the folder
s.play()#play() is the function that plays the wav.file in the tkinter
messagebox.showinfo("Lose", "You Lose!")#displays a messagebox
app.destroy()#closes the tkinter window
def instruct():#A function
s = sounds.Sound("07_button7.wav")#plays a selected .wav file from the folder
s.play()#play() is the function that plays the wav.file in the tkinter
messagebox.showinfo("Instruction", "The numbers on the board represent how many bombs are adjacent to a square. For example, if a square has a 2 on it, then there are 2 bombs next to that square. The bombs could be above, below, right left, or diagonal to the square. Avoid all the bombs to win Minesweeper.")
Label(app, text = "Mini Minesweeper", font=("Courier",15)).pack()#
Label(app, text = "There are two bombs", font=("Courier",10)).pack()#
#row 1
b787 = Button(app, text = 'Help\n', width = 6, height=2 , command=instruct) # a button that displays an answer in a text, and when pressed it activates incorrect()
b787.place (relx=0.01, rely=0.85, anchor =W)#rely and relx controls the format where the button is placed
b1 = Button(app, text = '', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b1.place (relx=0.10, rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
b1h = Button(app, text = '', width = 6, command=lambda : [destroy1(), bomb()] ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b1h.place (relx=0.10, rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
bf = Button(app, text = '1' , width = 6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf.place (relx=0.30 , rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
bf7 = Button(app, text = '', width = 6, command=destroy2) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf7.place (relx=0.30 , rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
b2 = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
b2.place(relx=0.50 , rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
ba = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba.place(relx=0.70, rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
#row 2
b11 = Button(app, text = '2', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b11.place (relx=0.10, rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
b11v = Button(app, text = '', width = 6, command= destroy3 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b11v.place (relx=0.10, rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
bf1 = Button(app, text = '2', width = 6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf1.place (relx=0.30 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
bf1io = Button(app, text = '', width = 6, command=destroy6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf1io.place (relx=0.30 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
b21 = Button(app, text = '1', width = 6)# a button that displays an answer in a text, and when pressed it activates correct()
b21.place(relx=0.50 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
b287 = Button(app, text = '', width = 6, command= destroy7)# a button that displays an answer in a text, and when pressed it activates correct()
b287.place(relx=0.50 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
ba1 = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba1.place(relx=0.70, rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
#row3
b111 = Button(app, text = '1', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b111.place (relx=0.10, rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
b111356 = Button(app, text = '', width = 6, command=destroy71 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b111356.place (relx=0.10, rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
bf13 = Button(app, text = '', width =6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf13.place (relx=0.30 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
bf13t = Button(app, text = '', width =6, command=lambda : [destroy4(), bomb()] ) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf13t.place (relx=0.30 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
b211 = Button(app, text = '1', width = 6)# a button that displays an answer in a text, and when pressed it activates correct()
b211.place(relx=0.50 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
b21134 = Button(app, text = '', width = 6, command=destroy75)# a button that displays an answer in a text, and when pressed it activates correct()
b21134.place(relx=0.50 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
ba11 = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba11.place(relx=0.70, rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
#row 4
b1112 = Button(app, text = '1', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b1112.place (relx=0.10, rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
b11124 = Button(app, text = '', width = 6, command= destroy72 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b11124.place (relx=0.10, rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
bf132 = Button(app, text = '1', width = 6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf132.place (relx=0.30 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
bf13245 = Button(app, text = '', width = 6, command= destroy73) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf13245.place (relx=0.30 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
b2112 = Button(app, text = '1', width = 6)# a button that displays an answer in a text, and when pressed it activates correct()
b2112.place(relx=0.50 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
b211245 = Button(app, text = '', width = 6, command= destroy74)# a button that displays an answer in a text, and when pressed it activates correct()
b211245.place(relx=0.50 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
ba112 = Button(app, text = '', width =6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba112.place(relx=0.70, rely=0.60, anchor =W)#rely and relx controls the format where the button is placed