import pygame, sys,os
from pygame.locals import *

pygame.init()
if not pygame.font: 	print 'Warning, fonts disabled'
if not pygame.mixer: 	print 'Warning, sound disabled'

screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
pygame.display.set_caption('labirynth')
pygame.mouse.set_visible(1)
background = pygame.Surface(screen.get_size()).convert()
background.fill((255, 255, 255))

key_up = False
key_down = False
key_left = False
key_right = False

#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
#---------------------little routine for easy loading arting------------------------------------
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
def load_image(name, colorkey=None):
	#let try to load the file
    	try:
        	image = pygame.image.load((os.path.join('data', name))).convert()
    	except pygame.error, message:
    		raise SystemExit, message
	#then set the color key
    	if colorkey is not None:
		if colorkey is -1:
			colorkey = image.get_at((0,0))
        	image.set_colorkey(colorkey, RLEACCEL)
    	return image, image.get_rect()

def load_sound(name):
	#build dumb sound file for mute mode
	class NoneSound:
		def play(self): pass
	if not pygame.mixer:
		return NoneSound()
	#try to load sound file
	try:
		sound = pygame.mixer.Sound((os.path.join('data', name)))
	except pygame.error, message:
		raise SystemExit, message
	return sound
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
#-------------------------------------sprite object---------------------------------------------
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
class wall(pygame.sprite.Sprite):
	def __init__(self, xpos, ypos):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface([(20), (20)])
		self.image.fill((0, 0, 0))
		self.rect = self.image.get_rect().move(xpos,ypos)

	def update(self,sprites):
		if pygame.sprite.spritecollide (self, sprites, False):
			for sprite in sprites:
				sprite.collide()

	def draw(self):
		pass

class hero(pygame.sprite.Sprite):
	def __init__(self, xpos, ypos):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface([(20), (20)])
		self.image.fill((0, 0, 255))
		self.rect = self.image.get_rect().move(xpos,ypos)

	def update(self):
		self.xold, self.yold = self.rect.x, self.rect.y
		if key_up == True:		self.rect.y -= 1
		elif key_down == True:		self.rect.y += 1
		elif key_left == True:		self.rect.x -= 1
		elif key_right == True:		self.rect.x += 1
			
	def draw(self):
		pass

	def collide(self):
		self.rect.x = self.xold
		self.rect.y = self.yold
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
#-------------------------------------level building--------------------------------------------
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
level = (wall(0,0), wall(20,0), wall(40,0), wall(60,0), wall(120,0), wall(140,0), wall(160,0)\
	, wall(180,0), wall(0,20), wall(0,40), wall(0,60), wall(0,120), wall(0,140), wall(0,160)\
	, wall(0,180), wall(20,180), wall(40,180), wall(60,180), wall(120,180), wall(140,180)\
	, wall(160,180), wall(180,180), wall(180,20), wall(180,40), wall(180,60), wall(180,120)\
	, wall(180,140), wall(180,160), wall(180,180))

walldown = ( wall(80,180), wall(100,180))

wallup = ( wall(80,0), wall(100,0))

wallleft = ( wall(0,80), wall(0,100))

wallright = ( wall(180,80), wall(180,100))

#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
#-------------------------------------------game------------------------------------------------
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------

labyrinth = pygame.sprite.RenderUpdates(level + wallup + walldown + wallleft + wallright)
#labyrinth.add(level + doorup + doordown)
hero = hero(80,80)


while True :
	clock.tick(60)

	for event in pygame.event.get():
    		if event.type == QUIT:
        		sys.exit(0)
    		elif event.type == MOUSEBUTTONDOWN: pass
		elif event.type == MOUSEBUTTONUP: pass
		elif event.type == KEYDOWN: 
			if event.key == K_UP:		key_up = True
			if event.key == K_DOWN:		key_down = True
			if event.key == K_LEFT:		key_left = True
			if event.key == K_RIGHT:	key_right = True
		elif event.type == KEYUP:
			if event.key == K_UP:		key_up = False
			if event.key == K_DOWN:		key_down = False
			if event.key == K_LEFT:		key_left = False
			if event.key == K_RIGHT:	key_right = False

	screen.blit(background, (0, 0))

	labyrinth.update([hero])
	hero.update()
	labyrinth.draw(screen)
	screen.blit(hero.image, hero.rect)


	pygame.display.flip()
