Sha256: 8e7c31ea9e3d7729cb139a43a1da44ab3df49047892fd8df10a14654d29c1ed3

Contents?: true

Size: 866 Bytes

Versions: 4

Compression:

Stored size: 866 Bytes

Contents

require 'ruby-processing'

class ObjectOrientedTimer < Processing::App

  def setup
    background 0
    @timer = Timer.new(5000)
    @timer.start
  end
  
  def draw
    if @timer.is_finished?
      background rand(255)
      @timer.start
    end
  end
  
end


class Timer
  def initialize(total_time)
    @saved_time = nil         # When it started
    @total_time = total_time  # How long it should last
  end
  
  # When the timer starts it stores the current time in milliseconds
  def start
    @saved_time = $app.millis
  end
  
  # The method is_finished? returns true if 5 seconds have passed.
  # Most of the work of the timer is farmed out to this method.
  def is_finished?
    passed_time = $app.millis - @saved_time
    passed_time > @total_time
  end
  
  
end

ObjectOrientedTimer.new :title => "Object Oriented Timer", :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_10/05_object_oriented_timer.rb
ruby-processing-1.0.2 samples/learning_processing/chapter_10/05_object_oriented_timer.rb
ruby-processing-1.0.3 samples/learning_processing/chapter_10/05_object_oriented_timer.rb
ruby-processing-1.0.4 samples/learning_processing/chapter_10/05_object_oriented_timer.rb