Advertisement
boris-vlasenko

Найди пару

Oct 13th, 2017
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice
  3. import copy
  4. import time
  5. root = Tk()
  6.  
  7. canv = Canvas(bg='white')
  8. canv.pack(fill=BOTH,expand=1)
  9.  
  10.  
  11. nr = 0
  12. nc = 0
  13. colors = ('yellow','orange','green')
  14.  
  15. class Cell():
  16.     def __init__(self):
  17.         self.color = choice(colors)
  18.         self.value = rnd(0,3)
  19.         self.backcolor = 'gray'
  20.         self.backvalue = ''
  21.    
  22.     def info(self):
  23.         return self.color + str(self.value)
  24.        
  25.     def reverse(self):
  26.         self.color, self.backcolor = self.backcolor, self.color
  27.         self.value, self.backvalue = self.backvalue, self.value
  28.         paint()
  29.  
  30. def new_game(event=0):     
  31.     global a,nr,nc,m,x0,y0,opencells, start, prevCell
  32.     print('new_game')
  33.     start = True
  34.     prevCell = None
  35.     nr += 2
  36.     nc += 2
  37.     m = 30
  38.     x0 = 20
  39.     y0 = 20
  40.     root.geometry(str(nc*m+2*x0)+'x'+str(nr*m+2*y0)+'+100+100')
  41.     opencells = 0
  42.    
  43.     cells = [Cell() for x in range(nr*nc//2)]
  44.     cells += copy.deepcopy(cells)
  45.            
  46.     a = []
  47.     for r in range(nr):
  48.         a.append([])       
  49.         for c in range(nc):
  50.             number = rnd(len(cells))
  51.             a[-1].append(cells.pop(number))
  52.  
  53.     root.bind('<1>')   
  54.     paint()
  55.     root.after(1000,start_game)
  56.    
  57. def start_game():
  58.     for r in range(nr):
  59.         for c in range(nc):
  60.             a[r][c].reverse()
  61.     root.bind('<1>',click)
  62.    
  63. def paint():
  64.     canv.delete(ALL)
  65.     for r in range(nr):
  66.         for c in range(nc):
  67.             x = x0 + c*m
  68.             y = y0 + r*m
  69.             canv.create_rectangle(x,y,x+m,y+m,fill=a[r][c].color)
  70.             canv.create_text(x+m//2,y+m//2,text=a[r][c].value, font = 'Arial ' + str(m*2//3))
  71.            
  72.            
  73. def click(event):
  74.     global prevCell,opencells
  75.  
  76.     print('click')
  77.     c = (event.x - x0)//m
  78.     r = (event.y - y0)//m
  79.     if a[r][c].color == 'gray':
  80.         if prevCell is None:
  81.             a[r][c].reverse()
  82.             prevCell = a[r][c]
  83.         else:
  84.             a[r][c].reverse()
  85.             if a[r][c].info() != prevCell.info():
  86.                 root.after(500,a[r][c].reverse)
  87.                 root.after(500,prevCell.reverse)
  88.             else:
  89.                 opencells += 2
  90.             if opencells == nr*nc :
  91.                 print ('you win')
  92.                 #~ winwin = Tk()
  93.                 #~ lab = Label(winwin, text = "sdfgsdfg")
  94.                 #~ lab.pack()
  95.                 canv.delete(ALL)
  96.                 canv.create_text(10,10,text='dfgsdfg')
  97.                 root.bind('<1>',new_game)
  98.  
  99.             prevCell = None
  100.            
  101.  
  102.  
  103.    
  104. new_game()     
  105.  
  106.  
  107.  
  108.  
  109.  
  110. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement