|
| 1 | +# Importing required libraries |
| 2 | +from tkinter import * |
| 3 | +from tkinter import messagebox as mb |
| 4 | +from tkinter import ttk |
| 5 | +import random |
| 6 | + |
| 7 | +# function to create screen for the game |
| 8 | +def board(): |
| 9 | + global value,w |
| 10 | + # initialising screen |
| 11 | + root=Tk() |
| 12 | + root.geometry("320x335") |
| 13 | + root.title("MineSweeper") |
| 14 | + root.resizable(False,False) |
| 15 | + root.eval('tk::PlaceWindow . center') |
| 16 | + # creating label |
| 17 | + w = Label(root, text="Start Playing!",bg='yellow',fg='red') |
| 18 | + # creating buttons |
| 19 | + but11=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but11,root)) |
| 20 | + but12=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but12,root)) |
| 21 | + but13=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but13,root)) |
| 22 | + but14=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but14,root)) |
| 23 | + but15=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but15,root)) |
| 24 | + but21=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but21,root)) |
| 25 | + but22=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but22,root)) |
| 26 | + but23=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but23,root)) |
| 27 | + but24=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but24,root)) |
| 28 | + but25=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but25,root)) |
| 29 | + but31=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but31,root)) |
| 30 | + but32=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but32,root)) |
| 31 | + but33=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but33,root)) |
| 32 | + but34=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but34,root)) |
| 33 | + but35=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but35,root)) |
| 34 | + but41=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but41,root)) |
| 35 | + but42=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but42,root)) |
| 36 | + but43=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but43,root)) |
| 37 | + but44=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but44,root)) |
| 38 | + but45=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but45,root)) |
| 39 | + but51=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but51,root)) |
| 40 | + but52=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but52,root)) |
| 41 | + but53=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but53,root)) |
| 42 | + but54=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but54,root)) |
| 43 | + but55=Button(root,bg="grey", text="", padx=7.5, pady=5,bd=4,font="digifacewide 18",height=1,width=2,command=lambda:game(but55,root)) |
| 44 | + # adding buttons to the screen |
| 45 | + but11.grid(row=1,column=1) |
| 46 | + but12.grid(row=1,column=2) |
| 47 | + but13.grid(row=1,column=3) |
| 48 | + but14.grid(row=1,column=4) |
| 49 | + but15.grid(row=1,column=5) |
| 50 | + but21.grid(row=2,column=1) |
| 51 | + but22.grid(row=2,column=2) |
| 52 | + but23.grid(row=2,column=3) |
| 53 | + but24.grid(row=2,column=4) |
| 54 | + but25.grid(row=2,column=5) |
| 55 | + but31.grid(row=3,column=1) |
| 56 | + but32.grid(row=3,column=2) |
| 57 | + but33.grid(row=3,column=3) |
| 58 | + but34.grid(row=3,column=4) |
| 59 | + but35.grid(row=3,column=5) |
| 60 | + but41.grid(row=4,column=1) |
| 61 | + but42.grid(row=4,column=2) |
| 62 | + but43.grid(row=4,column=3) |
| 63 | + but44.grid(row=4,column=4) |
| 64 | + but45.grid(row=4,column=5) |
| 65 | + but51.grid(row=5,column=1) |
| 66 | + but52.grid(row=5,column=2) |
| 67 | + but53.grid(row=5,column=3) |
| 68 | + but54.grid(row=5,column=4) |
| 69 | + but55.grid(row=5,column=5) |
| 70 | + # adding label to the screen |
| 71 | + w.grid(row=0,column=0,columnspan=6) |
| 72 | + # creating values for each cell from 1-5 and "b" for bomb |
| 73 | + butlist=[but11,but12,but13,but14,but15,but21,but22,but23,but24,but25, |
| 74 | + but31,but32,but33,but34,but35,but41,but42,but43,but44,but45, |
| 75 | + but51,but52,but53,but54,but55] |
| 76 | + vallist=['1','2','3','4','1','2','3','4','1','2','3','4','1','2','3','4', |
| 77 | + '1','2','3','4','b','b','b','b','b'] |
| 78 | + value={} |
| 79 | + random.shuffle(vallist)# shuffle for randomness |
| 80 | + for i in range(25): |
| 81 | + value[butlist[i]]=vallist[i]# assining values to buttons |
| 82 | + root.mainloop() |
| 83 | + |
| 84 | +def game(b,tk): |
| 85 | + if value[b]=='b': # if bomb is clicked |
| 86 | + bomb_clicked(b,tk) |
| 87 | + else: # if number is clicked |
| 88 | + number_clicked(b,int(value[b]),tk) |
| 89 | + |
| 90 | +total =0 |
| 91 | + |
| 92 | +#function when bomb is clicked |
| 93 | +def bomb_clicked(b,tk): |
| 94 | + # making changes to cell |
| 95 | + b['text']="\U0001f600" |
| 96 | + b['relief']=SUNKEN; |
| 97 | + b['bg']="orange" |
| 98 | + global value,total |
| 99 | + # displaying message and asking for replay |
| 100 | + a=mb.askquestion("YOU LOSE"," Your score : " +str(total) +"\nDo you want to play again??") |
| 101 | + |
| 102 | + tk.destroy()# exiting current board |
| 103 | + if a=='yes' : |
| 104 | + total = 0 |
| 105 | + board() |
| 106 | + |
| 107 | +def number_clicked(b,n,tk): |
| 108 | + global value,total |
| 109 | + if n!=0 and b['text']=="": |
| 110 | + # making changes to cell and updating score |
| 111 | + b['text']=n |
| 112 | + total+=n |
| 113 | + value[b]='0' |
| 114 | + w['text']="Your Score : " +str(total) |
| 115 | + if total>=50: # if player reached score of 50 he won |
| 116 | + b['text']="\U0001f600" |
| 117 | + b['relief']=SUNKEN; |
| 118 | + b['bg']="orange" |
| 119 | + # displaying message and asking for replay |
| 120 | + a=mb.askquestion("YOU WON"," Your score : " +str(total) +"\nDo you want to play again??") |
| 121 | + tk.destroy()# exiting current board |
| 122 | + if a=='yes' : |
| 123 | + total=0 |
| 124 | + board() |
| 125 | + # showinfo("YOU WON", "YOUR SCORE : " + str(total)) |
| 126 | + tk.destroy() |
| 127 | + |
| 128 | +board() |
0 commit comments