import pygame, math, random
from pygame.locals import *

class mice(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.button = None
		self.position = None
		self.pressed = None
		self.raize = None
		#-----------	
		self.image = pygame.Surface((20,20))
		self.image.set_colorkey((0,0,0))
		self.rect = self.image.get_rect()
		pygame.draw.circle(self.image,(1,1,1),self.rect.center,8,2)
		
	def update(self):
		self.button = pygame.mouse.get_pressed()	
		self.position = pygame.mouse.get_pos()
		if True in self.button :
			if self.pressed != True:
				self.raize = True
			else:
				self.raize = False
			self.pressed = True
		else:
			self.pressed = False
			self.raize = False
		#------------------
		self.rect.center = self.position
		
	def draw(self):
		pass
			
class boulle(pygame.sprite.Sprite):
	def __init__(self,x,y,color,limit,grp):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface((30,30))
		self.rect = self.image.get_rect()
		pygame.draw.circle(self.image, color, self.rect.center, 14)
		self.image.set_colorkey((0,0,0))
		self.rect.center = (x,y)
		self.limit = limit
		self.move = (random.choice((1,-1)),random.choice((1,-1)))
		self.grp = grp

	def collide(self,other):
		(x,y) = self.move
		if other != self:
			if other.rect.collidepoint(self.rect.left,self.rect.centery):
				x = 1
			if other.rect.collidepoint(self.rect.right,self.rect.centery):
				x = -1
			if other.rect.collidepoint(self.rect.centerx,self.rect.top):
				y = 1
			if other.rect.collidepoint(self.rect.centerx,self.rect.bottom):
				y = -1
		self.move = (x,y)

	def update(self):

		if self.rect.right < self.limit.left:
			self.rect.left = self.limit.right 
		elif self.rect.left > self.limit.right:
			self.rect.right = self.limit.left
		elif self.rect.top > self.limit.bottom:
			self.rect.bottom = self.limit.top
		elif self.rect.bottom < self.limit.top:
			self.rect.top = self.limit.bottom
		else:
			(x,y) = self.move
			self.rect.move_ip(x,y)
		for other in pygame.sprite.spritecollide(self, self.grp, 0):
			self.collide(other)

#--------------------------------------------------------------
#--------------------------------------------------------------
#--------------------------------------------------------------
#--------------------------------------------------------------
#--------------------------------------------------------------
#--------------------------------------------------------------
pygame.init()
pygame.display.set_caption("Martin")
pygame.mouse.set_visible(1)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((300, 300))
offscr = screen.get_rect()
end = False
mice = mice()
#-------------------------------------------------
spiele = pygame.sprite.Group()

coal = (51,51,51)
green = (0,200,0)
red = (200,0,0)
blue = (0,0,200)
colors = (green,red,blue)

for i in range(10):
	spiele.add(boulle(random.randint(20,280),random.randint(20,280),random.choice(colors),offscr,spiele))


#-------------------------------------------------
while end == False :	
	clock.tick(30)
	pygame.display.flip()
	mice.update()
	for event in pygame.event.get():
    		if event.type == QUIT:
        			end = True
	#=======================================================
	screen.fill((240,240,240))
	screen.blit(mice.image,mice.rect)
	for it in pygame.sprite.spritecollide(mice,spiele,0):
		it.collide(mice)
	spiele.update()
	spiele.draw(screen)

	#======================================================
