Sha256: 1a65bc9d9f9d0e0ebce270a6ba505d1b2329c53e6dae9978bda31af40f1444ac

Contents?: true

Size: 850 Bytes

Versions: 4

Compression:

Stored size: 850 Bytes

Contents

require 'ruby-processing'

class CarClassAndCarVariable < Processing::App

  def setup
    # Initialize a car object
    @my_car = Car.new(self)
    rect_mode CENTER
  end
  
  def draw
  background 255
  
  # Operate the car object in draw
  # by calling object methods using the dots syntax.
  @my_car.move
  @my_car.display_car
  end
    
end


class Car # Define a class below the rest of the program.

  def initialize(app)
    @app = app
    @c = @app.color 175
    @xpos = @app.width/2
    @ypos = @app.height/2
    @xspeed = 1
  end

  def display_car # A new function of class Car
    @app.stroke 0
    @app.fill @c
    @app.rect @xpos, @ypos, 20, 10
  end

  def move
    @xpos = @xpos + @xspeed
    @xpos = 0 if @xpos > @app.width
  end
end

CarClassAndCarVariable.new :title => "Car Class And Car Variable", :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_08/01_car_class_and_car_variable.rb
ruby-processing-1.0.2 samples/learning_processing/chapter_08/01_car_class_and_car_variable.rb
ruby-processing-1.0.4 samples/learning_processing/chapter_08/01_car_class_and_car_variable.rb
ruby-processing-1.0.3 samples/learning_processing/chapter_08/01_car_class_and_car_variable.rb