#!/usr/bin/python
import pygame

ship = [                  (4,1),(5,1),
		          (4,2),
	            (3,3),(4,3),
	            (3,4),(4,4),
	      (2,5),(3,5),(4,5),
	(1,6),(2,6),(3,6),
	(1,7),(2,7),(3,7),
	(1,8),(2,8),(3,8),
	(1,9),(2,9),(3,9),(4,9),
	           (3,10),(4,10),(5,10) ]

core = [ (5,2),(6,2), (5,3),(6,3), (5,4),(6,4),      (5,9),(6,9) ]
cockpit = [   (4,6), (5,6),
              (4,7), (5,7),
              (4,8), (5,8) ]

	  
import colorsys
def normalize(color): return map(lambda x: x / 255.0, color)
def reformat(color): return map(lambda x: int(round(x * 255)), color)

def plot(surf, point, color):
	x, y = point
	surf.set_at(point, color)
	for pt in ((x+1,y),(x-1,y),(x,y+1),(x,y-1)):
		try:
			if surf.get_at(pt) == (255, 255, 255, 255):
				surf.set_at(pt, (0,0,0))
		except: pass

def get_rgb(x, y, code):
	colors = [ code & 255, code >> 8 & 255, code >> 16 & 255, code >> 24 & 255 ]
	if x < 7:
		value = 220 - (30 * (6 - x))
	else:
		value = 220 - (30 * (x - 7))
	if y < 6:
		hue = colors[0]
		saturation = 100 - (20 * abs(y - 2))
	elif y < 9:
		hue = colors[1]
		saturation = 120 - (20 * (8 - y))
	else:
		hue = colors[2]
		saturation = 100 - (20 * (y - 10))

	hsv = normalize((hue, saturation, value))
	return reformat(colorsys.hsv_to_rgb(*hsv))

def get_cockpit_rgb(x, y, hue):
	saturation = 100 - (20 * (7 - x))
	if y == 8:
		value = 230
	else:
		value = 255
	hsv = normalize((hue, saturation, value))
	return reformat(colorsys.hsv_to_rgb(*hsv))

def draw_ship(code):
	dood = pygame.Surface((12,12))
	dood.fill((255,255,255))
	for point in core: dood.set_at(point, (0,0,0))
	for bit in xrange(0,26):
		if code & 2**bit:
			x, y = ship[bit]
			color = get_rgb(x, y, code)
			plot(dood, (x,y),color)
			x = 11 - x
			color = get_rgb(x, y, code)
			plot(dood, (x,y),color)
	for bit in xrange(26,32):
		x,y = cockpit[bit - 26]
		if code & 2**bit:
			hue = code >> 24 & 255
			color = get_cockpit_rgb(x, y, hue)
			dood.set_at((x,y),color)
			x = 11 - x
			color = get_cockpit_rgb(x, y, hue)
			dood.set_at((x,y),color)
		else:
			dood.set_at((x,y),(0,0,0))
			dood.set_at((11 - x,y),(0,0,0))
	return dood

def bigship(code, size=(144,144)):
	dood = draw_ship(code)
	scaled = pygame.transform.scale(dood, size)
	return scaled


def main(code):
	pygame.init()
	screen = pygame.display.set_mode((144, 144))
	screen.fill((255,255,255))
	pygame.display.set_caption('Pixel Spaceships')
	print "Spaceship Code: 0x%08x" % code

	clock = pygame.time.Clock()
	spriteship = bigship(code)
	screen.blit(spriteship, spriteship.get_rect())
	while 1:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				return
			elif event.type == pygame.MOUSEBUTTONDOWN:
				code = randint(0, 2**32)
				print "Spaceship Code: 0x%08x" % code
				spriteship = bigship(code)
				screen.blit(spriteship, spriteship.get_rect())
		clock.tick(3)
		pygame.display.update()

if __name__ == '__main__': 
	from sys import argv
	from random import randint
	if len(argv) > 2:
		main(*map(int,argv[1:4]))
	elif len(argv) == 2:
		num=argv[1]
		main(int(num[2:],16))
	else:
		main(randint(0, 2**32))
