# Leo Salazar
# 6/13/2024
# Silly typing game for swimming (for the olympics theme)

import pygame
import random
from classes import *

# initial setup
pygame.init()
screen = pygame.display.set_mode((720, 480))
pygame.display.set_caption("Swimming")
clock = pygame.time.Clock()
running = True
end = False
pressed = False

# presets for the characters
swim_speed = 40
difficulty = 5
person_w, person_h = 80, 40

# the ocean background
bg = pygame.image.load("ocean.jpg")
bg = pygame.transform.scale(bg, (720, 480))
bg_rect = bg.get_rect(topleft=(0,0))

# the swimmer image
athlete = pygame.image.load("athlete.png")
athlete = pygame.transform.scale(athlete, (person_w, person_h))
other = pygame.transform.grayscale(athlete)

# goal coordinates for reference
goal_coord = [screen.get_width() - 25, screen.get_height() - 25]

# making the groups
all_sprites = pygame.sprite.Group()
swimmers = pygame.sprite.Group()
enemies = pygame.sprite.Group()

# making the player, enemies, and goal
p1 = Player(all_sprites, athlete, "blue", 20, screen.get_height() - 60, 40, 20)
goal = Text(all_sprites, "*", 50, "grey", goal_coord[0], goal_coord[1], 50, 50)
finish_line = pygame.Rect(screen.get_width() - 50, 0, 1, screen.get_height())
swimmers.add(p1)
for i in range(3):
    temp = Enemy(all_sprites, other, "red", i+1, 20, screen.get_height() - 60, person_w, person_h)
    enemies.add(temp)
    swimmers.add(temp)

# generates new falling key
def newKey():
    return Falling(all_sprites, chr(random.randint(97,122)), 50, "orange", goal_coord[0], -25, 50, 50)

# stores current falling key
current = newKey()

# main game loop
while running:

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

        # if you press a key on the keyboard
        if event.type == pygame.KEYDOWN:
            pressed = True

            # checks to see if key matches the current falling key and on the target zone
            if event.unicode == current.text and goal.rect.colliderect(current.rect):

                # moves the player forward if true, not if false
                p1.rect.x += swim_speed

    # drawing the background
    screen.fill("light blue")
    screen.blit(bg, bg_rect)

    # updates the falling key and goal
    goal.update()
    current.update(screen, pressed, difficulty)
    pygame.draw.rect(screen, "black", finish_line)

    # draws all the objects on the screen
    all_sprites.draw(screen)

    # checks to see if a swimmer crosses the finish line
    for person in swimmers:
        if person.rect.colliderect(finish_line):
            font = pygame.font.Font(None, 60)

            # if the player crosses first, you win
            if isinstance(person, Player):
                text = font.render("You Win!", True, "Black", "White")
                text_rect = text.get_rect(topleft=(10,10))
                screen.blit(text, text_rect)

            # if an enemy crosses first, you lose
            else:
                text = font.render("You Lose!", True, "Black", "White")
                text_rect = text.get_rect(topleft=(10,10))
                screen.blit(text, text_rect)
            end = True

    # only make new keys if game isn't over
    if not current.alive() and not end:
        current = newKey()
        enemies.update(True)
        pressed = False

    # framerate
    clock.tick(60)

    # display stuff on screen
    pygame.display.flip()

# quit
pygame.quit()