# Homework 1 Solutions
# Software Design
# Allen Downey

# LEVEL 0 PRIMITIVES are provided by World.py.
# They include fd, bk, lt, rt, pu and pd

# LEVEL 1 PRIMITIVES are simple combinations of Level 0 primitives.
# They have no pre- or post-conditions.

def fdrt(t, n, angle=90):
    # forward and right
    fd(t, n)
    rt(t, angle)

def fdlt(t, n, angle=90):
    # forward and left
    fd(t, n)
    lt(t, angle)

def fdbk(t, n):
    # forward and back, ending at the original position
    fd(t, n)
    bk(t, n)

def ltbk(t, n, angle=90):
    # left turn and back up
    lt(t, angle)
    bk(t, n)

def skip(t, n):
    # lift the pen and move
    pu(t)
    fd(t, n)
    pd(t)


# LEVEL 2 PRIMITIVES use primitives from Levels 0 and 1
# to draw posts (vertical elements) and beams (horizontal elements)
# Level 2 primitives ALWAYS return the turtle to the original
# location and direction.

def post(t, n):
    # make a vertical line and return to the original position
    lt(t)
    fdbk(t, n)
    rt(t)

def beam(t, n, height):
    # make a horizontal line at the given height and return
    lt(t)
    fdrt(t, n * height)
    fdbk(t, n)
    ltbk(t, n * height)
    rt(t)

# The letter-drawing functions all have the precondition
# that the turtle is in the lower-left corner of the letter,
# and postcondition that the turtle is in the lower-right
# corner, facing in the direction it started in.

def draw_a(t, n):
    beam(t, n, 2)
    beam(t, n, 1)
    skip(t, n)
    post(t, 2*n)


def draw_c(t, n):
    beam(t, n, 2)
    fd(t, n)

def draw_e(t, n):
    beam(t, n, 2)
    beam(t, n, 1)
    fd(t, n)

def draw_f(t, n):
    beam(t, n, 2)
    beam(t, n, 1)
    skip(t, n)

def draw_h(t, n):
    post(t, 2*n)
    beam(t, n, 1)
    skip(t, n)
    post(t, 2*n)

def draw_l(t, n):
    post(t, 2*n)
    fd(t, n)

def draw_o(t, n):
    beam(t, n, 2)
    fd(t, n)
    post(t, 2*n)

def draw_(t, n):
    # draw a space?
    skip(t, n)


# the following is the code for the turtle typewriter.
# it uses some features we haven't seen yet.

def teleport(t, x, y):
    t.x = x
    t.y = y
    t.redraw()

def keypress(event):
    # this function gets called when the user presses a key
    try:
        # figure out which function to call, and call it
        func = eval('draw_' + event.char)
        func(bob, size)
        skip(bob, size/2)
    except NameError:
        print "I don't know how to draw an", event.char
    except SyntaxError:
        # this happens when the user presses return
        teleport(bob, -180, bob.y-size*3)

# create and position the turtle
size = 20
bob = Turtle(world)
bob.delay = 0.01
teleport(bob, -180, 150)

# tell world to call keypress when the user presses a key
world.bind('<Key>', keypress)

