import turtle
import math

NUM_TURTLES = 12
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 800
BACKGROUND_COLOR = "black"
PEN_WIDTH = 2
FORWARD_STEP = 5

def setup_screen():
    screen = turtle.Screen()
    screen.title("Python Demoscene by Gevorg Sargsyan")
    screen.bgcolor(BACKGROUND_COLOR)
    screen.setup(width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
    screen.colormode(255)
    screen.tracer(0)
    return screen

def create_turtles(count):
    turtles_list = []
    for i in range(count):
        t = turtle.Turtle()
        t.speed(0)
        t.width(PEN_WIDTH)
        t.hideturtle()
        t.left(i * (360 / count))
        turtles_list.append(t)
    return turtles_list

def get_shifting_color(tick):
    red = int((math.sin(tick * 0.015 + 0) + 1) * 127.5)
    green = int((math.sin(tick * 0.015 + 2) + 1) * 127.5)
    blue = int((math.sin(tick * 0.015 + 4) + 1) * 127.5)
    return (red, green, blue)

def main_animation_loop(screen, turtles_list):
    tick = 0
    try:
        while True:
            current_color = get_shifting_color(tick)

            turn_angle = 2 + math.cos(tick * 0.012) * 0.5

            for t in turtles_list:
                t.pencolor(current_color)
                t.forward(FORWARD_STEP)
                t.right(turn_angle)

            screen.update()
            tick += 1

    except turtle.Terminator:
        print("Demoscene animation stopped.")

if __name__ == "__main__":
    main_screen = setup_screen()
    drawing_turtles = create_turtles(NUM_TURTLES)

    main_animation_loop(main_screen, drawing_turtles)
