1

这是我的代码的当前状态,虽然我得到了预期的效果,但它并没有以我需要的方式工作。因为程序处于无限循环中,很明显它会不断地产生两个背景渐变,每个循环会产生 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 做的鞋子可以做或不能做的事情。一些关于从哪里去的建议将不胜感激。

4

1 回答 1

5

Each of those ovals and backgrounds you draw is a separate item in memory, which means they bog it down after a while. If you just want to show the last frame you drew, then you need to clear the app each time:

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

That's not as cool as your original one (other than the fact that it'll run indefinitely), because you don't have the layering effect any more. In that case, we can let it run through a few times before clearing. In this example, it'll clear every sixth time through:

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

Now an even more interesting strategy would be to keep the last n passes and clear out the oldest, so that we always have, say, 6 layers on-screen (I find 6 to be a good cutoff point, but your opinion (and computer's performance!) may vary):

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
于 2009-06-12T21:27:51.310 回答