tags:

views:

127

answers:

1

This is the current state of my code, and, although I get the desired effect, it's not working in the manner in which I need it to. Because the program is in an infinite loop, obviously it will produce both background gradients on top of one another constantly, and 10 circles with each loop, and quickly they overproduce themselves and slow the program right down.

Here goes:

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

I've tried what I can think of, but I'm not overly familiar with Ruby's syntax, or what I can or can't do with shoes that I can do with Ruby. Some advice on where to go from here would be greatly appreciated.

+1  A: 

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
Pesto
Cheers for the help man - I decided to use "3" as a cutoff point, as, my system cant really take much more =PThanks SO much for this - I've learned a lot more about Ruby's syntax, as well as the counting method, which, I could work, but could never figure out how to operate it in the way I wanted to! lol.I owe you one =)Regards, G0DL355