import pygame
import time
import random


pygame.init()
crash_sound = pygame.mixer.Sound("Sound Tracks/crash.wav")
m_sound = pygame.mixer.music.load("Sound Tracks/menusong.wav")
gray=(127,127,127)
black=(0,0,0)
red=(199, 38, 38)
green=(0,200,0)
blue=(52, 147, 201)
bright_red=(255,0,0)
bright_green=(0,255,0)
bright_blue=(0,0,255)
display_width=800
display_height=600
pause=False
gamedisplays=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("TraffIc RIder")
clock=pygame.time.Clock()
carimg=pygame.image.load('Vehicles/car1.jpg')
backgroundpic=pygame.image.load("Background images/download12.jpg")
yellow_strip=pygame.image.load("Background images/yellow strip.jpg")
strip=pygame.image.load("Background images/strip.jpg")
intro_background=pygame.image.load("Background images/intro_background.jpg")
instruction_background=pygame.image.load("Background images/instruction_background.jpg")
car_width=56


def intro_loop():
    intro=True
    pygame.mixer.music.load("Sound Tracks/menusong.wav")
    pygame.mixer.music.play(-1)
    while intro:
        for event in pygame.event.get():
           if event.type==pygame.QUIT:
               pygame.quit()
               quit()
               sys.exit()
        gamedisplays.blit(intro_background,(0,0))
        largetext=pygame.font.SysFont('agencyfb',115)
        textsurf,textrect=text_objects("TraffIC RIder",largetext)
        textrect.center=(400,109)
        gamedisplays.blit(textsurf,textrect)
        button("START",150,520,100,50,green,bright_green,"play")
        button("QUIT",550,520,100,50,red,bright_red,"quit")
        button("INSTRUCTION",300,520,200,50,blue,bright_blue,"intro")
        pygame.display.update()
        clock.tick(50)

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if x+w>mouse[0]>x and y+h>mouse[1]>y:
        pygame.draw.rect(gamedisplays,ac,(x,y,w,h))
        if click[0]==1 and action!=None:
            if action=="play":
                countdown()
            elif action=="quit":
                pygame.quit()
                quit()
                sys.exit()
            elif action=="intro":
                introduction()
            elif action=="menu":
                intro_loop()
            elif action=="pause":
                paused()
            elif action=="unpause":
                unpaused()

    else:
            pygame.draw.rect(gamedisplays,ic,(x,y,w,h))
    smalltext=pygame.font.Font("freesansbold.ttf",20)
    textsurf,textrect=text_objects(msg,smalltext)
    textrect.center=((x+(w/2)),(y+(h/2)))
    gamedisplays.blit(textsurf,textrect)

def introduction():
    introduction=True
    while introduction:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
                sys.exit()
        gamedisplays.blit(instruction_background,(0,0))
        largetext=pygame.font.Font('freesansbold.ttf',80)
        smalltext=pygame.font.Font('freesansbold.ttf',20)
        mediumtext=pygame.font.Font('freesansbold.ttf',40)
        textSurf,textRect=text_objects("This is a car game in which you need to dodge the coming cars",smalltext)
        textRect.center=((350),(200))
        TextSurf,TextRect=text_objects("INSTRUCTION",largetext)
        TextRect.center=((400),(100))
        gamedisplays.blit(TextSurf,TextRect)
        gamedisplays.blit(textSurf,textRect)
        stextSurf,stextRect=text_objects("ARROW LEFT : LEFT TURN",smalltext)
        stextRect.center=((150),(400))
        hTextSurf,hTextRect=text_objects("ARROW RIGHT : RIGHT TURN" ,smalltext)
        hTextRect.center=((150),(450))
        atextSurf,atextRect=text_objects("ARROW UP : ACCELERATOR",smalltext)
        atextRect.center=((150),(500))
        rtextSurf,rtextRect=text_objects("ARROW DOWN : BRAKE ",smalltext)
        rtextRect.center=((150),(550))
        ptextSurf,ptextRect=text_objects("P : PAUSE  ",smalltext)
        ptextRect.center=((150),(350))
        sTextSurf,sTextRect=text_objects("CONTROLS",mediumtext)
        sTextRect.center=((350),(300))
        gamedisplays.blit(sTextSurf,sTextRect)
        gamedisplays.blit(stextSurf,stextRect)
        gamedisplays.blit(hTextSurf,hTextRect)
        gamedisplays.blit(atextSurf,atextRect)
        gamedisplays.blit(rtextSurf,rtextRect)
        gamedisplays.blit(ptextSurf,ptextRect)
        button("BACK",600,450,100,50,blue,bright_blue,"menu")
        pygame.display.update()
        clock.tick(30)
def paused():
    pygame.mixer.music.pause()
    pygame.mixer.music.load("Sound Tracks/menusong.wav")
    pygame.mixer.music.play(-1)
    global pause
    pause=True
    while pause:
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    pygame.quit()
                    quit()
                    sys.exit()
            gamedisplays.blit(instruction_background,(0,0))
            largetext=pygame.font.Font('freesansbold.ttf',115)
            TextSurf,TextRect=text_objects("PAUSED",largetext)
            TextRect.center=((display_width/2),(display_height/2))
            gamedisplays.blit(TextSurf,TextRect)
            button("CONTINUE",150,450,150,50,green,bright_green,"unpause")
            button("RESTART",350,450,150,50,blue,bright_blue,"play")
            button("MAIN MENU",550,450,200,50,red,bright_red,"menu")
            pygame.display.update()
            clock.tick(30)

def unpaused():
    global pause
    pygame.mixer.music.pause()
    pygame.mixer.music.load("Sound Tracks/backgroundmusic.wav")
    pygame.mixer.music.play(-1)

    pause=False

def countdown_background():
    font=pygame.font.SysFont(None,25)
    x=(display_width*0.45)
    y=(display_height*0.8)
    gamedisplays.blit(backgroundpic,(0,0))
    gamedisplays.blit(backgroundpic,(0,200))
    gamedisplays.blit(backgroundpic,(0,400))
    gamedisplays.blit(backgroundpic,(700,0))
    gamedisplays.blit(backgroundpic,(700,200))
    gamedisplays.blit(backgroundpic,(700,400))
    gamedisplays.blit(yellow_strip,(400,100))
    gamedisplays.blit(yellow_strip,(400,200))
    gamedisplays.blit(yellow_strip,(400,300))
    gamedisplays.blit(yellow_strip,(400,400))
    gamedisplays.blit(yellow_strip,(400,100))
    gamedisplays.blit(yellow_strip,(400,500))
    gamedisplays.blit(yellow_strip,(400,0))
    gamedisplays.blit(yellow_strip,(400,600))
    gamedisplays.blit(strip,(120,200))
    gamedisplays.blit(strip,(120,0))
    gamedisplays.blit(strip,(120,100))
    gamedisplays.blit(strip,(680,100))
    gamedisplays.blit(strip,(680,0))
    gamedisplays.blit(strip,(680,200))
    gamedisplays.blit(carimg,(x,y))
    text=font.render("VEHICLE DODGED: 0",True, (0, 0, 255))
    score=font.render("SCORE: 0",True,(255, 0, 0))
    level=font.render("LEVEL: 0",True,(0,255, 0))
    gamedisplays.blit(level,(0,50))
    gamedisplays.blit(text,(0,30))
    gamedisplays.blit(score,(0,10))
    button("PAUSE",650,0,150,50,blue,bright_blue,"pause")

def countdown():
    countdown=True

    while countdown:
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    pygame.quit()
                    quit()
                    sys.exit()
            gamedisplays.fill(gray)
            countdown_background()
            largetext=pygame.font.Font('freesansbold.ttf',115)
            TextSurf,TextRect=text_objects("3",largetext)
            TextRect.center=((display_width/2),(display_height/2))
            gamedisplays.blit(TextSurf,TextRect)
            pygame.display.update()
            clock.tick(1)
            gamedisplays.fill(gray)
            countdown_background()
            largetext=pygame.font.Font('freesansbold.ttf',115)
            TextSurf,TextRect=text_objects("2",largetext)
            TextRect.center=((display_width/2),(display_height/2))
            gamedisplays.blit(TextSurf,TextRect)
            pygame.display.update()
            clock.tick(1)
            gamedisplays.fill(gray)
            countdown_background()
            largetext=pygame.font.Font('freesansbold.ttf',115)
            TextSurf,TextRect=text_objects("1",largetext)
            TextRect.center=((display_width/2),(display_height/2))
            gamedisplays.blit(TextSurf,TextRect)
            pygame.display.update()
            clock.tick(1)
            gamedisplays.fill(gray)
            countdown_background()
            largetext=pygame.font.Font('freesansbold.ttf',115)
            TextSurf,TextRect=text_objects("GO!!!",largetext)
            TextRect.center=((display_width/2),(display_height/2))
            gamedisplays.blit(TextSurf,TextRect)
            pygame.display.update()
            clock.tick(1)
            game_loop()


def obstacle(obs_startx,obs_starty,obs):
    if obs==0:
        obs_pic=pygame.image.load("Vehicles/car.jpg")
    elif obs==1:
        obs_pic=pygame.image.load("Vehicles/car1.jpg")

    elif obs==2:
        obs_pic=pygame.image.load("Vehicles/car2.jpg")

    elif obs==3:
        obs_pic=pygame.image.load("Vehicles/car4.jpg")

    elif obs==4:
        obs_pic=pygame.image.load("Vehicles/car5.jpg")

    elif obs==5:
        obs_pic=pygame.image.load("Vehicles/car6.jpg")

    elif obs==6:
        obs_pic=pygame.image.load("Vehicles/car7.jpg")

    elif obs==7:
        obs_pic=pygame.image.load("Vehicles/car8.jpg")

    elif obs==8:
        obs_pic=pygame.image.load("Vehicles/bike1.jpg")

    elif obs==9:
        obs_pic=pygame.image.load("Vehicles/bike2.jpg")

    elif obs==10:
        obs_pic=pygame.image.load("Vehicles/bike3.jpg")

    elif obs==11:
        obs_pic=pygame.image.load("Vehicles/car9.jpg")

    elif obs==12:
        obs_pic=pygame.image.load("Vehicles/car10.jpg")

    elif obs==13:
        obs_pic=pygame.image.load("Vehicles/car11.jpg")

    gamedisplays.blit(obs_pic,(obs_startx,obs_starty))

def score_system(passed,score,level):
    font=pygame.font.SysFont(None,25)
    text=font.render("VEHICLE DODGED :"+str(passed),True,(0, 0, 255))
    score=font.render("SCORE :"+str(score),True,(255 , 0, 0))
    level=font.render("LEVEL :"+str(level),True,(0,255, 0))
    gamedisplays.blit(text,(0,30))
    gamedisplays.blit(score,(0,10))
    gamedisplays.blit(level,(0,50))
def text_objects(text,font):
    textsurface=font.render(text,True,black)
    return textsurface,textsurface.get_rect()

def message_display(text):
    largetext=pygame.font.Font("freesansbold.ttf",80)
    textsurf,textrect=text_objects(text,largetext)
    textrect.center=((display_width/2),(display_height/2))
    gamedisplays.blit(textsurf,textrect)
    pygame.display.update()
    time.sleep(3)
    game_loop()
def crash():
    pygame.mixer.Sound.play(crash_sound)
    pygame.mixer.music.stop()
    message_display("YOU CRASHED")

def background():
    gamedisplays.blit(backgroundpic,(0,0))
    gamedisplays.blit(backgroundpic,(0,200))
    gamedisplays.blit(backgroundpic,(0,400))
    gamedisplays.blit(backgroundpic,(700,0))
    gamedisplays.blit(backgroundpic,(700,200))
    gamedisplays.blit(backgroundpic,(700,400))
    gamedisplays.blit(yellow_strip,(400,0))
    gamedisplays.blit(yellow_strip,(400,100))
    gamedisplays.blit(yellow_strip,(400,200))
    gamedisplays.blit(yellow_strip,(400,300))
    gamedisplays.blit(yellow_strip,(400,400))
    gamedisplays.blit(yellow_strip,(400,500))
    gamedisplays.blit(strip,(120,0))
    gamedisplays.blit(strip,(120,100))
    gamedisplays.blit(strip,(120,200))
    gamedisplays.blit(strip,(680,0))
    gamedisplays.blit(strip,(680,100))
    gamedisplays.blit(strip,(680,200))
    gamedisplays.blit(strip,(120,100))


def car(x,y):
    gamedisplays.blit(carimg,(x,y))


def game_loop():
    global pause
    pygame.mixer.music.load("Sound Tracks/backgroundmusic.wav")
    pygame.mixer.music.play(-1)
    x=(display_width*0.45)
    y=(display_height*0.8)
    x_change=0
    obstacle_speed=5
    obs=0
    y_change=0
    obs_startx=random.randrange(200,(display_width-200))
    obs_starty=-750
    obs_width=56
    obs_height=125
    passed=0
    level=0
    score=0
    y2=7
    fps=120
    bumped=False
    while not bumped:
        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.key==pygame.K_UP:
                  if obstacle_speed<22:
                      obstacle_speed+=1.5
               if event.key==pygame.K_DOWN:
                  if obstacle_speed>level*2:
                       obstacle_speed-=1
            if event.type==pygame.KEYUP:
                if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
                   x_change=0
        x+=x_change
        gamedisplays.fill(gray)

        rel_y=y2%backgroundpic.get_rect().width
        gamedisplays.blit(backgroundpic,(0,rel_y-backgroundpic.get_rect().width))
        gamedisplays.blit(backgroundpic,(700,rel_y-backgroundpic.get_rect().width))
        if rel_y<800:
            gamedisplays.blit(backgroundpic,(0,rel_y))
            gamedisplays.blit(backgroundpic,(700,rel_y))
            gamedisplays.blit(yellow_strip,(400,rel_y))
            gamedisplays.blit(yellow_strip,(400,rel_y+100))
            gamedisplays.blit(yellow_strip,(400,rel_y+200))
            gamedisplays.blit(yellow_strip,(400,rel_y+300))
            gamedisplays.blit(yellow_strip,(400,rel_y+400))
            gamedisplays.blit(yellow_strip,(400,rel_y+500))
            gamedisplays.blit(yellow_strip,(400,rel_y-100))
            gamedisplays.blit(strip,(120,rel_y-200))
            gamedisplays.blit(strip,(120,rel_y+20))
            gamedisplays.blit(strip,(120,rel_y+30))
            gamedisplays.blit(strip,(680,rel_y-100))
            gamedisplays.blit(strip,(680,rel_y+20))
            gamedisplays.blit(strip,(680,rel_y+30))

        y2+=obstacle_speed
        obs_starty-=(obstacle_speed/4)
        obstacle(obs_startx,obs_starty,obs)
        obs_starty+=obstacle_speed
        car(x,y)
        score_system(passed,score,level)
        if x>680-car_width or x<120:
            crash()
        if obs_starty>display_height:
            obs_starty=0-obs_height
            obs_startx=random.randrange(170,(display_width-170))
            obs=random.randrange(0,14)
            passed=passed+1
            score=passed*10
            if int(passed)%10==0:
                level=level+1
                obstacle_speed+=2
                largetext=pygame.font.Font("freesansbold.ttf",80)
                textsurf,textrect=text_objects("LEVEL"+str(level),largetext)
                textrect.center=((display_width/2),(display_height/2))
                gamedisplays.blit(textsurf,textrect)
                pygame.display.update()
                time.sleep(0.3)


        if y<obs_starty+obs_height:
            if x>obs_startx and x < obs_startx + obs_width or x+car_width > obs_startx and x+car_width < obs_startx+obs_width:
                    crash()

        button("PAUSE",650,0,150,50,blue,bright_blue,"pause")
        pygame.display.update()
        clock.tick(120)

intro_loop()
game_loop()
pygame.quit()
quit()
