"""
LESSON 1 - SOLUTION (for the instructor)
=========================================
Full working version of lesson1_starter.py, plus a bonus free-movement
mode controlled with the arrow keys, so kids who finish early can keep
exploring before Lesson 2 introduces keyboard input properly.
"""

import turtle

screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgpic("path_background.png")
screen.title("Lesson 1: Guide the Turtle! (solution)")

t = turtle.Turtle()
t.shape("turtle")
t.color("purple")
t.penup()
t.goto(-350, -250)
t.setheading(90)
t.pendown()
t.speed(2)

# --- Part A: scripted movement (matches the waypoints baked into the image) ---
t.forward(170)
t.right(90)
t.forward(200)
t.left(90)
t.forward(170)
t.right(90)
t.forward(250)
t.left(90)
t.forward(160)
t.right(90)
t.forward(250)

finish_x, finish_y = 350, 250
distance_to_finish = t.distance(finish_x, finish_y)
if distance_to_finish < 20:
    t.write("You reached the finish!  ", align="left", font=("Arial", 16, "bold"))
else:
    t.write(f"Not quite! {int(distance_to_finish)} px away.", align="left", font=("Arial", 14, "normal"))

"""
--- Part B (BONUS): free movement with arrow keys ---
Uncomment the block below to let kids drive the turtle by hand instead.
This previews the keyboard-input skill used in Lesson 2.
"""
STEP = 15
#
def go_up():
    t.setheading(90)
    t.forward(STEP)
#
def go_down():
    t.setheading(270)
    t.forward(STEP)
#
def go_left():
    t.setheading(180)
    t.forward(STEP)
#
def go_right():
    t.setheading(0)
    t.forward(STEP)
    
screen.listen()
screen.onkeypress(go_up, "Up")
screen.onkeypress(go_down, "Down")
screen.onkeypress(go_left, "Left")
screen.onkeypress(go_right, "Right")

screen.mainloop()
