如何让这款鞋子的代码陷入困境?

时间:2009-06-12 20:34:00

标签: ruby shoes

这是我的代码的当前状态,虽然我得到了预期的效果,但它并没有按我需要的方式工作。因为程序处于无限循环中,显然它会不断地产生两个背景渐变,每个循环产生10个圆圈,很快它们就会过度生成并使程序慢下来。

这里是:

Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do
            i = 0
# Animation loop
    animate ( 24 ) do |i|
# Variables For Randomized Colours
            randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
            randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
            randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
            randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
            background randomCol..randomCol2
            fill    randomCol3
            stroke  randomCol4
            strokewidth     ( 0..5 ).rand
# Generate 10 circles per loop cycle
            10.times{
            i += 1
            oval :left => ( -5..self.width ).rand,
            :top => ( -5..self.height ).rand,
            :radius => ( 1..100 ).rand
            } end
        end

我已经尝试过我能想到的东西,但是我并不过分熟悉Ruby的语法,或者我可以用或不能用Ruby做的鞋子。关于从哪里出发的一些建议将不胜感激。

1 个答案:

答案 0 :(得分:5)

您绘制的每个椭圆和背景都是内存中的单独项目,这意味着它们会在一段时间后陷入困境。如果您只想显示您绘制的最后一帧,则每次都需要清除该应用程序:

Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do

  # Animation loop
  animate ( 24 ) do |i|
    app.clear

    # Variables For Randomized Colours
    randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )

    background randomCol..randomCol2
    fill    randomCol3
    stroke  randomCol4
    strokewidth ( 0..5 ).rand

    # Generate 10 circles per loop cycle
    10.times do |i|
      i += 1
      oval :left => ( -5..self.width ).rand,
        :top => ( -5..self.height ).rand,
        :radius => ( 1..100 ).rand
    end
  end
end

这不像你原来的那么酷(除了它会无限期地运行),因为你不再具有分层效果了。在这种情况下,我们可以让它在清除之前运行几次。在这个例子中,它将每隔六次清除:

Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do

  # Animation loop
  animate ( 24 ) do |i|
    app.clear if (i % 6 == 0)

    # Variables For Randomized Colours
    randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )

    background randomCol..randomCol2
    fill    randomCol3
    stroke  randomCol4
    strokewidth ( 0..5 ).rand

    # Generate 10 circles per loop cycle
    10.times do |i|
      i += 1
      oval :left => ( -5..self.width ).rand,
        :top => ( -5..self.height ).rand,
        :radius => ( 1..100 ).rand
    end
  end
end

现在一个更有趣的策略是保留最后n次传球并清除最旧的传球,这样我们总是在屏幕上有6层(我发现6是一个很好的截止点,但您的意见(以及计算机的性能!)可能会有所不同):

Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do
  n = 6
  @layers = []
  n.times { @layers << [] }
  # Animation loop
  animate ( 24 ) do |i|
    oldest = i % n
    # Clear out oldest frame
    @layers[oldest].each {|x| x.remove}
    @layers[oldest] = []

    # Variables For Randomized Colours
    randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
    randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )

    @layers[oldest] << background(randomCol..randomCol2)
    fill    randomCol3
    stroke  randomCol4
    strokewidth ( 0..5 ).rand

    # Generate 10 circles per loop cycle
    10.times do |i|
      @layers[oldest] << oval (:left => ( -5..self.width ).rand,
        :top => ( -5..self.height ).rand,
        :radius => ( 1..100 ).rand)
      end
  end
end