
Shoes.app :width => 500, :height => 500 do
  howmany = 0

  @flashy = false

  def randomcolor
    rgb(rand(256), rand(256), rand(256))
  end

  def newcentersphere(left, top)
    @discs.push({ :left => left,
                  :top => top,
                  :moveleft => rand(50)-25,
                  :movetop => rand(50)-25,
                  :size => rand(40) + 40,
                  :outercolor => randomcolor,
                  :innercolor => randomcolor
                })
  end


  def drawbackground
    background gradient(@backgroundcolors[0], @backgroundcolors[1])
  end

  def drawcentersphere(centersphere)
    left = centersphere[:left]
    top = centersphere[:top]
    size = centersphere[:size]
    nostroke
    fill centersphere[:outercolor]
    oval(:left => left, :top => top, :radius => size, :center => true)
    nostroke
    fill centersphere[:innercolor]
    oval(:left => left, :top => top, :radius => size/2, :center => true)
  end

  def randomize_colors
    debug "randomize_colors"
    @backgroundcolors = [randomcolor, randomcolor]
    @discs.each do | centersphere |
      centersphere[:outercolor] = randomcolor
      centersphere[:innercolor] = randomcolor
    end
  end

  def randomize_discs
    @discs.each do | centersphere |
      centersphere[:size] = rand(40) + 40
      centersphere[:moveleft] = rand(50) - 25
      centersphere[:movetop] = rand(50) - 25
    end
  end


  @discs = []
  howmany.times do
    newcentersphere(rand(480), rand(480))
  end

  @backgroundcolors = [randomcolor, randomcolor]
    
  keypress do | key |
    case key
      when " "
      @flashy = (not @flashy)
      when "\n"
      newcentersphere(rand(width), rand(height))
      randomize_colors
      randomize_discs
      when :escape
      exit
    end
  end


  click do | button, left, top |
    if button == 3
      @flashy = (not @flashy)
    end
    if button == 1
      newcentersphere(left, top)
      randomize_discs
      randomize_colors
    end
  end


  animate do
    clear do
      drawbackground
      @discs.each do | centersphere |
        drawcentersphere(centersphere)
        centersphere[:left] = centersphere[:left] + centersphere[:moveleft]
        unless centersphere[:left] > 0 and centersphere[:left] < width
          centersphere[:left] = width - centersphere[:left] % width
          centersphere[:moveleft] = 0 - centersphere[:moveleft]
        end
        
        centersphere[:top] = centersphere[:top] + centersphere[:movetop]
        unless centersphere[:top] > 0 and centersphere[:top] < height
          centersphere[:top] = height - centersphere[:top] % height
          centersphere[:movetop] = 0 - centersphere[:movetop]
        end
      end
      if @flashy
        randomize_colors
      end

    end
  end
end
