"""
Program that draws spiky squares using turtle graphics

CS 21, Fall 2016
"""

from graphics import *
from turtlegraphics import *

def main():
  width = 600
  height = width
  gw = GraphWin("Spiky Sides", 600, 600)
  gw.setBackground("white")
  t = Turtle(gw)

  # Uncomment line below to turn off animation
  # t.animate(False)

  t.jump(Point(width/4.0, 3*height/4.0))
  depth = getDepth()

  # Draw spiky sides around a square
  for i in range(4):
    spikes(t, depth, width/2.0)
    t.turnLeft(90)

  gw.getMouse()

def spikes(turtle, depth, distance):
  """
  Draw a spiky side

  Parameters: turtle - a Turtle object
              depth - depth of recursion
              distance - distance between start and end points
  Returns: None
  """
  if depth == 0:
    turtle.go(distance)
  else:
    spikes(turtle, depth-1, distance/2.0)
    drawSpike(turtle, distance/5.0)
    spikes(turtle, depth-1, distance/2.0)

def drawSpike(turtle, spikeLength):
  """
  Draw a spike perpendicular to the turtle's current
  orientation, then re-orient the turtle

  Parameters: turtle - a Turtle object
              spikeLength - length of the spike
  """
  turtle.turnRight(90)
  turtle.go(spikeLength)
  turtle.turnRight(180)
  turtle.go(spikeLength)
  turtle.turnRight(90)

def getDepth():
  """
  Get the user's desired recursion depth from raw_input

  Parameters: n/a
  Returns: the recursion depth
  """
  while True:
    depth = raw_input("Depth: ")
    if depth.isdigit():
      depth = int(depth)
      if depth >= 0:
        return depth
      else:
        print("Please enter a positive integer")
    else:
      print("Please enter an integer")

main()