如何让这款鞋子代码不受电脑影响呢?

1 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
Run Code Online (Sandbox Code Playgroud)

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

Pes*_*sto 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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

现在一个更有趣的策略是保留最后的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
Run Code Online (Sandbox Code Playgroud)