# drawing.py
# ----------
# By Ilma Buraira
#
# Draws the image of penguins on an iceberg
# (Briefly describe what this program does.)

from turtle import *
from math import cos, sin, pi, radians

# ---------- Setup ----------
setup(1000, 800)
screen = Screen()
screen.title("Penguin on Ice (with real ovals)")
screen.bgcolor("white")
screen.tracer(0)

pensize(3)
pencolor("black")

# ---------- Helpers ----------
def move_to(x, y):
    penup(); goto(x, y); pendown()

def filled_rect(x1, y1, x2, y2, fill, outline="black"):
    pencolor(outline)
    fillcolor(fill)
    move_to(x1, y1)
    begin_fill()
    goto(x2, y1)
    goto(x2, y2)
    goto(x1, y2)
    goto(x1, y1)
    end_fill()

def draw_oval(cx, cy, rx, ry, tilt=0, fill=None, outline=None, steps=180, pen=None):
    """Parametric ellipse; steps controls smoothness."""
    ang = radians(tilt)
    def R(x, y):  # rotate (x,y) by tilt
        return (x*cos(ang) - y*sin(ang), x*sin(ang) + y*cos(ang))
    pensize(pen if pen else 3)
    pencolor(outline if outline else "")
    fillcolor(fill if fill else "")
    move_to(cx + rx, cy)  # start at angle 0
    if fill:
        begin_fill()
    for i in range(1, steps + 1):
        theta = 2*pi*i/steps
        x, y = rx*cos(theta), ry*sin(theta)
        xr, yr = R(x, y)
        goto(cx + xr, cy + yr)
    if fill:
        end_fill()

def poly(points, fill=None):
    if fill: fillcolor(fill); begin_fill()
    move_to(*points[0])
    for p in points[1:]:
        goto(*p)
    goto(*points[0])
    if fill: end_fill()

def right_triangle(ax, ay, leg1, leg2, angle=0, fill=None):
    """Right angle at (ax, ay). leg1 along 'angle' degrees, leg2 perpendicular."""
    a = radians(angle)
    B = (ax + leg1*cos(a), ay + leg1*sin(a))            # along angle
    C = (ax - leg2*sin(a), ay + leg2*cos(a))            # 90° to the left
    poly([(ax, ay), B, C], fill=fill)

# ---------- Scene ----------
def background():
    # Ocean (bottom half)
    filled_rect(-500, -150, 500, -400, fill="#3d44c6", outline="#3d44c6")

    # Iceberg (white with light gray outline)
    pensize(4)
    filled_rect(-500, -60, 120, -150, fill="white", outline="#7f7f7f")

def penguins(ax, armtilt, footangle): 
    background()
    for x in range (ax, -500, -100):
        setheading(0)
        # Body 
        draw_oval(x-60, 10, rx=28, ry=70, tilt=0, fill="black")     
        # Belly 
        draw_oval(x-42, 12, rx=12, ry=65, tilt=0,fill="white", outline="darkslategray")
        # Arm 
        draw_oval(x-65, 0, rx=10, ry=40, tilt=armtilt, fill="black", outline="darkslategray", pen= 1)  
        # Head 
        draw_oval(x-42, 80, rx=25, ry=25, fill="black")
        # Beak
        right_triangle(x-28, 60, 25, 20, fill="gold")
        # Foot
        right_triangle(x-50, -60, 35, 15, footangle, fill="gold")   
    

#penguin arm movement: armtilt=345
#foot movement: footangle=35

def jump_penguin(ax, ay, armtilt, footangle):
    setheading(0)
        # Body 
    draw_oval(ax-60, ay+10, rx=28, ry=70, tilt=0, fill="black")     
        # Belly 
    draw_oval(ax-42, ay+12, rx=12, ry=65, tilt=0,fill="white", outline="darkslategray")
        # Arm 
    draw_oval(ax-65, ay+0, rx=10, ry=40, tilt=armtilt, fill="black", outline="darkslategray", pen= 1)  
        # Head 
    draw_oval(ax-42, ay+80, rx=25, ry=25, fill="black")
        # Beak
    right_triangle(ax-28, ay+60, 25, 20, fill="gold")
        # Foot
    right_triangle(ax-50, ay-60, 35, 15, footangle, fill="gold")




from superturtle.animation import animate
from superturtle.easing import easeInOutBack

for frame in animate(12, debug=False, loop=True):
    ax= frame.interpolate(120, 400, 0, 12)
    ay= frame.interpolate(0,-300, 0,12, easing=easeInOutBack)
    jump_penguin(ax,ay,345,35)
    if frame.index%12<=3:
        penguins(0,0,0)
    elif frame.index%12==4:
        penguins(60,345,35)
    elif frame.index%12==5 or frame.index%12==6:
        penguins(120, 345,35)
    else:
        penguins(120,0,0)

input()