import pygame
import sys
import pygame.locals
import pygame.mixer
import random
atime = 0.0
title = pygame.image.load("data/title.jpg")
lose = pygame.image.load("data/lose.jpg")
class intro:
    
    def run(self,time):
        screen.blit(pygame.image.load("data\slide1.jpg"),[0,0])
        pygame.display.update()
        ltime = pygame.time.get_ticks()
        while pygame.time.get_ticks() - ltime <= time * 1000:
            pass
        screen.blit(pygame.image.load("data\slide2.jpg"),[0,0])
        pygame.display.update()
        ltime = pygame.time.get_ticks()
        while pygame.time.get_ticks() - ltime <= time * 1000:
            pass
        screen.blit(pygame.image.load("data\slide3.jpg"),[0,0])
        pygame.display.update()
        ltime = pygame.time.get_ticks()
        while pygame.time.get_ticks() - ltime <= time * 1000:
            pass
        screen.blit(pygame.image.load("data\slide4.jpg"),[0,0])
        pygame.display.update()
        ltime = pygame.time.get_ticks()
        while pygame.time.get_ticks() - ltime <= time * 2000:
            pass
        screen.blit(pygame.image.load("data\slide5.jpg"),[0,0])
        pygame.display.update()
        ltime = pygame.time.get_ticks()
        while pygame.time.get_ticks() - ltime <= time * 1000:
            pass
class mainmenu:
    def __init__(self):
        self.list = [["Play Game",[270,360]],["Exit",[300,450]]]
        self.current = 0
    def run(self):
        screen.blit(title,[0,0])
        pygame.event.poll()
        
        screen.blit(menut.render("Play Game",1,[255,255,255]),[270,360])
        screen.blit(menut.render("Exit",1,[255,255,255]),[300,450])
        screen.blit(menut.render(self.list[self.current][0],1,[255,255,255],[240,240,10]),self.list[self.current][1])
        if pygame.key.get_pressed()[pygame.locals.K_UP] == 1 or pygame.key.get_pressed()[pygame.locals.K_DOWN] == 1:
            

            while pygame.key.get_pressed()[pygame.locals.K_UP] == 1 or pygame.key.get_pressed()[pygame.locals.K_DOWN] == 1:
                pygame.event.poll()
            if self.current == 1:
                self.current = 0
            else:
                self.current = self.current + 1
        if pygame.key.get_pressed()[pygame.locals.K_RETURN] == 1 or pygame.key.get_pressed()[pygame.locals.K_SPACE] == 1:
            if self.current == 0:
                atime = pygame.time.get_ticks()
                a.reset(player())
                music.stop()
                musicb.play(-1)
                
            if self.current == 1:
                sys.quit()
        pygame.display.update()
        
        
        
pygame.init()
screen = pygame.display.set_mode([700,700])

music = pygame.mixer.Sound("data\song1b.wav")
musicb = pygame.mixer.Sound("data\songmiddle.wav")
hpbar = pygame.image.load("data\hpbar.png")
walli = pygame.image.load("data\wall.png")
hp100 = pygame.image.load("data\l100%.png")
hp90 = pygame.image.load("data\l90%.png")
hp80 = pygame.image.load("data\l80%.png")
hp70 = pygame.image.load("data\l70%.png")
hp60 = pygame.image.load("data\l60%.png")
hp50 = pygame.image.load("data\l50%.png")
hp40 = pygame.image.load("data\l40%.png")
hp30 = pygame.image.load("data\l30%.png")
hp20 = pygame.image.load("data\l20%.png")
hp10 = pygame.image.load("data\l10%.png")

hpbar.convert()
walli.convert()
hp100.convert()
hp90.convert()
hp80.convert()
hp70.convert()
hp60.convert()
hp50.convert()
hp40.convert()
hp30.convert()
hp20.convert()
hp10.convert()


hp100.set_colorkey([255,255,255])
hp90.set_colorkey([255,255,255])
hp80.set_colorkey([255,255,255])
hp70.set_colorkey([255,255,255])
hp60.set_colorkey([255,255,255])
hp50.set_colorkey([255,255,255])
hp40.set_colorkey([255,255,255])
hp30.set_colorkey([255,255,255])
hp20.set_colorkey([255,255,255])
hp10.set_colorkey([255,255,255])
walli.set_colorkey([255,255,255])
menu = pygame.image.load("data\menu.png")
hppi = pygame.image.load("data\hpp.png")                         
menu.convert()
hppi.convert()


hpi = hp100
hppi.set_colorkey([255,255,255])
hp100w = pygame.image.load("data\l100%.png")
hp90w = pygame.image.load("data\l90%.png")
hp80w = pygame.image.load("data\l80%.png")
hp70w = pygame.image.load("data\l70%.png")
hp60w = pygame.image.load("data\l60%.png")
hp50w = pygame.image.load("data\l50%.png")
hp40w = pygame.image.load("data\l40%.png")
hp30w = pygame.image.load("data\l30%.png")
hp20w = pygame.image.load("data\l20%.png")
hp10w = pygame.image.load("data\l10%.png")
clear = pygame.image.load("data\clear.png")
background = pygame.image.load("data\cbackground.jpg")

hp100w.convert()
hp90w.convert()
hp80w.convert()
hp70w.convert()
hp60w.convert()
hp50w.convert()
hp40w.convert()
hp30w.convert()
hp20w.convert()
hp10w.convert()

x = 0
y = 0
class engine:
    def __init__(self,player):
        self.player = player
        self.loo = []
        self.level = 1.0
        self.playing = 1
        self.dogged = 0.0
        self.pu = []
        self.hpp = []
        self.fps = 0.0
        self.ltime=0.0
        self.power = 0.0
        self.background_pos = 0.0
    def reset(self,player):
        self.player = player
        self.loo = []
        self.level = 1.0
        self.playing = 1
        self.dogged = 0.0
        self.pu = []
        self.hpp = []
        self.fps = 0.0
        self.ltime=0.0
        self.power = 0.0
        self.background_pos = 0.0
    def add(self,y):
        self.loo.append(walli.get_rect().move(700,y))
    def addhp(self,y):
        self.hpp.append(hppi.get_rect().move(700,y))
    def render(self):
        timer.tick(30)
        self.background_pos = self.background_pos + self.level * 2
        screen.fill([0,0,0])

            
        if self.background_pos >= 4000:
            self.background_pos = 0
            self.level = self.level + 1 

        self.fps=self.fps+1
            
        if pygame.time.get_ticks()-self.ltime > 1000:
            self.ltime=pygame.time.get_ticks()
            self.power = 20.0/self.fps
            #print self.fps

            self.fps=0
        if self.player.hp >= 90:
            hpi = hp100w            
        if self.player.hp <= 90:
            hpi = hp90w
        if self.player.hp <= 80:
            hpi = hp80w
        if self.player.hp <= 70:
            hpi = hp70w
        if self.player.hp <= 60:
            hpi = hp60w
        if self.player.hp <= 50:
            hpi = hp50w
        if self.player.hp <= 40:
            hpi = hp40w
        if self.player.hp <= 30:
            hpi = hp30w
        if self.player.hp <= 20:
            hpi = hp20w
        if self.player.hp <= 10:
            hpi = hp10w
        screen.blit(hpi,[self.player.rect[0] - 350,self.player.rect[1] - 300,700,600])
        if random.randint(0,100) > (100 - self.level * 5):
            self.add(random.randint(0,536))
        if random.random() > .999:
            self.addhp(random.randint(0,558))
        if self.background_pos >= 3300:

            screen.blit(background,[3300 - self.background_pos ,0,700,600],[3300,0,700,600])

            screen.blit(background,[4000 - self.background_pos,0,700,600],[0 ,0,700,600])
                
        else:
            screen.blit(background,[0,0,700,600],[self.background_pos,0,700,600])

        self.player.input()

        if len(self.loo) != 0:
            for wall in self.loo:
                wall[0] = wall[0] - self.level * 2
                x = wall[0]
                x = x * self.power
                 

                if wall[0] <= 0:
                    self.loo.pop(self.loo.index(wall))
                    self.dogged = self.dogged + 1
                    
                else:
                    screen.blit(walli,wall)
        if len(self.hpp) != 0:
            for hpp in self.hpp:
                hpp[0] = hpp[0] - self.level * 2

                if hpp[0] <= 0:
                    self.hpp.pop(self.hpp.index(hpp))
                else:
                    screen.blit(hppi,hpp)
                    

        if self.player.rect.collidelist(self.hpp) != -1:
            self.player.hp = self.player.hp + 10
            self.hpp.pop(self.player.rect.collidelist(self.hpp))
        if self.player.rect.collidelist(self.loo) != -1:
            self.player.rect[0] = self.player.rect[0] - 10
            self.player.hp = self.player.hp - 2
            if self.player.rect[0] < 0:
                screen.blit(lose,[0,0])
                pygame.display.update()
                musicb.stop()
                music.play(-1)
                event = 1
                while event ==1:
                    pygame.event.poll()
                    for thing in pygame.key.get_pressed():
                        if thing == 1:
                            event = 0
                            break
                music.play(-1)
                self.playing = 0
            
        self.player.draw()      

        if self.player.hp >= 90:
            hpi = hp100
        if self.player.hp <= 90:
            hpi = hp90
        if self.player.hp <= 80:
            hpi = hp80
        if self.player.hp <= 70:
            hpi = hp70
        if self.player.hp <= 60:
            hpi = hp60
             
        if self.player.hp <= 50:
            hpi = hp50
        if self.player.hp <= 40:
            hpi = hp40
        if self.player.hp <= 30:
            hpi = hp30
        if self.player.hp <= 20:
            hpi = hp20
        if self.player.hp <= 10:
            hpi = hp10
        if self.player.hp <= 0:
            screen.blit(lose,[0,0])
            pygame.display.update()
            event = 1
            musicb.stop()
            while event ==1:
                pygame.event.poll()
                for thing in pygame.key.get_pressed():
                    if thing == 1:
                        event = 0
                        break
            self.playing = 0        
        screen.blit(hpi,[self.player.rect[0] - 350,self.player.rect[1] - 300,700,600])
        screen.blit(clear,[self.player.rect[0] - 350 + 700,self.player.rect[1] - 300,700,600])
        screen.blit(clear,[self.player.rect[0] - 350 - 700,self.player.rect[1] - 300,700,600])
        screen.blit(clear,[self.player.rect[0] - 350,self.player.rect[1] - 300 + 600,700,600])
        screen.blit(clear,[self.player.rect[0] - 350,self.player.rect[1] - 300 - 600,700,600])
        screen.blit(clear,[self.player.rect[0] - 350 - 700,self.player.rect[1] - 300 + 600,700,600])
        screen.blit(clear,[self.player.rect[0] - 350 + 700,self.player.rect[1] - 300 + 600,700,600])
        screen.blit(clear,[self.player.rect[0] - 350 - 700,self.player.rect[1] - 300 - 600,700,600])
        screen.blit(clear,[self.player.rect[0] - 350 + 700,self.player.rect[1] - 300 - 600,700,600])
        screen.blit(menu,[0,600,700,100])
        screen.blit(hpbar,[14 + self.player.hp,655,100,30],[self.player.hp - 100,0,100,30])

        screen.blit(menut.render(str(self.level),1,[255,0,0]),[320,652])
        screen.blit(menut.render(str((pygame.time.get_ticks() - atime) / 1000),1,[255,0,0]),[567,652])
        
        pygame.display.update()



class player:
    def __init__(self):
        self.image = pygame.image.load("data\player.png")
        self.rect = self.image.get_rect()
        self.rect = self.rect.move(200,200)
        self.hp = 100
        self.image.set_colorkey(self.image.get_at([0,0]))
        
    def draw(self):
        screen.blit(self.image,self.rect)
    
        
    def input(self):
        pygame.event.poll()


        if pygame.key.get_pressed()[pygame.locals.K_UP] == 1 and self.rect[1] > 0:
            self.rect = self.rect.move(0,-2)
        if pygame.key.get_pressed()[pygame.locals.K_RIGHT] == 1 and self.rect[0] < 668:
            self.rect = self.rect.move(2,0)
        if pygame.key.get_pressed()[pygame.locals.K_LEFT] == 1 and self.rect[0] > 0:
            self.rect = self.rect.move(-2,0)
        if pygame.key.get_pressed()[pygame.locals.K_DOWN] == 1 and self.rect[1] < 568:
            self.rect = self.rect.move(0,2)
        

            
pygame.init()
timer = pygame.time.Clock()
menut = pygame.font.Font(None,40)

x = 0
a = engine(player())
a.add(100)
a.bakcground_pos = 3300
intro().run(2)
music.play(-1)
a.playing = 0
b = mainmenu()
while 1 == 1:
    if a.playing == 0:
        b.run()
    else:
        if pygame.key.get_pressed()[pygame.locals.K_q]:
            sys.quit()
            print 'Good bye'
        a.render()
    

