Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, choice
- import copy
- import time
- root = Tk()
- canv = Canvas(bg='white')
- canv.pack(fill=BOTH,expand=1)
- nr = 0
- nc = 0
- colors = ('yellow','orange','green')
- class Cell():
- def __init__(self):
- self.color = choice(colors)
- self.value = rnd(0,3)
- self.backcolor = 'gray'
- self.backvalue = ''
- def info(self):
- return self.color + str(self.value)
- def reverse(self):
- self.color, self.backcolor = self.backcolor, self.color
- self.value, self.backvalue = self.backvalue, self.value
- paint()
- def new_game(event=0):
- global a,nr,nc,m,x0,y0,opencells, start, prevCell
- print('new_game')
- start = True
- prevCell = None
- nr += 2
- nc += 2
- m = 30
- x0 = 20
- y0 = 20
- root.geometry(str(nc*m+2*x0)+'x'+str(nr*m+2*y0)+'+100+100')
- opencells = 0
- cells = [Cell() for x in range(nr*nc//2)]
- cells += copy.deepcopy(cells)
- a = []
- for r in range(nr):
- a.append([])
- for c in range(nc):
- number = rnd(len(cells))
- a[-1].append(cells.pop(number))
- root.bind('<1>')
- paint()
- root.after(1000,start_game)
- def start_game():
- for r in range(nr):
- for c in range(nc):
- a[r][c].reverse()
- root.bind('<1>',click)
- def paint():
- canv.delete(ALL)
- for r in range(nr):
- for c in range(nc):
- x = x0 + c*m
- y = y0 + r*m
- canv.create_rectangle(x,y,x+m,y+m,fill=a[r][c].color)
- canv.create_text(x+m//2,y+m//2,text=a[r][c].value, font = 'Arial ' + str(m*2//3))
- def click(event):
- global prevCell,opencells
- print('click')
- c = (event.x - x0)//m
- r = (event.y - y0)//m
- if a[r][c].color == 'gray':
- if prevCell is None:
- a[r][c].reverse()
- prevCell = a[r][c]
- else:
- a[r][c].reverse()
- if a[r][c].info() != prevCell.info():
- root.after(500,a[r][c].reverse)
- root.after(500,prevCell.reverse)
- else:
- opencells += 2
- if opencells == nr*nc :
- print ('you win')
- #~ winwin = Tk()
- #~ lab = Label(winwin, text = "sdfgsdfg")
- #~ lab.pack()
- canv.delete(ALL)
- canv.create_text(10,10,text='dfgsdfg')
- root.bind('<1>',new_game)
- prevCell = None
- new_game()
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement