# -*- coding: utf-8 -*-
"""
Created on Thu Apr 14 11:56:47 2016

@author: gtruch
"""
import pygame as pg
import math

# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)


class App(object):
    
    def __init__(self):
           
        pg.init()       # Initialize pygame

        # Initialize display surface
        self.screen_size = (800,800)
        self.screen = pg.display.set_mode((int(self.screen_size[0]),
                                           int(self.screen_size[1])))
                                           
        # Set up the timer.
        self.clock = pg.time.Clock()        
        self.ticks = pg.time.get_ticks()
        self.pixels_per_tick = 0.2
        
        # Initialize the lander image        
        self.lander = pg.image.load("lander_on.png")
        self.lx = 0
        self.ly = self.screen_size[0]/2   
        self.step = 0                     
                                                                                    
    def run(self):
        '''Main animation loop'''
    
        done = False
                                           
        while not done:
                        
            # Process events
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
                elif event.type == pg.KEYDOWN:                
                    if event.key == pg.K_ESCAPE:
                        done = True
                        
            self.advance()
                                    
        pg.quit()

    def advance(self):
        '''Advance the simulation one frame'''
            
        # Calculate step
        current_ticks = pg.time.get_ticks()
        self.step += (current_ticks - self.ticks)*self.pixels_per_tick
        self.ticks = current_ticks
        if self.step > 1: 

            # Advance the simulation
            self.lx += math.floor(self.step)
            if self.lx > self.screen_size[0]:
                self.lx = 0

            self.step -= math.floor(self.step)
        
                    
        # Update the display
        self.screen.fill(BLACK)
        self.screen.blit(self.lander,(self.lx,self.ly))
        
        pg.display.update()
    
if __name__=='__main__':
    app = App()
    app.run()
