
# ============== pygame visualiser

import pygame
class Visualiser(object):
    def __init__(visualiser, size):
        pygame.init()

        screen = pygame.display.set_mode(size)
        surface = pygame.Surface(size).convert()
        satellite = pygame.Surface((3, 3)).convert()
        fire = pygame.Surface((3, 3)).convert()
        smoke = pygame.Surface((3, 3)).convert()
        trail = [None for i in xrange(10)]

        visualiser.__dict__.update(
            size = size,
            screen = screen,
            surface = surface,
            satellite = satellite,
            fire = fire,
            smoke = smoke,
            trail = trail,
        )

        pygame.draw.circle(visualiser.screen, (0,0,255), (250, 250), 160)
        
        satellite.fill((255, 0, 0))
        fire.fill((250, 150, 0))
        smoke.fill((50, 50, 0))

        def visualise(t, score, fuel_remaining, x, y, target_orbit_radius):
            if t % 10:
                return
            
            thrust = trail[-1]
            if thrust:
                screen.blit(fire, thrust)

            end_of_trail = trail.pop(0)
            if end_of_trail:
                screen.blit(smoke, end_of_trail)
                
            pos = (250 + (x/40000), (y/40000) + 250)
            trail.append(pos)
            
            screen.blit(satellite, pos)
            pygame.display.flip()
        
        visualiser.visualise = visualise
        
    def log(visualiser, t, score, fuel_remaining, x, y, target_orbit_radius):
        print "%s : [ Fuel: %s | X: %s | Y: %s | target = %s ]" % ( t, fuel_remaining, x, y, target_orbit_radius )        
        
