import pygame

bg = ''

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        pygame.sprite.Sprite.__init__(self)
        
        # Нарисуем зеленый квадрат на “чистой” поверхности
        self.image = pygame.Surface((20, 20))
        pygame.draw.rect(self.image, color, (0, 0, 20, 20))

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.y_speed = 0
        self.can_jump = False
   

        # Добавим созданный объект в группу спрайтов
        all_sprites.add(self)

    def update(self):
        # Движение вниз
        self.rect.y += self.y_speed
        # Увеличение скорости движения вниз (гравитация)
        self.y_speed += 0.2

        if self.is_touch_platform():
            # В случае когда падает вниз
            if self.y_speed > 0:
                self.y_speed = 0
                self.push_up()
                self.can_jump = True
            else:
                self.y_speed = 0
                self.push_down()

        


    def is_touch_platform(self):
        return pygame.sprite.spritecollide(self, platforms, False)
    
    def jump(self):
        if self.can_jump == True:
            self.y_speed -= 10
            self.can_jump = False

    def move(self, step):
        self.rect.x += step
        # Чтобы не залететь в платформу сбоку
        if self.is_touch_platform():
            self.rect.x -= step

        if self.rect.right > WIDTH:
            if level.level_number == level.max_level:
                self.rect.right = WIDTH
            else:
                self.rect.x = 100
                self.rect.y = 100
                level.next_level()
                #levelSound.play()

     #  Выталкивание вниз
    def push_down(self):
        while self.is_touch_platform():
            self.rect.y += 1

    #  Выталкивание вверх
    def push_up(self):
        while self.is_touch_platform():
            self.rect.y -= 1


class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((width, height))
        pygame.draw.rect(self.image, 'yellow', (0, 0, width, height))

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        all_sprites.add(self)
        platforms.add(self)  

class Button(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, text):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((width, height))
        pygame.draw.rect(self.image, 'lightgreen', (0, 0, width, height))

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.width = width
        self.height = height
        self.text = text

        buttons.add(self) 

    def update(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(mouse_pos):
            self.image.fill('plum')
        else:
            self.image.fill('purple')

        button_font_text = button_font.render(self.text, False, 'black')
        # Отобразить текст по центру кнопки можно следующим образом:
        self.image.blit(button_font_text, (self.width / 2 - button_font_text.get_width() / 2, self.height / 2 - button_font_text.get_height() / 2))
       

class Level():
    def __init__(self):
        self.level_number = 1
        self.max_level = 6

    def clear_level(self):
        for platform in platforms: 
            platform.kill()

    def create_level(self, level_number):
        global bg
        self.clear_level()

        if level_number == 1:
            bg = bg1
            
            plat0 = Platform(0, HEIGHT - 50, WIDTH, 50)
            plat1 = Platform(150, 500, 100, 20)
            plat2 = Platform(250, 450, 80, 20)
            plat2 = Platform(350, 400, 80, 20)
            plat2 = Platform(450, 350, 80, 20)
            plat2 = Platform(550, 300, 80, 20)

        if level_number == 2:
            bg = bg2
            plat0 = Platform(0, HEIGHT - 50, WIDTH, 50)
            plat1 = Platform(50, 200, 300, 20)
            plat2 = Platform(250, 100, 80, 20)
            plat2 = Platform(350, 150, 80, 20)
            plat2 = Platform(450, 200, 80, 20)
            plat2 = Platform(550, 250, 80, 20)

        if level_number == 4:
            bg = bg4
            plat0 = Platform(0, HEIGHT - 50, WIDTH, 50)
            
            

        

        if level_number == 3:
            bg = bg3
            plat0 = Platform(0, HEIGHT - 50, WIDTH, 50)

        
        
        
        
        
        
        
        if level_number == 6:
            bg = bg6
            plat0 = Platform(0, HEIGHT - 50, WIDTH, 50)
            

    def next_level(self):
        if self.level_number < self.max_level:
            self.level_number += 1
            self.create_level(self.level_number)

    def secret_level(self):
        if self.y == 0:
            self.level_number = 6
            self.create_level(self.level_number)

# Начало программы

WIDTH = 800
HEIGHT = 600
FPS = 60

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Платформер')
clock = pygame.time.Clock()

bg_menu = pygame.image.load('bg_menu.png')
bg_menu = pygame.transform.scale(bg_menu, (WIDTH, HEIGHT))

bg1 = pygame.image.load('bg_level1.png')
bg1 = pygame.transform.scale(bg1, (WIDTH, HEIGHT))

bg2= pygame.image.load('bg_level2.png')
bg2= pygame.transform.scale(bg2, (WIDTH, HEIGHT))

bg3= pygame.image.load('bg_level3.png')
bg3= pygame.transform.scale(bg3, (WIDTH, HEIGHT))

bg4= pygame.image.load('bg_level4.png')
bg4= pygame.transform.scale(bg4, (WIDTH, HEIGHT))

bg6 = pygame.image.load('bg_secretlevel.jpg')
bg6= pygame.transform.scale(bg6, (WIDTH, HEIGHT))

bg0 = pygame.image.load('bg_level0.png')
bg0= pygame.transform.scale(bg0, (WIDTH, HEIGHT))




all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
buttons = pygame.sprite.Group()

level = Level()
level.create_level(1)


player = Player(100, 100, 'red')



button_font = pygame.font.SysFont('impact', 24)


stage = 'start'
running = True

jumpSound = pygame.mixer.Sound('mario_jump.ogg')
jumpSound.set_volume(0.05)

button1 = Button(100, 250, 120, 35, 'Start')

button2 = Button(100, 300, 120, 35, 'QUIT')

button3 = Button(100, 350, 120, 35, 'Creators')

buttons.add(button1,button2, button3)

while running:
    mouse_pos = pygame.mouse.get_pos()
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1 and button1.rect.collidepoint(mouse_pos):
                stage = 'game'
            if event.button == 1 and button2.rect.collidepoint(mouse_pos):
                running = False
            if event.button == 1 and button3.rect.collidepoint(mouse_pos):
                stage = 'Creators'
                
            

    if stage == 'start':
        screen.blit(bg_menu, (0, 0))
        buttons.update()
        buttons.draw(screen)

    if stage == 'Creators':
        screen.blit(bg0, (0,0))
        keys = pygame.key.get_pressed()
        if keys[pygame.K_ESCAPE]:
            stage = 'start'

        


        
    
    if stage == 'game':
        keys = pygame.key.get_pressed()


        if keys[pygame.K_SPACE]:
            player.jump()
            jumpSound.play(2)

        if keys[pygame.K_RIGHT]:
            player.move(5)
        
        if keys[pygame.K_LEFT]:
            player.move(-5)

        # Обновление спрайтов группы all_sprites
        all_sprites.update()

        screen.fill('white')
        screen.blit(bg, (0,0))
        # Отрисовка спрайтов группы all_sprites
        all_sprites.draw(screen)
        
    

    pygame.display.update()
    clock.tick(FPS)