import pygame as pg
from math import pi
from time import sleep
from random import *


class Game():
    def __init__(self):
        self.colors = [tuple([randrange(50, 256) for _ in range(3)]) for _ in range(9)]
        self.line_color = choice(self.colors)
        self.right = choice([True, False])
        self.lose = False
        self.win = False
        self.scores = 0

    def render(self, screen):  # Создание круга из случайных цветов и стрелки
        pg.draw.arc(screen, (self.colors[0]),
                    (120, 90, 350, 350), 0, pi / 9, 7)
        pg.draw.arc(screen, (self.colors[1]),
                    (120, 90, 350, 350), pi / 9, 2 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[2]),
                    (120, 90, 350, 350), 2 * pi / 9, 3 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[3]),
                    (120, 90, 350, 350), 3 * pi / 9, 4 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[4]),
                    (120, 90, 350, 350), 4 * pi / 9, 5 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[5]),
                    (120, 90, 350, 350), 5 * pi / 9, 6 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[6]),
                    (120, 90, 350, 350), 6 * pi / 9, 7 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[7]),
                    (120, 90, 350, 350), 7 * pi / 9, 8 * pi / 9, 7)
        pg.draw.arc(screen, (self.colors[8]),
                    (120, 90, 350, 350), 8 * pi / 9, pi, 7)
        pg.draw.line(screen, (self.line_color), [300, 270], [300, 130], 9)

        font = pg.font.Font(None, 50)
        text = font.render('Счёт:', True, (255, 255, 255))
        screen.blit(text, (50, 300))
        text = font.render(f'{self.scores}/10', True, (255, 255, 255))
        screen.blit(text, (70, 340))

        if self.lose:
            Lose.text(self)
        elif self.win:
            Win.text(self)

    def side_change(self):  # Изменение стороны
        if not self.right:
            self.right = True
        elif self.right:
            self.right = False
        if color_change:
            self.old_color = self.line_color
            while self.old_color == self.line_color:
                self.line_color = choice(self.colors)

    def animation(self):  # анимация (изменение координат)
        if self.right:
            sleep(0.4)
            self.color = self.colors[0]
            del self.colors[0]
            self.colors.append(self.color)
        else:
            sleep(0.4)
            self.colors.reverse()
            self.color = self.colors[0]
            del self.colors[0]
            self.colors.append(self.color)
            self.colors.reverse()
        if self.scores == 10:
            self.win = True

    def check(self):
        if self.line_color == self.colors[3] or self.line_color == self.colors[5] or self.line_color == self.colors[4]:
            self.scores += 1
        else:
            self.lose = True


class Win:
    def text(self):
        pg.draw.rect(screen, (40, 40, 40), (0, 0, 599, 599))
        font = pg.font.Font(None, 70)
        text = font.render('Вы выиграли!', True, (255, 255, 255))
        screen.blit(text, (120, 150))


class Lose:
    def text(self):
        pg.draw.rect(screen, (40, 40, 40), (0, 0, 599, 599))
        font = pg.font.Font(None, 70)
        text = font.render('Вы проиграли!', True, (255, 255, 255))
        screen.blit(text, (120, 150))


if __name__ == '__main__':
    pg.init()
    size = 600, 400
    screen = pg.display.set_mode(size)
    game = Game()
    animation = False
    color_change = False
    running = True

    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONDOWN:  # Запуск игры
                if color_change == False:
                    animation = True
                    game.side_change()
                    color_change = True
                else:
                    game.side_change()
                    game.check()
        if animation:
            game.animation()
        pg.display.update()
        screen.fill((40, 40, 40))
        game.render(screen)
        pg.display.flip()
    pg.display.quit()
