
import pygame, random, sys, time, pygame._view
from pygame.locals import *


#set up some variables
WINDOWWIDTH = 1024
WINDOWHEIGHT = 640
FPS = 6000
TEXTCOLOR = (140, 0, 0)
BLACK = (0,0,0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
DARKRED = (136, 0, 0)
GREY = (100, 100, 100)

# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
Window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
WindowSurf = pygame.Surface((WINDOWWIDTH, WINDOWHEIGHT))
Rect = WindowSurf.get_rect()
winrect=Window.get_rect()
backgroundSurface = pygame.Surface
pygame.display.set_caption('ESCAPE')

# set up fonts
font = pygame.font.Font(None, 30)
font2 = pygame.font.Font(None, 250)
font3 = pygame.font.Font(None, 175)
#Images

    #key
KeyImage = pygame.image.load('TexturePack/Key.png')
    #area1
    #scissors
scissorsImage = pygame.image.load('TexturePack/area1/scissors.png')
scissorsRect = scissorsImage.get_rect()

    #startroom
backgroundImage = pygame.image.load('TexturePack/area1/Background1.png')
rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT))

    #Title
titleImage = pygame.image.load('TexturePack/area1/title.png').convert_alpha()
rescaledTitle = pygame.transform.scale(titleImage, (WINDOWWIDTH, WINDOWHEIGHT))

    #start room lit
startRoomLitImage = pygame.image.load('TexturePack/area1/Background1lit.png')
rescaledstartRoomLit = pygame.transform.scale(startRoomLitImage, (WINDOWWIDTH, WINDOWHEIGHT))

#images, area2
    #Background
A2Background = pygame.image.load('Texturepack/area2/Background2.png')
rescaledA2Background = pygame.transform.scale(A2Background, (WINDOWWIDTH, WINDOWHEIGHT))
    #Save
SaveImage = pygame.image.load('TexturePack/save.png')
SaveRect = SaveImage.get_rect()

PlayerHitTl = False
PlayerHitTr = False
PlayerHitBl = False
PlayerHitBr = False

def terminate():
    pygame.quit()
    sys.exit()

def textBox(Word, x, y):
    Text = font.render(Word, True, BLACK)
    rect = Text.get_rect()
    textBox1 = pygame.Surface((200, 50))
    textBox2 = pygame.Surface((200, 50))
    rect1 = textBox1.get_rect()
    texts['title']=[textBox1, rect]
    textBox1.fill(DARKRED)
    textBox2.fill(RED)
    pygame.draw.rect(textBox1, GREY, rect1, 5)
    pygame.draw.rect(textBox2, GREY, rect1, 5)
    rect.center = rect1.center
    textBox1.blit(Text, rect)
    textBox2.blit(Text, rect)
    rect1.centerx= x
    rect1.top=texts['title'][1].top+ y
    texts[Word]=[textBox1, rect1, textBox2]

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if texts[Word][1].collidepoint(event.pos):
                mode = Word    
    mouse=pygame.mouse.get_pos()
    if texts[Word][1].collidepoint(mouse):
        Window.blit(texts[Word][2], texts[Word][1])
    else:
        Window.blit(texts[Word][0], texts[Word][1])  
       
def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    terminate()
                if event.key == K_RETURN:
                    return
                if event.key == ord('q'):
                    return

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

def collideTl(x1, y1, w1, h1, x2, y2, w2, h2):
    #top left
    if (x2+w2>=x1>=x2 and y2+h2>=y1+5>=y2):
        return True
    else:
        return False

def collideTr(x1, y1, w1, h1, x2, y2, w2, h2):
    #top right
    if (x2+w2>=x1+w1>=x2 and y2+h2>=y1+5>=y2):
        return True
    else:
        return False
    
def collideBl(x1, y1, w1, h1, x2, y2, w2, h2):
    #bottom left
    if (x2+w2>=x1>=x2 and y2+h2>=y1+h1+5>=y2):
        return True
    else:
        return False
    
def collideBr(x1, y1, w1, h1, x2, y2, w2, h2):
    #bottom right
    if (x2+w2>=x1>=x2 and y2+h2>=y1+h1+5>=y2):
        return True
    else:
        return False
    
    
class Wall():
    def __init__(self,x,y,w,h):

        self.x=x

        self.y=y

        self.width=w

        self.height=h

    def render(self, tL, tR, bL, bR):
        
        if (tL==True):                   
            None 
        if (tR==True):                 
            None
        if (bL==True):                
            None
        if (bR==True):                   
            None   
        else:
            pygame.draw.rect(Window,BLACK,(self.x,self.y,self.width,self.height)) 


class solidWall():
    def __init__(self,x,y,w,h):

        self.x=x

        self.y=y

        self.width=w

        self.height=h

    def render(self, tL, tR, bL, bR):
        
        if (tL==True):
            global PlayerHitTl
            PlayerHitTl = True                    
            None 
        if (tR==True):
            global PlayerHitTr
            PlayerHitTr = True                    
            None
        if (bL==True):
            global PlayerHitBl
            PlayerHitBl = True                    
            None
        if (bR==True):
            global PlayerHitBr
            PlayerHitBr = True                    
            None 


        if (tL==False):
            global PlayerHitTl
            PlayerHitTl = False                    
             
        if (tR==False):
            global PlayerHitTr
            PlayerHitTr = False                    
            
        if (bL==False):
            global PlayerHitBl
            PlayerHitBl = False                    
            
        if (bR==False):
            global PlayerHitBr
            PlayerHitBr = False                                

        pygame.draw.rect(Window,RED,(self.x,self.y,self.width,self.height)) 
    
class Sprite:
    def __init__(self,x,y):
        #demensions
        self.x=x
        self.y=y
        self.width=30
        self.height=30
        self.reset = False
        #images
        self.i0 = pygame.image.load('TexturePack/Player/Player.png')
        self.i1 = pygame.image.load('TexturePack/Player/Player1.png')
        self.i2 = pygame.image.load('TexturePack/Player/Player2.png')
            #image Rotation
        self.i0L=pygame.transform.rotate(self.i0,90)
        self.i0R=pygame.transform.rotate(self.i0,270)
        self.i0U=pygame.transform.rotate(self.i0, 0)
        self.i0D=pygame.transform.rotate(self.i0, 180)

        #Rect
        self.rect = self.i0.get_rect
        
        #frame update
        self.timeTarget=10
        self.timeNum=0
        self.currentImage=0
        self.face = 0
        
    def render(self):
        if (self.currentImage==0):
            if moveLeft == True:
                self.face=1
                
            if moveRight == True:
                self.face=2
                
            if moveUp == True:
                self.face=0
                
            if moveDown == True:
                self.face=3

            if self.face == 1:
                Window.blit(self.i0L, (self.x, self.y))
                
            if self.face == 2:
                Window.blit(self.i0R, (self.x, self.y))

            if self.face == 0:
                Window.blit(self.i0U, (self.x, self.y))

            if self.face == 3:
                Window.blit(self.i0D, (self.x, self.y))

            if self.reset == 1:
                self.x = 500
                self.y = 570
                self.reset = 2
        pygame.display.flip()   

def TitleScreen():
    Window.blit(rescaledTitle, (0, 0))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE: # pressing escape quits
                terminate()
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                terminate()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if texts['Exit'][1].collidepoint(event.pos):
                terminate()
            if texts['Start'][1].collidepoint(event.pos):
                global mode
                mode = 'A1'

def area1():
    # Draw the game world on the window.
    # set up start of game
    Window.fill(BLACK)
    Window.blit(rescaledBackground, (0,0))
    # set up rooms
        #start room
    Startroom=Wall(310,430, 310, 225)
    tL=collideTl(Player.x,Player.y,Player.width-15,Player.height-15,Startroom.x,Startroom.y,Startroom.width,Startroom.height)
    tR=collideTr(Player.x,Player.y,Player.width-15,Player.height-15,Startroom.x,Startroom.y,Startroom.width,Startroom.height)
    bL=collideBl(Player.x,Player.y,Player.width-15,Player.height-15,Startroom.x,Startroom.y,Startroom.width,Startroom.height)
    bR=collideBr(Player.x,Player.y,Player.width-15,Player.height-15,Startroom.x,Startroom.y,Startroom.width,Startroom.height)
    Startroom.render(tL, tR, bL, bR)
        #hallway1
    Hall=Wall(172,320, 701, 110)
    tL=collideTl(Player.x,Player.y,Player.width-15,Player.height-15,Hall.x,Hall.y,Hall.width,Hall.height)
    tR=collideTr(Player.x,Player.y,Player.width-15,Player.height-15,Hall.x,Hall.y,Hall.width,Hall.height)
    bL=collideBl(Player.x,Player.y,Player.width-15,Player.height-15,Hall.x,Hall.y,Hall.width,Hall.height)
    bR=collideBr(Player.x,Player.y,Player.width-15,Player.height-15,Hall.x,Hall.y,Hall.width,Hall.height)
    Hall.render(tL, tR, bL, bR)
        #torture room
    tRoom=Wall(324,0, 700, 320)
    tL=collideTl(Player.x,Player.y,Player.width-15,Player.height-15,tRoom.x,tRoom.y,tRoom.width,tRoom.height)
    tR=collideTr(Player.x,Player.y,Player.width-15,Player.height-15,tRoom.x,tRoom.y,tRoom.width,tRoom.height)
    bL=collideBl(Player.x,Player.y,Player.width-15,Player.height-15,tRoom.x,tRoom.y,tRoom.width,tRoom.height)
    bR=collideBr(Player.x,Player.y,Player.width-15,Player.height-15,tRoom.x,tRoom.y,tRoom.width,tRoom.height)
    tRoom.render(tL, tR, bL, bR)    
        #stairs
    stairs=solidWall(873,320,157,115)
    tL=collideTl(Player.x,Player.y,Player.width-15,Player.height-15,stairs.x,stairs.y,stairs.width,stairs.height)
    tR=collideTr(Player.x,Player.y,Player.width-15,Player.height-15,stairs.x,stairs.y,stairs.width,stairs.height)
    bL=collideBl(Player.x,Player.y,Player.width-15,Player.height-15,stairs.x,stairs.y,stairs.width,stairs.height)
    bR=collideBr(Player.x,Player.y,Player.width-15,Player.height-15,stairs.x,stairs.y,stairs.width,stairs.height)
    stairs.render(tL, tR, bL, bR)
        #hallway 2
    Hall2 = Wall(170,0,151,320)
    tL=collideTl(Player.x,Player.y,Player.width-15,Player.height-15,Hall2.x,Hall2.y,Hall2.width,Hall2.height)
    tR=collideTr(Player.x,Player.y,Player.width-15,Player.height-15,Hall2.x,Hall2.y,Hall2.width,Hall2.height)
    bL=collideBl(Player.x,Player.y,Player.width-15,Player.height-15,Hall2.x,Hall2.y,Hall2.width,Hall2.height)
    bR=collideBr(Player.x,Player.y,Player.width-15,Player.height-15,Hall2.x,Hall2.y,Hall2.width,Hall2.height)
    Hall2.render(tL, tR, bL, bR)

    Player.render()

def area2():
    Hall2 = False
    Hall3 = False
    Bathroom = False
    SaveRoom1 = False
    Player.reset += 1

    Window.fill(BLACK)

    Window.blit(rescaledA2Background, (0,0))
    SaveRoom1 = Wall(170,220,0,320)
    collisions=collide(Player.x,Player.y,Player.width-15,Player.height-15,SaveRoom1.x,SaveRoom1.y,SaveRoom1.width,SaveRoom1.height)
    SaveRoom1.render(collisions)

    Player.render()

# show the "Start" screen
Player = Sprite(500,570)
wall = Wall(0,0,100,100)
TitleScreen()
waitForPlayerToPressKey()
texts={}
PlayerHit = False

while True:
    mode = 'A1'
    moveLeft = moveRight =False
    moveUp = moveDown = False
    while True: # the game loop runs while the game part is playing
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
                
            if event.type == KEYDOWN:
                if event.key == K_LEFT or event.key == ord('a'):
                    moveRight = False
                    moveLeft = True
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveLeft = False
                    moveRight = True
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = True
                    moveDown = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveUp = False
                    moveDown = True
            if event.type == KEYUP:
                if event.key == K_ESCAPE:
                        terminate()

                if event.key == K_LEFT or event.key == ord('a'):
                    moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveRight = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False

        if (collideTl==False):
            global PlayerHitTl
            PlayerHitTl = False                    
             
        if (collideTr==False):
            global PlayerHitTr
            PlayerHitTr = False                    
            
        if (collideBl==False):
            global PlayerHitBl
            PlayerHitBl = False                    
            
        if (collideBr==False):
            global PlayerHitBr
            PlayerHitBr = False   
                    
                    # Move the player around.
        if moveLeft == True and Player.x > 0 and PlayerHitTl == False and PlayerHitBl == False:
            Player.x += -2
        if moveRight == True and Player.x < WINDOWWIDTH-30 and PlayerHitTr == False and PlayerHitBr == False:
            Player.x += 2
        if moveUp == True and Player.y > 0 and PlayerHitTl == False and PlayerHitTr == False:
            Player.y += -2
        if moveDown == True and Player.y < WINDOWHEIGHT-30 and PlayerHitBl == False and PlayerHitBr == False:
            Player.y += 2

        if Player.y <=30:
            global mode
            mode = 'A2'
            pygame.time.delay(10)

        global mode

        if mode == 'title':
            TitleScreen()
            
        elif mode == 'A1':
            area1()

        elif mode == 'A2':
            area2()
        
        # update the display
        mainClock.tick(60)
