import pygame
import time
import random
import sys

pygame.init()

display_width = 800
display_height = 600
screen = pygame.display.set_mode((800, 600))
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
light_red = (255,0,0)                
green =(34,177,76)
light_green = (0,255,0)
block_color = (53,115,255)
blue =(53, 115, 255)
car_width = 73

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('CAR')
clock = pygame.time.Clock()

carImg = pygame.image.load('racecar.png')
tree = pygame.image.load('tree.png')
treeThingImg = pygame.image.load('ham.png')
tr = pygame.image.load('t.png')
map1= pygame.image.load('bg.png')
bg= pygame.image.load('bgm.png')
def things_dodged(count):
    font = pygame.font.SysFont(None, 50)
    text = font.render("Source: "+str(count), True, black)
    gameDisplay.blit(text,(0,0))

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
    

def car(x,y):
    gameDisplay.blit(carImg,(x,y))
    
    
def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font(None,115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()
    
    
                 
def crash():
    message_display('YOU LOSE')

    
def car_thing(x, y):
    gameDisplay.blit(tree, (x, y))
    
def tree_thing(x, y):
    gameDisplay.blit(treeThingImg, (x, y))

def tre(x, y):
    gameDisplay.blit(tr, (x, y))

def line(lineX, lineY, lineW, lineH, color):
    """draw way lines """ 
    pygame.draw.rect(gameDisplay, color, [lineX,lineY, lineW,lineH])
    
def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)
    pygame.mixer.music.load("play.wav")
    pygame.mixer.music.play(-1)
    x_change = 0
    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_startx2 = random.randrange(0, display_width-100)
    thing_starty2 = 200
    thing_speed = 4
    thing_width = 80
    thing_height = 80
    thing_width2 = 60
    thing_height2 = 60

    lineX = 400
    lineY = 0
    lineW = 10 
    lineH = 450

    tree_y_right = 600
    tree_y_left = 300
    tree_h = 600 

    dodged = 0

    gameExit = False
    

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        gameDisplay.blit(map1,(0,0))

        

        line(240, 0, 20, display_height, white)
        line(display_width-250, 0, 20, display_height, white)

     
        line(lineX, lineY, lineW, lineH, white)    
        lineY += thing_speed
        tre (80, tree_y_left)
        tre(700, tree_y_right)
        car_thing(thing_startx, thing_starty)
        tree_y_left += thing_speed
        tree_y_right += thing_speed
        tree_thing (thing_startx2, thing_starty2)
        thing_starty += thing_speed
        thing_starty2 += thing_speed
        car(x,y)
        things_dodged(dodged)
        if lineY > display_height  :
            

            lineY = 0 - lineH # reset y

        if tree_y_left > display_height  :
   

            tree_y_left = 0 - tree_h # reset y 

        if tree_y_right > display_height  :


            tree_y_right = 0 - tree_h # reset y 
        
        if x > display_width - car_width or x < 0:
            crash()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            thing_speed += 0.2
            
        if thing_starty2 > display_height:
            thing_starty2 = 0 - thing_height2
            thing_startx2 = random.randrange(0,display_width)
            thing_speed += 0.1
            
        if y < thing_starty+thing_height:

            if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
                crash()
                
        if y < thing_starty2+thing_height2:

            if x > thing_startx2 and x < thing_startx2 + thing_width2 or x+car_width > thing_startx2 and x + car_width < thing_startx2+thing_width2:
                  dodged += 10
                  thing_speed+=0.1
                  thing_starty2=-1000
                    
        pygame.display.update()
        clock.tick(60)

class Menu:

   
    def __init__(self, *options):
        self.options = options
        self.x = 0
        self.y = 0
        self.font = pygame.font.Font(None, 32)
        self.option = 0 
        self.width = 1
        self.color = [0, 0, 0]
        self.hcolor = [0, 0, 0] #Highlight Color
        self.height = len(options)*self.font.get_height()
        for i in options: 
            text = i[0] 
            ren = self.font.render(text, 1, (0, 0, 0))
            if ren.get_width() > self.width:
                self.width = ren.get_width() 


    def draw(self, surface):
        i = 0
        for j in self.options:
            if i == self.option: 
                col = self.hcolor 
            else:
                col = self.color
            text = j[0]
            ren = self.font.render(text, 1, col)
            if ren.get_width() > self.width:
                self.width = ren.get_width()
            surface.blit(ren, (self.x, self.y + i * self.font.get_height())) 
            i += 1

  
    def update(self, events): 
        for e in events:
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_UP:
                    self.option -= 1
                elif e.key == pygame.K_DOWN:
                    self.option += 1
                elif e.key == pygame.K_RETURN:
                    self.options[self.option][1]()
            if self.option > len(self.options) - 1:
                self.option = 0
            elif self.option < 0:
                self.option = len(self.options) - 1

   
    def set_pos(self, x, y):
        self.x = x
        self.y = y

    
    def set_font(self, font):
        self.font = font
        for o in self.options:
            text = o[0]
            ren = self.font.render(text, 1, (0, 0, 0))
            if ren.get_width() > self.width:
                self.width = ren.get_width()


    def set_highlight_color(self, color):
        self.hcolor = color


    def set_normal_color(self, color):
        self.color = color

 
    def center_at(self, x, y):
        self.x = x-(self.width/2)
        self.y = y-(self.height/2)

def option1():
    game_loop()
def option3():
    pygame.quit()
    sys.exit()
       
def main():
    menuTitle = Menu(
        ["SpongeBob"])

    menu = Menu(
        ["Start", option1],
        ["Exit", option3])
    pygame.mixer.music.load("h.wav")
    pygame.mixer.music.play(-1)
 
    menuTitle.set_font(pygame.font.Font(None, 60))
    menuTitle.center_at(400, 150)
    menuTitle.set_highlight_color((0,0,0))
    menuTitle.set_normal_color((0,0,0))

 
    menu.set_font(pygame.font.Font(None, 40))
    menu.center_at(400, 350)
    menu.set_highlight_color((255,255,255))
    menu.set_normal_color((0,0,0))

    clock = pygame.time.Clock()
    keepGoing = True

    while True:
        clock.tick(30)

        #Events
        events = pygame.event.get()

        #Update Menu
        menu.update(events)

        #quit event
        for e in events:
            if e.type == pygame.QUIT:
                pygame.quit()
                return

        #Draw
        gameDisplay.blit(bg,(0,0))
        menu.draw(screen)
        menuTitle.draw(screen)

        pygame.display.flip() #update        
main()
pygame.quit()
quit()
