Advertisement
gorskaja2019

Untitled

Apr 3rd, 2019
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. from tkinter import *
  2. from random import *
  3. from time import *
  4. from tkinter import messagebox
  5.  
  6. root = Tk()
  7. root.title('Мемори')
  8. root.resizable(0,0)
  9.  
  10. symbols = [u'\u2702', u'\u2702', u'\u2705', u'\u2705', u'\u2708', u'\u2708', u'\u2709',u'\u2709',
  11. u'\u2716',u'\u2716',u'\u2714',u'\u2714',u'\u2728',u'\u2728',u'\u270A',u'\u270A',u'\u270B',
  12. u'\u270B',u'\u270C',u'\u270C',u'\u270F',u'\u270F',u'\u2712',u'\u2712',]
  13. shuffle(symbols)
  14. first = True
  15. prevX = 0
  16. prevY = 0
  17. buttons = {}
  18. button_symbols = {}
  19. cnt = 0
  20. pairs = 0
  21.  
  22. def on_click(event):
  23. t = event.widget
  24. y = int(t.grid_info()['row'])
  25. x = int(t.grid_info()['column'])
  26. global first
  27. global prevX, prevY
  28. global cnt
  29. global pairs
  30. buttons[x, y]['text'] = button_symbols[x, y]
  31. buttons[x, y].update_idletasks()
  32.  
  33. if first:
  34. prevX = x
  35. prevY = y
  36. first = False
  37. cnt += 1
  38. elif prevX != x or prevY != y:
  39. if buttons[x, y]['text'] != buttons[prevX, prevY]['text']:
  40. sleep(0.5)
  41. buttons[x, y]['text'] = ''
  42. buttons[prevX, prevY]['text'] = ''
  43. else:
  44. buttons[x,y].config(bg = 'skyblue')
  45. buttons[prevX,prevY].config(bg = 'skyblue')
  46. buttons[x,y].unbind('<Button-1>')
  47. buttons[prevX,prevY].unbind('<Button-1>')
  48. pairs += 1
  49. first = True
  50. if pairs == len(buttons) // 2:
  51. messagebox.showinfo('Количество ходов', 'Сделано ходов: '+ str(cnt))
  52.  
  53. for x in range(6):
  54. for y in range(4):
  55. button = Button(font = 'Verdana 24', width = 3)
  56. button.grid(column = x, row = y, padx = 5, pady = 5)
  57. button.bind('<Button-1>', on_click)
  58. buttons[x, y] = button
  59. button_symbols[x, y] = symbols.pop()
  60.  
  61. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement