import time

# Sample solution for 22c80 HW4, Spring 2009
#
#
# scroll text on a window
# if mode is 0, text always moves right except that when it 
#               reaches right edge of window it jumps back to left edge
# if mode is 1, text scrolls both left and right, reversing directions 
#               at window edges
#
def scrollBanner(text, mode, speed):
  true = (1==1)
  width = 300
  height = 20
  scrollWindow = makeEmptyPicture(width, height)

  approxTextWidth = len(text)*6
 
  nextPosition = 1
  increment = 1
 
  while (true):

    addRectFilled(scrollWindow, 1, 1, width, height, white)
    addText(scrollWindow, nextPosition, 16, text)
    # this extra 'if' was not required in HW4.  It uses a second copy 
    # of the text so that when some of the text starts disappearing at
    # the right edge of the window, it reappears immediately at the left edge
    if (mode == 0):
      addText(scrollWindow, nextPosition - width, 16, text)

    if ((nextPosition == width) and (mode == 0)):
      nextPosition = 1
    elif ((nextPosition == (width - approxTextWidth)) and (mode == 1)):
      nextPosition = nextPosition - 1
      increment = -1
    elif ((nextPosition == 1) and (mode == 1)):
      nextPosition = 2
      increment = 1
    else:
      nextPosition = nextPosition + increment

    repaint(scrollWindow)      
    time.sleep(.033 * (1.0/speed))

 