ruby - 我怎样才能使这双鞋的代码不至于使计算机陷入困境?

标签 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 能做的鞋子能做或不能做的事情。非常感谢关于从这里去哪里的一些建议。

最佳答案

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

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

关于ruby - 我怎样才能使这双鞋的代码不至于使计算机陷入困境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/988903/

相关文章:

ruby - 如何跳过不正确的 ssh 密码

ruby - 如何组织 Shoes Ruby 应用程序?

ruby - 如何在 OSX 10.7 上打包 ruby​​ shoes 应用程序

graphics - 构建轻量级跨平台文本编辑器的方法

sql - 从 sql RUBY/shoes GUI 获取 list_box

ruby - 我无法在 Ubuntu 14 中安装 ShoesRB。问题是 .run 或 .install 文件类型吗?

ruby - 如何查看来自 HTTParty get 请求的响应 cookie?

ruby-on-rails - 正确关联 Ruby on Rails

以换行符、空格或逗号分隔的 Ruby 正则表达式

mysql - rails MySql :Empty result in development mode