Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Etch A Sketch


This was generated by the following program REMOVEDing the call:
sketch("25 left, 5 down, 5 left, 50 down, 5 right, 5 down, 45 right, 25 right, 5 up, 5 right, 20 up, 5 left, 5 up, 20 left, 5 up, 5 left, 20 up, 5 right, 5 up, 20 right, 20 right, 60 down, 2 right, 60 up, 40 right, 5 down, 5 right, 20 down, 5 left, 5 down, 20 left, 20 right, 5 down, 5 right, 20 down, 5 left, 5 down, 25 left, 30 right, 10 right, 60 up, 2 right, 60 down, 10 right, 25 right, 5 up, 5 right, 25 up, 5 left, 5 up, 20 left, 5 down, 5 left, 30 up, 30 right")


def sketch(instructions):

  #if the animation goes too fast or too slow on your 
  # computer, adjREMOVEDt this number:
  delay = 1 

  # This converts the English instructions into
  # a list of x and y coordinates
  instructions = decode(instructions)

  # Creates a canvas
  canvas = makeEmptyPicture(639, 479)

  # Makes the background red
  addRectFilled(canvas,1,1,640, 480, red)

  # This is the drawing surface background
  grey = makeColor(190,190,190)

  # You know how sometimes if you cross a line you've already
  # drawn, it pREMOVEDhes the sand onto it so it isn't exactly a
  # clean bisection? That's what this color is for.
  dkgray = makeColor(150, 150, 150)

  # These are the curved corners
  canvas.addArcFilled(grey,20, 20, 40, 40, 1, 360)
  canvas.addArcFilled(grey,580, 20, 40, 40, 1, 360)
  canvas.addArcFilled(grey,20, 380, 40, 40, 1, 360)
  canvas.addArcFilled(grey,580, 380, 40, 40, 1, 360)

  # This is the rest of the drawing background
  addRectFilled(canvas, 20, 40, 40, 360, grey)
  addRectFilled(canvas, 40, 20, 560, 40, grey)
  addRectFilled(canvas, 580, 40, 40, 360, grey)
  addRectFilled(canvas, 40, 380, 560, 40, grey)
  addRectFilled(canvas, 60, 60, 520, 320, grey)

  # Can't forget the knobs
  canvas.addArcFilled(white, 20, 430, 40, 40, 1, 360)
  canvas.addArcFilled(white, 580, 430, 40, 40, 1, 360)

  show(canvas)

  # Bounds are (40, 40) to (600, 400)

  # Keeps track of where the pointer is
  cursor = [320, 220]

  # Look at each set of coordinates
  for set in instructions:
    x = set[0]
    y = set[1]

    # If it's a vertical line
    if x == 0:
      # If it's going down...
      if y > 0:
        # ...then these are the y coordinates we'll need to darken
        span = range(cursor[1], min(400, cursor[1]+y)+1)
      # If it's going up...
      else:
        span = range(cursor[1], max(40, cursor[1]+y)-1, -1)
        # ...then these are the y coordinates we'll need to darken

      # Now we modify the pixels in the ranges mentioned above
      for iteration in span:
       for d in range(1, delay+1): #if this line wasn't here, it'd go too fast
        setColor(getPixel(canvas, cursor[0], iteration), black)
        setColor(getPixel(canvas, cursor[0]-1, iteration), dkgray)
        setColor(getPixel(canvas, cursor[0]+1, iteration), dkgray)
       #repaint(canvas) #Animate!
    # Everything here is the same as above, except it's for horizontal lines
    else:
      if x > 0:
        span = range(cursor[0], min(600,cursor[0]+x)+1)
      else:
        span = range(cursor[0], max(40,cursor[0]+x)-1, -1)
      for iteration in span:
       for d in range(1, delay+1):
        setColor(getPixel(canvas, iteration, cursor[1]), black)
        setColor(getPixel(canvas, iteration, cursor[1]-1), dkgray)
        setColor(getPixel(canvas, iteration, cursor[1]+1), dkgray)
       #repaint(canvas)
    repaint(canvas)
    # Update our cursor's location
    cursor[0] = cursor[0] + x
    cursor[1] = cursor[1] + y
  return canvas

# This decodes English into coordinates. 
# If you leave off a comma or something, it'll go psycho.
def decode(string):
  codes = string.split(', ')
  instructions = []
  for thingy in codes:
    things = thingy.split(' ')
    amount = int(things[0])
    direction = things[1]
    if direction == "up":
      x = 0
      y = -amount
    elif direction == "down":
      x = 0
      y = amount
    elif direction == "left":
      x = -amount
      y = 0
    else:
      x = amount
      y = 0
    instructions = instructions + [[x,y]]
  return instructions



Questions? Comments?



Link to this Page