Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import *
- from time import *
- from tkinter import messagebox
- root = Tk()
- root.title('Мемори')
- root.resizable(0,0)
- symbols = [u'\u2702', u'\u2702', u'\u2705', u'\u2705', u'\u2708', u'\u2708', u'\u2709',u'\u2709',
- u'\u2716',u'\u2716',u'\u2714',u'\u2714',u'\u2728',u'\u2728',u'\u270A',u'\u270A',u'\u270B',
- u'\u270B',u'\u270C',u'\u270C',u'\u270F',u'\u270F',u'\u2712',u'\u2712',]
- shuffle(symbols)
- first = True
- prevX = 0
- prevY = 0
- buttons = {}
- button_symbols = {}
- cnt = 0
- pairs = 0
- def on_click(event):
- t = event.widget
- y = int(t.grid_info()['row'])
- x = int(t.grid_info()['column'])
- global first
- global prevX, prevY
- global cnt
- global pairs
- buttons[x, y]['text'] = button_symbols[x, y]
- buttons[x, y].update_idletasks()
- if first:
- prevX = x
- prevY = y
- first = False
- cnt += 1
- elif prevX != x or prevY != y:
- if buttons[x, y]['text'] != buttons[prevX, prevY]['text']:
- sleep(0.5)
- buttons[x, y]['text'] = ''
- buttons[prevX, prevY]['text'] = ''
- else:
- buttons[x,y].config(bg = 'skyblue')
- buttons[prevX,prevY].config(bg = 'skyblue')
- buttons[x,y].unbind('<Button-1>')
- buttons[prevX,prevY].unbind('<Button-1>')
- pairs += 1
- first = True
- if pairs == len(buttons) // 2:
- messagebox.showinfo('Количество ходов', 'Сделано ходов: '+ str(cnt))
- for x in range(6):
- for y in range(4):
- button = Button(font = 'Verdana 24', width = 3)
- button.grid(column = x, row = y, padx = 5, pady = 5)
- button.bind('<Button-1>', on_click)
- buttons[x, y] = button
- button_symbols[x, y] = symbols.pop()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement