from turtle import *
from math import sin, cos, pi
import random


def ellipse(a, b):
    x, y = pos()

    penup()
    goto(x + a, y)
    pendown()
    begin_fill()

    for i in range(361):
        t = i * pi / 180
        goto(x + a * cos(t), y + b * sin(t))

    end_fill()
    penup()
    goto(x, y)


def draw_eye(x, y, width, height):
    goto(x, y)
    color("#f7f7f7")
    ellipse(width, height)

    color("#2b2b2b")
    ellipse(width * 0.28, height * 0.45)


def draw_face(scale_factor=2):
    start_x, start_y = pos()

    # wenige Grundparameter
    face_width = random.gauss(1.0, 0.05)
    face_height = random.gauss(1.0, 0.06)
    eye_size = random.gauss(1.0, 0.08)
    nose_size = random.gauss(1.0, 0.07)
    mouth_size = random.gauss(1.0, 0.08)

    skull_width = 82 * face_width / scale_factor
    skull_height = 105 * face_height / scale_factor

    jaw_width = skull_width * random.gauss(0.82, 0.04)
    jaw_height = skull_height * random.gauss(0.92, 0.04)

    eye_y = start_y + skull_height * 0.12
    eye_distance = skull_width * random.gauss(0.42, 0.03)
    eye_width = skull_width * 0.18 * eye_size
    eye_height = skull_height * 0.055 * eye_size

    nose_top_y = eye_y - skull_height * 0.10
    nose_length = skull_height * 0.28 * nose_size
    nose_width = skull_width * 0.18 * nose_size

    mouth_y = nose_top_y - nose_length - skull_height * 0.18
    mouth_width = nose_width * random.gauss(2.15, 0.10) * mouth_size
    mouth_height = skull_height * 0.035 * mouth_size

    ear_width = skull_width * 0.11
    ear_height = skull_height * 0.25
    ear_y = start_y + skull_height * 0.03

    hair_height = skull_height * random.gauss(0.22, 0.03)

    # Kopf
    goto(start_x, start_y)
    color("#d6b98f")
    ellipse(skull_width, skull_height)

    # Gesicht innen / Kieferpartie
    goto(start_x, start_y - skull_height * 0.08)
    color("#f0d2a5")
    ellipse(jaw_width, jaw_height)

    # Haare
    goto(start_x, start_y + skull_height * 0.62)
    color("#4a3526")
    ellipse(skull_width * 0.95, hair_height)

    # Ohren
    color("#e8c394")
    goto(start_x - skull_width * 1.02, ear_y)
    ellipse(ear_width, ear_height)

    goto(start_x + skull_width * 1.02, ear_y)
    ellipse(ear_width, ear_height)

    # Augen
    draw_eye(start_x - eye_distance, eye_y, eye_width, eye_height)
    draw_eye(start_x + eye_distance, eye_y, eye_width, eye_height)

    # Nase
    color("#d3a36f")
    pensize(max(2, nose_width * 0.35))
    goto(start_x, nose_top_y)
    pendown()
    goto(start_x, nose_top_y - nose_length)
    penup()

    color("#c98f5f")
    goto(start_x, nose_top_y - nose_length)
    ellipse(nose_width * 0.55, skull_height * 0.035)

    # Mund
    color("#a95f6b")
    goto(start_x, mouth_y)
    ellipse(mouth_width * 0.55, mouth_height)

    color("#6e3038")
    pensize(max(1, mouth_height * 0.45))
    goto(start_x - mouth_width * 0.45, mouth_y)
    pendown()
    goto(start_x + mouth_width * 0.45, mouth_y)
    penup()
    pensize(1)

    # zurücksetzen
    goto(start_x, start_y)
    setheading(0)


# Fenster
#setup(650, 650)
setup(width=650, height=650, startx=300, starty=150)
hideturtle()
speed(0)
tracer(0, 0)
penup()

# mehrere Gesichter zeichnen
y = 230
for row in range(4):
    x = -230
    for col in range(4):
        goto(x, y)
        draw_face(scale_factor=2)
        x += 155
    y -= 155

update()
done()