import pygame
import main					#able to load the lib
main.init()					#
import data, color				#then load the libs
from color import *

class pot_red(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.color = red
		self.image = data.load_img('RED.png')
		self.rect = self.image.get_rect()
		self.image.convert()
		self.rect = self.rect.move(220,400)
		self.image.set_colorkey(white)
		self.here = (252,340)

	def update(self):
		pass

	def draw(self,screen):
		screen.blit(self.image,self.rect)

class pot_blue(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.color = blue
		self.image = data.load_img('BLUE.png')
		self.rect = self.image.get_rect()
		self.image.convert()
		self.rect = self.rect.move(287,400)
		self.image.set_colorkey(white)
		self.here = (319,340)

	def update(self):
		pass

	def draw(self,screen):
		screen.blit(self.image,self.rect)

class pot_yellow(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.color = yellow
		self.image = data.load_img('YELLOW.png')
		self.rect = self.image.get_rect()
		self.image.convert()
		self.rect = self.rect.move(352,400)
		self.image.set_colorkey(white)
		self.here = (382,340)

	def update(self):
		pass

	def draw(self,screen):
		screen.blit(self.image,self.rect)

if __name__=="__main__":
	#tester
	pygame.init()					#first
	screen = pygame.display.set_mode((640,480))	#the screen
	clock = pygame.time.Clock()			#the time
	#get the lib

	#accelerator
	exit_button = pygame.QUIT
	pygame.event.set_blocked(None)
	pygame.event.set_allowed(exit_button)
	happen = pygame.event.peek
	fps = clock.tick
	flip = pygame.display.flip
	cls = screen.fill
	
	#the pot
	pots = [pot_red(),pot_blue(),pot_yellow()]

	#the robo
	#import robo
	#robo.init()
	#robot = robo.robo((100,40,540,440))

	#the spring
	import spring
	spring.init()
	springs = pygame.sprite.Group(spring.spring(320,240,grey))
	springs.add(spring.spring(230,340,grey))

	#cube
	import grapping_cube
	grapping_cube.init()
	cube = grapping_cube.grappling_cube()
	taken = None

	#main loop
	end = False
	raize = False
	button = False
	while not end:
		fps(40)
		flip()
		cls(white)
		if happen(exit_button):
			end = True
		else:
			pygame.event.clear()
		for pot in pots:
			pot.draw(screen)
		#robot.update()
		#robot.draw(screen)
		springs.draw(screen)
		springs.update()
		cube.update()
		cube.draw(screen)
		if True in pygame.mouse.get_pressed():
			if not button:
				raize = True
				button = True
			else:
				raize = False
		else:
			raize = False
			button = False

		if raize :
			(x,y) = pygame.mouse.get_pos()
			if not taken and not cube.spr:
				for sp in springs:
					if sp.rect.collidepoint(x,y):
						cube.take(sp)
						taken = sp
			elif taken and cube.destx == 0 and cube.desty == 0:
				for pot in pots:
					if pot.rect.collidepoint(x,y):
						cube.go((x,y))
						taken = None
		if not cube.spr:
			for pot in pots:
				for sp in pygame.sprite.spritecollide(pot, springs,0):
					#if type(sp) == spring.spring:
					springs.add(spring.spring(pot.rect.centerx,pot.rect.top - 20,pot.color))
					springs.remove(sp) 
