forked from flatplanet/Intro-To-TKinter-Youtube-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.py
146 lines (116 loc) · 4.33 KB
/
match.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from tkinter import *
import random
from tkinter import messagebox
root = Tk()
root.title('Codemy.com - Match Game!')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("460x450")
global winner, matches
# Set Winner counter to 0
winner = 0
# Create our matches
matches = [1,1,2,2,3,3,4,4,5,5,6,6]
# Shuffle our matches
random.shuffle(matches)
# Create button frame
my_frame = Frame(root)
my_frame.pack(pady=10)
# Define some variables
count = 0
answer_list = []
answer_dict = {}
# Reset the Game
def reset():
global matches, winner
winner = 0
# Create our matches
matches = [1,1,2,2,3,3,4,4,5,5,6,6]
# Shuffle our matches
random.shuffle(matches)
# Reset Label
my_label.config(text="")
# Reset our Tiles
button_list = [b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11]
# Loop thru buttons and change colors
for button in button_list:
button.config(text=" ", bg="SystemButtonFace", state="normal")
# Create winner function
def win():
my_label.config(text="Congratulations! You Win!!!")
button_list = [b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11]
# Loop thru buttons and change colors
for button in button_list:
button.config(bg="yellow")
# Function for clicking buttons
def button_click(b, number):
global count, answer_list, answer_dict, winner
if b["text"] == ' ' and count < 2:
b["text"] = matches[number]
# Add number to answer list
answer_list.append(number)
# Add button and number to Answer Dictionary
answer_dict[b] = matches[number]
# Increment our Counter
count += 1
#print(answer_dict)
# Start to determine correct or not
if len(answer_list) == 2:
if matches[answer_list[0]] == matches[answer_list[1]]:
my_label.config(text="MATCH!")
for key in answer_dict:
key["state"] = "disabled"
count = 0
answer_list = []
answer_dict = {}
# Increment our winner counter
winner += 1
if winner == 6:
win()
else:
#my_label.config(text="DOH!")
count = 0
answer_list = []
# pop up box
messagebox.showinfo("Incorrect!", "Incorrect")
# Reset the buttons
for key in answer_dict:
key["text"] = " "
answer_dict = {}
# Define our buttons
b0 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b0, 0), relief="groove")
b1 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b1, 1), relief="groove")
b2 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b2, 2), relief="groove")
b3 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b3, 3), relief="groove")
b4 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b4, 4), relief="groove")
b5 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b5, 5), relief="groove")
b6 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b6, 6), relief="groove")
b7 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b7, 7), relief="groove")
b8 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b8, 8), relief="groove")
b9 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b9, 9), relief="groove")
b10 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b10, 10), relief="groove")
b11 = Button(my_frame, text=' ', font=("Helvetica", 20), height=3, width=6, command=lambda: button_click(b11, 11), relief="groove")
# Grid our Buttons
b0.grid(row=0, column=0)
b1.grid(row=0, column=1)
b2.grid(row=0, column=2)
b3.grid(row=0, column=3)
b4.grid(row=1, column=0)
b5.grid(row=1, column=1)
b6.grid(row=1, column=2)
b7.grid(row=1, column=3)
b8.grid(row=2, column=0)
b9.grid(row=2, column=1)
b10.grid(row=2, column=2)
b11.grid(row=2, column=3)
my_label = Label(root, text="")
my_label.pack(pady=20)
# Create a menu
my_menu = Menu(root)
root.config(menu=my_menu)
# Create an Options Dropdown Menu
option_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Options", menu=option_menu)
option_menu.add_command(label="Reset Game", command=reset)
option_menu.add_separator()
option_menu.add_command(label="Exit Game", command=root.quit)
root.mainloop()