from pygame import *
from pygame import gfxdraw
from random import sample,choice
init()
FONT = font.Font(font.get_default_font(),50)

class Case(Rect):
	''
	CASE = Surface((61,61))
	CASE.set_colorkey((1,1,1))
	CASERECT = CASE.get_rect()
	gfxdraw.filled_circle(CASE,30,30,30,(200,200,200))
	gfxdraw.aacircle(CASE,30,30,30,(200,200,200))
	def __init__(self,x,y):
		self.jonction = [0,0,0,0]
		self.marqueur = False
		self.image = Case.CASE.copy()
		S = sample(((Case.CASERECT.midtop,0),(Case.CASERECT.midright,1),(Case.CASERECT.midbottom,2),(Case.CASERECT.midleft,3)),3)[:choice((2,3))]
		for j in S:
			draw.line(self.image,(1,1,1),(30,30),j[0],9)
			self.jonction[j[1]] = 1
		Rect.__init__(self,SCREEN.blit(self.image,(x,y)))

	def rotate(self,dir):
		dir -= 2
		if dir == 1:
			self.jonction.insert(0,self.jonction.pop())
		else:
			self.jonction.append(self.jonction.pop(0))
		SCREEN.fill(0,self)
		SCREEN.blit(transform.rotate(self.image,-45*dir),self.move(-13,-13))
		display.update(self)
		time.wait(40)
		self.image = transform.rotate(self.image,-90*dir)
		SCREEN.fill(0,self)
		SCREEN.blit(self.image,self)
		display.update(self)

score = 0
x = 15
y = 10
SCREEN = display.set_mode((61*x,61*y))
SRECT = SCREEN.get_rect()
TIME = 300
time.set_timer(USEREVENT,1000)
while True:
	ALLS = []
	for i in range(x*y):
		ALLS.append(Case(61*(i%x),61*(i/x)))
	START = choice(range(x*y)[::x])
	gfxdraw.filled_circle(ALLS[START].image,30,30,30,(200,0,0))
	gfxdraw.aacircle(ALLS[START].image,30,30,30,(200,0,0))
	SCREEN.blit(ALLS[START].image,ALLS[START])
	ALLS[START].jonction = [1,1,1,1]
	END = choice(range(x*y)[x-1::x])
	gfxdraw.filled_circle(ALLS[END].image,30,30,30,(0,0,200))
	gfxdraw.aacircle(ALLS[END].image,30,30,30,(0,0,200))
	SCREEN.blit(ALLS[END].image,ALLS[END])
	ALLS[END].jonction = [1,1,1,1]
	for i in sample(ALLS,x*y):
		time.wait(10)
		display.update(i)
	event.clear()
	event.post(event.Event(MOUSEBUTTONDOWN,{'button':1,'pos':(0,0)}))
	coup = 0
	while not ALLS[END].marqueur:
		ev = event.wait()
		if ev.type == USEREVENT:
			TIME -=1
			display.set_caption(str(TIME))
		if ev.type == QUIT or not TIME:event.clear();event.post(event.Event(QUIT));break
		elif ev.type == MOUSEBUTTONDOWN and ev.button in (1,3):
			coup += 1
			C = Rect(ev.pos,(0,0)).collidelist(ALLS)
			ALLS[C].rotate(ev.button)
			for i in ALLS:
				i.marqueur = False
				SCREEN.fill(0,i)
				SCREEN.blit(i.image,i)
			temp = [START]
			ALLS[START].marqueur = True
			while temp:
				for case in temp:
					for e,j in enumerate(ALLS[case].jonction):
						if j and SRECT.contains(ALLS[case].move(((0,-61),(61,0),(0,61),(-61,0))[e]))\
						and ALLS[case+(-x,1,x,-1)[e]].marqueur == False\
						and ALLS[case+(-x,1,x,-1)[e]].jonction[e-2]\
						and case != END:
							temp.append(case+(-x,1,x,-1)[e])
							ALLS[case+(-x,1,x,-1)[e]].marqueur = True
							SCREEN.fill(0xf00000,ALLS[case+(-x,1,x,-1)[e]])
							SCREEN.blit(ALLS[case+(-x,1,x,-1)[e]].image,ALLS[case+(-x,1,x,-1)[e]])
					temp.remove(case)
			display.update()
	if event.poll().type == QUIT and TIME: event.clear();event.post(event.Event(QUIT));break
	if TIME:
		T = [i for i in ALLS if i.marqueur]
		score += len(T)/coup
	for i in sample(ALLS,len(ALLS)):
		SCREEN.fill(0,i)
		time.wait(10)
		display.update(i)
	SC = FONT.render('SCORE : '+str(score),1,(255,255,255))
	SCPOS = SC.get_rect()
	SCPOS.center = SRECT.center
	SCREEN.blit(SC,SCPOS)
	display.flip()
	if not TIME: break
	time.wait(1000)
	SCREEN.fill(0)
	display.flip()
while event.wait().type != QUIT: pass
