Sha256: f8d6d220f2c069615249c0630c2794689c0c97585727ebc8d953a6a123bc10c1

Contents?: true

Size: 960 Bytes

Versions: 4

Compression:

Stored size: 960 Bytes

Contents

require 'ruby-processing'

class BouncingColor < Processing::App

  def setup
    @c1, @c2 = 1.0, 255.0
    # Start by incrementing c1 and decrementing c2.
    @c1_dir, @c2_dir = 0.1, -0.1
  end
  
  def draw
    no_stroke
    
    # Draw rectangle on left
    fill @c1, 0, @c2
    rect 0, 0, 100, 200
    
    # Draw rectangle on right
    fill @c2, 0, @c1
    rect 100, 0, 100, 200
    
    # Adjust color values
    @c1 += @c1_dir
    @c2 += @c2_dir
    
    # Instead of reaching the edge of a window, these variables reach the "edge" of color:  
    # 0 for no color and 255 for full color.  
    # When this happens, just like with the bouncing ball, the direction is reversed.
    #
    # The following lines use Ranges and "unless" (which is the opposite of if).    
    @c1_dir *= -1 unless (0..255).include?(@c1)
    @c2_dir *= -1 unless (0..255).include?(@c1)
  end
  
end

BouncingColor.new :title => "Bouncing Color", :width => 200, :height => 200

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-processing-1.0.1 samples/learning_processing/chapter_05/07_bouncing_color.rb
ruby-processing-1.0.2 samples/learning_processing/chapter_05/07_bouncing_color.rb
ruby-processing-1.0.3 samples/learning_processing/chapter_05/07_bouncing_color.rb
ruby-processing-1.0.4 samples/learning_processing/chapter_05/07_bouncing_color.rb