Загрузка данных


from turtle import *


v = 0
step = 10
palette_size = 30 # Размер квадратиков палитры
colors = [
    ('red', 'r'),
    ('green', 'g'),
    ('blue', 'b'),
    ('orange', 'o'),
    ('yellow', 'y'),
    ('light blue', 'l'),
    ('violet', 'v')
]

# --- Инициализация черепашки ---
t = Turtle()
t.color('black')
t.width(5)
t.shape('circle')
t.pendown()
t.speed(v)

scr = t.getscreen()

# --- Функции управления ---
def set_color(color_name):
    t.color(color_name)

def stepUp():
    t.goto(t.xcor(), t.ycor() + step)

def stepDown():
    t.goto(t.xcor(), t.ycor() - step)

def stepLeft():
    t.goto(t.xcor() - step, t.ycor())

def stepRight():
    t.goto(t.xcor() + step, t.ycor())

def startFill():
    t.begin_fill()

def endFill():
    t.end_fill()

def background_black():
    scr.bgcolor('black')

def background_white():
    scr.bgcolor('white')

# --- Привязка событий клавиатуры ---
scr.onkey(lambda: set_color('red'), 'r')
scr.onkey(lambda: set_color('green'), 'g')
scr.onkey(lambda: set_color('blue'), 'b')
scr.onkey(lambda: set_color('orange'), 'o')
scr.onkey(lambda: set_color('yellow'), 'y')
scr.onkey(lambda: set_color('light blue'), 'l')
scr.onkey(lambda: set_color('violet'), 'v')

scr.onkey(stepUp, 'Up')
scr.onkey(stepDown, 'Down')
scr.onkey(stepLeft, 'Left')
scr.onkey(stepRight, 'Right')
scr.onkey(startFill, 'f')
scr.onkey(endFill, 'e')
scr.onkey(background_black, 'n')
scr.onkey(background_white, 'd')

# --- Рисование палитры ---
palette_turtle = Turtle()
palette_turtle.hideturtle()
palette_turtle.penup()
palette_turtle.speed(0)

start_x = - (len(colors) * (palette_size + 5)) / 2 # Центрируем палитру сверху
start_y = 200

for i, (color_name, _) in enumerate(colors):
    palette_turtle.goto(start_x + i * (palette_size + 5), start_y)
    palette_turtle.pendown()
    palette_turtle.color('black', color_name) # Обводка черная, заливка - цвет
    palette_turtle.begin_fill()
    for _ in range(4):
        palette_turtle.forward(palette_size)
        palette_turtle.right(90)
    palette_turtle.end_fill()
    palette_turtle.penup()

# --- Функция клика по палитре ---
def get_clicked_color(x, y):
    for i, (color_name, _) in enumerate(colors):
        x_min = start_x + i * (palette_size + 5)
        x_max = x_min + palette_size
        y_min = start_y - palette_size
        y_max = start_y
        
        if x_min <= x <= x_max and y_min <= y <= y_max:
            return color_name
    return None

def on_screen_click(x, y):
    clicked_color = get_clicked_color(x, y)
    if clicked_color:
        set_color(clicked_color)
    else:
        move(x, y) # Если кликнули не по палитре, а по холсту

# --- Привязка событий мыши ---


def draw(x, y):
    t.ondrag(None) # Отключаем рекурсию
    t.goto(x, y)
    t.ondrag(draw)

def move(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()

scr.onscreenclick(on_screen_click) # Клик мышью

t = Turtle()
t.color('black')
t.width(5)
t.shape('circle')
t.pendown()
t.speed(v)
 
def draw(x, y):
  t.goto(x, y)
 
def move(x, y):
  t.penup()
  t.goto(x, y)
  t.pendown()
 
def setRed():
  t.color('red')
 
def setGreen():
  t.color('green')
 
def setBlue():
  t.color('blue')


def setOrange():
    t.color('orange')


def setYellow():
    t.color('yellow')


def setLightBlue():
    t.color('light blue')


def setViolet():
    t.color('violet')


def setWidth_1():
    t.width(5)


def setWidth_2():
    t.width(10)


def setWidth_3():
    t.width(18)


def setWidth_4():
    t.width(25)


def setWidth_5():
    t.width(35)    
 
def stepUp():
 t.goto(t.xcor(), t.ycor() + step)
 
def stepDown():
 t.goto(t.xcor(), t.ycor() - step)
 
def stepLeft():
 t.goto(t.xcor() - step, t.ycor())
 
def stepRight():
 t.goto(t.xcor() + step, t.ycor())
 
def startFill():
  t.begin_fill()
 
def endFill():
  t.end_fill()


def background_black():
    scr.bgcolor('black')


def background_white():
    scr.bgcolor('white')


t.ondrag(draw)
 
scr = t.getscreen()

# --- Запуск слушателя ---
scr.listen()