import pygame
import sys
pygame.init()

CLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((1000, 800))

X_POS, Y_POS = 100, 700
Y_GRAVITY = 1
JUMP_HEIGHT = 20
Y_VELOCITY = 20
jumping = False

STANDING_SURFACE = pygame.transform.scale(pygame.image.load("images/character1.png"), (48, 64))
JUMPING_SURFACE = pygame.transform.scale(pygame.image.load("images/character2.png"), (48, 64))
BACKGROUND = pygame.image.load("images/back.png")
BACKGROUND_MAIN = pygame.image.load("images/back_main")

crest = STANDING_SURFACE.get_rect(center = (X_POS, Y_POS))

door_image = pygame.image.load("images/door.png")
door_rect = door_image.get_rect(center = (800, 700))
wall_image = pygame.image.load("images/wall.png")
wall_right = pygame.Rect(1000, 0, 1, 800)
wall_left = pygame.Rect(0, 0, 1, 800)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    keys_pressed = pygame.key.get_pressed()

    if keys_pressed[pygame.K_SPACE]:

        jumping = True
    SCREEN.blit(wall_image, wall_right)
    SCREEN.blit(wall_image, wall_left)
    SCREEN.blit(BACKGROUND_MAIN, (0, 0))
    SCREEN.blit(door_image, door_rect)

    if crest.colliderect(wall_right):
        X_POS -= 5
    if crest.colliderect(wall_left):
        X_POS += 5

    if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
        X_POS += 5

    if keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
        X_POS -= 5
        

    if jumping:
        Y_POS -= Y_VELOCITY
        Y_VELOCITY -= Y_GRAVITY
        if Y_VELOCITY < -JUMP_HEIGHT:
            jumping = False
            Y_VELOCITY = JUMP_HEIGHT
        crest = JUMPING_SURFACE.get_rect(center = (X_POS, Y_POS))
        SCREEN.blit(JUMPING_SURFACE, crest)
    else:
        crest = STANDING_SURFACE.get_rect(center = (X_POS, Y_POS))
        SCREEN.blit(STANDING_SURFACE, crest)

    if crest.colliderect(door_rect) and keys_pressed[pygame.K_e]:
        print("You win!")
        pygame.quit()
        sys.exit()


    pygame.display.update()
    CLOCK.tick(60)