import pygame
import os
import random


GREEN = (50, 110, 0)
BLUE = (0, 0, 255)
WIDTH, HEIGHT = 1200, 960
FPS = 60

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")
bag = pygame.image.load(os.path.join(img_folder, "bag.png"))
egg = pygame.image.load(os.path.join(img_folder, "egg.png"))
shtk = pygame.image.load(os.path.join(img_folder, "SHTUKA.png"))
egg_img = pygame.transform.scale(egg, (130, 150))
bag_img = pygame.transform.scale(bag, (400, 284))
shtk_img = pygame.transform.scale(shtk, (1200, 400))
shtk_rect = shtk_img.get_rect()
clock = pygame.time.Clock()

class Bag(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = bag_img
        self.rect = self.image.get_rect()
        self.rect.x = WIDTH/2 - 200
        self.rect.y = 675
        self.speedx = 0
    def update(self):
        self.speedx = 0
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.speedx = -50
        if key[pygame.K_RIGHT]:
            self.speedx = 50

        self.rect.x += self.speedx
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        elif self.rect.left < 0:
            self.rect.left = 0

class Egg(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = egg_img
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(100, 1200, 400)
        self.rect.y = random.randrange(-250, 100)
        self.speedy = random.randint(10, 15)
        self.speedx = 0
    def update(self):
        self.rect.y += self.speedy
        self.rect.x += self.speedx
        if self.rect.bottom > HEIGHT:
            quit()


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()


bag = Bag()
egg = Egg()
all_sprites = pygame.sprite.Group()
eggs = pygame.sprite.Group()
all_sprites.add(bag)
all_sprites.add(egg)
eggs.add(egg)

font_name = pygame.font.match_font('arial')

def draw_text(surf, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, GREEN)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)


score = 0
running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

    hits = pygame.sprite.spritecollide(bag, eggs, True)
    if hits:
        score += 1
        egg = Egg()
        all_sprites.add(egg)
        eggs.add(egg)
    screen.fill(GREEN)
    all_sprites.update()
    screen.blit(shtk_img, shtk_rect)
    all_sprites.draw(screen)
    draw_text(screen, str(score), 30, WIDTH / 2, 10)
    pygame.display.flip()
pygame.quit()