-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
132 lines (106 loc) · 3.66 KB
/
main.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
"""
File: Final.py
----------------
"""
import os
import random
import sys
import tkinter
import time
from PIL import ImageTk
from tkinter import messagebox
from intro import choose_level
from intro import choose_mode
MODE = choose_mode()
LEVEL = choose_level()
COL_SIZE = LEVEL[0]
ROW_SIZE = LEVEL[1]
ROUTE = MODE[1]
def main():
root = tkinter.Tk()
root.title("Pairs")
root.geometry("+200+100")
root.resizable(0, 0)
root.bind("<Escape>", lambda event: exit())
root.bind("<Return>", lambda event: restart(root))
draw_board(root)
root.mainloop()
def restart(root):
root.destroy()
python = sys.executable
os.execl(python, python, *sys.argv)
def draw_board(root):
start_time = time.time()
answers = make_answers(root)
default_image = ImageTk.PhotoImage(master=root, file="images/default.png")
buttons = []
for row in range(COL_SIZE):
for col in range(ROW_SIZE):
button = tkinter.Button(root, image=default_image,
command=lambda row=row, column=col:
change_label(buttons, row, column, answers, root, default_image, start_time))
buttons.append(button)
button.grid(column=col, row=row)
buttons = set_on_board(buttons)
return buttons
def set_on_board(raw_list):
set_list = []
for i in range(0, len(raw_list), ROW_SIZE):
set_list.append(raw_list[i:i + ROW_SIZE])
return set_list
def make_answers(root):
cards_total = ROW_SIZE * COL_SIZE // 2
answers = []
for i in range(cards_total):
route = ROUTE + str(i) + '.png'
image = ImageTk.PhotoImage(master=root, file=route)
answers.append(image)
answers.append(image)
random.shuffle(answers)
answers = set_on_board(answers)
return answers
def change_label(buttons, row, col, answers, root, default_image, start_time):
buttons[row][col].config(image=answers[row][col], bg="gray99")
if not is_first(buttons):
first_card_coords = find_first(buttons, row, col)
x = first_card_coords[0]
y = first_card_coords[1]
if answers[row][col] == answers[x][y]:
buttons[row][col].config(state=tkinter.DISABLED, bg="white")
buttons[x][y].config(state=tkinter.DISABLED, bg="white")
if is_over(buttons):
time_passed = int(time.time() - start_time)
messagebox.showinfo(title="Success!", message="Time: " + str(time_passed) + " sec")
buttons[row][col].after(1000, draw_board, root)
else:
buttons[row][col].after(800, hide_buttons, buttons, row, col, x, y, default_image)
def is_first(buttons):
counter = 0
for row in range(COL_SIZE):
for col in range(ROW_SIZE):
button = buttons[row][col]
if button["bg"] == "gray99":
counter += 1
return counter % 2 == 1
def find_first(buttons, row, col):
coords = []
for i in range(COL_SIZE):
for j in range(ROW_SIZE):
button = buttons[i][j]
if button["bg"] == "gray99" and buttons[row][col] != buttons[i][j]:
coords.append(i)
coords.append(j)
return coords
def hide_buttons(buttons, row, col, x, y, default_image):
buttons[row][col].config(image=default_image, bg="white")
buttons[x][y].config(image=default_image, bg="white")
def is_over(buttons):
counter = 0
for row in range(COL_SIZE):
for col in range(ROW_SIZE):
button = buttons[row][col]
if button["state"] != tkinter.DISABLED:
counter += 1
return counter == 0
if __name__ == "__main__":
main()