Skip to content

Commit 13e0d01

Browse files
author
monkstone
committed
1st update as ruby-processing
1 parent 25b2ad2 commit 13e0d01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+981
-1115
lines changed

chapter_06/Rakefile~

Lines changed: 0 additions & 30 deletions
This file was deleted.

chapter_08/01_car_class_and_car_variable.rb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
require 'ruby-processing'
1+
# Introducing the Processing::Proxy module, to access sketch methods/variables
2+
3+
4+
# require 'car'
25

36
class CarClassAndCarVariable < Processing::App
47

58
def setup
69
# Initialize a car object
7-
@my_car = Car.new(self)
10+
@my_car = Car.new
811
rect_mode CENTER
912
end
1013

@@ -20,25 +23,35 @@ def draw
2023
end
2124

2225

23-
class Car # Define a class below the rest of the program.
26+
# Define a class below the rest of the program.
27+
# Or in another file eg car.rb then use require 'car' to include in the sketch
28+
#
29+
# 'include Processing::Proxy' makes methods and variables available
30+
# to the Car (similar to processings java inner class) except that
31+
# width and height are excluded, hence global $app is reqd to access them
2432

25-
def initialize(app)
26-
@app = app
27-
@c = @app.color 175
28-
@xpos = @app.width/2
29-
@ypos = @app.height/2
33+
class Car
34+
include Processing::Proxy
35+
36+
attr_reader :width, :height
37+
38+
def initialize
39+
@width, @height = $app.width, $app.height
40+
@c = color 175
41+
@xpos = width / 2
42+
@ypos = height / 2
3043
@xspeed = 1
3144
end
3245

3346
def display_car # A new function of class Car
34-
@app.stroke 0
35-
@app.fill @c
36-
@app.rect @xpos, @ypos, 20, 10
47+
stroke 0
48+
fill @c
49+
rect @xpos, @ypos, 20, 10
3750
end
3851

3952
def move
4053
@xpos = @xpos + @xspeed
41-
@xpos = 0 if @xpos > @app.width
54+
@xpos = 0 if @xpos > width
4255
end
4356
end
4457

chapter_08/02_two_car_objects.rb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
require 'ruby-processing'
1+
# Introducing the ruby-processing control panel
22

33
class TwoCarObjects < Processing::App
44
load_library :control_panel
5-
5+
attr_reader :panel, :hide
6+
67
def setup
8+
@hide = false
79
control_panel do |c|
10+
c.look_feel "Metal"
811
c.slider :red_car_speed, -5..15
912
c.slider :blue_car_speed, -5..15
13+
@panel = c
1014
end
1115

1216
# Initialize car objects
13-
@red_car = Car.new(self, color(255,0,0), 0, 100)
14-
@blue_car = Car.new(self, color(0,0,255), 0, 10)
17+
@red_car = Car.new(color(255,0,0), 0, 100)
18+
@blue_car = Car.new(color(0,0,255), 0, 10)
1519
@red_car_speed = 1
1620
@blue_car_speed = 2
1721
rect_mode CENTER
1822
end
1923

2024
def draw
25+
if !hide # only set once
26+
panel.visible = true
27+
@hide = true
28+
end
29+
2130
background 255
2231

2332
# Operate the car object in draw
@@ -32,27 +41,29 @@ def draw
3241

3342

3443
class Car # Define a class below the rest of the program.
35-
attr_accessor :temp_color, :temp_x_position, :temp_y_position, :temp_x_speed
44+
include Processing::Proxy
45+
46+
attr_accessor :width, :temp_color, :temp_x_position, :temp_y_position, :temp_x_speed
3647
LENGTH = 20
37-
HEIGHT = 10
48+
CHEIGHT = 10
3849

39-
def initialize(app, temp_color, temp_x_position, temp_y_position)
40-
@app = app
50+
def initialize(temp_color, temp_x_position, temp_y_position)
51+
@width = $app.width
4152
@c = temp_color
4253
@xpos = temp_x_position
4354
@ypos = temp_y_position
4455
end
4556

4657
def display_car # A new function of class Car
47-
@app.stroke 0
48-
@app.fill @c
49-
@app.rect @xpos, @ypos, LENGTH, HEIGHT
58+
stroke 0
59+
fill @c
60+
rect @xpos, @ypos, LENGTH, CHEIGHT
5061
end
5162

5263
def move(speed)
5364
@xpos += speed
54-
@xpos = 0 if @xpos > @app.width + LENGTH/2
55-
@xpos = @app.width if @xpos < -LENGTH/2
65+
@xpos = 0 if @xpos > width + LENGTH/2
66+
@xpos = width if @xpos < -LENGTH/2
5667
end
5768
end
5869

chapter_08/03_object_oriented_zoog.rb

Lines changed: 25 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,35 @@
1-
require 'ruby-processing'
1+
require 'zoog' # zoog class is defined in zoog.rb
22

3-
class ObjectOrientedZoog < Processing::App
4-
load_library "control_panel"
3+
load_library :control_panel
4+
attr_reader :panel, :hide
55

6-
def setup
7-
smooth
8-
control_panel do |c|
9-
c.slider :zoog_jiggle_factor, 0..width/10
10-
end
11-
12-
# Initalize zoog
13-
@zoog = Zoog.new(self, 100, 125, 60, 60, 16)
14-
@zoog_jiggle_factor = 10
15-
16-
rect_mode CENTER
17-
ellipse_mode CENTER
6+
def setup
7+
size 200, 200
8+
smooth 4
9+
control_panel do |c|
10+
c.look_feel "Metal"
11+
c.slider :zoog_jiggle_factor, 0..width / 10, 5.0
12+
@panel = c
1813
end
1914

20-
def draw
21-
background 255
22-
23-
# mouse_x position determines speed factor
24-
# factor = constrain(mouse_x/10, 0, 5)
25-
26-
@zoog.jiggle(@zoog_jiggle_factor)
27-
@zoog.display
28-
end
15+
# Initalize zoog
16+
@zoog = Zoog.new(100, 125, 60, 60, 16)
17+
2918

19+
rect_mode CENTER
20+
ellipse_mode CENTER
3021
end
3122

32-
class Zoog
33-
attr_accessor :temp_x_position, :temp_y_position, :temp_width, :temp_height, :temp_eye_size
34-
35-
def initialize(app, temp_x_position, temp_y_position, temp_zoog_width, temp_zoog_height, temp_eye_size)
36-
@app = app
37-
@x, @y = temp_x_position, temp_y_position
38-
@w, @h = temp_zoog_width, temp_zoog_height
39-
@eye_size = temp_eye_size
23+
def draw
24+
if !hide # only set once
25+
panel.visible = true
26+
@hide = true
4027
end
28+
background 255
4129

42-
# Move zoog
43-
def jiggle(speed)
44-
@x = @x + speed * @app.random(-1,1)
45-
@y = @y + speed * @app.random(-1,1)
46-
47-
@x = @app.constrain(@x, 0, @app.width)
48-
@y = @app.constrain(@y, 0, @app.height)
49-
end
30+
# mouse_x position determines speed factor
31+
# factor = constrain(mouse_x / 10, 0, 5)
5032

51-
def display
52-
# Draw zoog's arms
53-
(@y - @h/3).step((@y + @h/2), 10) do |i|
54-
@app.stroke 0
55-
@app.line @x-@w/4, i, @x+@w/4, i
56-
end
57-
58-
# Draw zoog's body
59-
@app.stroke 0
60-
@app.fill 255
61-
@app.rect @x, @y, @w/6, @h
62-
63-
# Draw zoog's head
64-
@app.stroke 0
65-
@app.fill 255
66-
@app.ellipse @x, @y-@h, @w, @h
67-
68-
# Draw zoog's eyes
69-
@app.fill 0
70-
@app.ellipse @x-@w/3, @y-@h, @eye_size, @eye_size*2
71-
@app.ellipse @x+@w/3, @y-@h, @eye_size, @eye_size*2
72-
73-
# Draw zoog's legs
74-
@app.stroke 0
75-
@app.line @x-@w/12, @y+@h/2, @x-@w/4, @y+@h/2+10
76-
@app.line @x+@w/12, @y+@h/2, @x+@w/4, @y+@h/2+10
77-
end
78-
79-
end
80-
81-
ObjectOrientedZoog.new :title => "Object Oriented Zoog", :width => 200, :height => 200
33+
@zoog.jiggle(@zoog_jiggle_factor)
34+
@zoog.display
35+
end

chapter_08/zoog.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Zoog
2+
include Processing::Proxy
3+
attr_accessor :width, :height
4+
attr_accessor :temp_x_position, :temp_y_position, :temp_width, :temp_height, :temp_eye_size
5+
6+
def initialize(temp_x_position, temp_y_position, temp_zoog_width, temp_zoog_height, temp_eye_size)
7+
@width, @height = $app.width, $app.height
8+
@x, @y = temp_x_position, temp_y_position
9+
@w, @h = temp_zoog_width, temp_zoog_height
10+
@eye_size = temp_eye_size
11+
end
12+
13+
# Move zoog
14+
def jiggle(speed)
15+
@x = @x + speed * rand(-1.0 .. 1)
16+
@y = @y + speed * rand(-1.0 .. 1)
17+
18+
@x = constrain(@x, 0, width)
19+
@y = constrain(@y, 0, height)
20+
end
21+
22+
def display
23+
# Draw zoog's arms
24+
(@y - @h/3).step((@y + @h/2), 10) do |i|
25+
stroke 0
26+
line @x-@w/4, i, @x+@w/4, i
27+
end
28+
29+
# Draw zoog's body
30+
stroke 0
31+
fill 255
32+
rect @x, @y, @w/6, @h
33+
34+
# Draw zoog's head
35+
stroke 0
36+
fill 255
37+
ellipse @x, @y-@h, @w, @h
38+
39+
# Draw zoog's eyes
40+
fill 0
41+
ellipse @x-@w/3, @y-@h, @eye_size, @eye_size*2
42+
ellipse @x+@w/3, @y-@h, @eye_size, @eye_size*2
43+
44+
# Draw zoog's legs
45+
stroke 0
46+
line @x-@w/12, @y+@h/2, @x-@w/4, @y+@h/2+10
47+
line @x+@w/12, @y+@h/2, @x+@w/4, @y+@h/2+10
48+
end
49+
50+
end
51+

chapter_10/01_catcher.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
require 'ruby-processing'
2-
31
class CatcherSketch < Processing::App
42
def setup
53
@catcher = Catcher.new(32)
6-
smooth
4+
smooth 4
75
end
86

97
def draw
108
background 255
11-
@catcher.set_location mouse_x, mouse_y
9+
@catcher.set_location
1210
@catcher.display
1311
end
1412
end
1513

1614

1715
class Catcher
16+
include Processing::Proxy
17+
1818
def initialize(temp_r)
1919
@r = temp_r
2020
@x, @y = 0, 0
2121
end
2222

23-
def set_location(temp_x, temp_y)
24-
@x, @y = temp_x, temp_y
23+
def set_location
24+
@x, @y = mouse_x, mouse_y
2525
end
2626

27-
# In Ruby-Processing, you can always find the sketch in $app.
2827
def display
29-
$app.stroke 0
30-
$app.fill 175
31-
$app.ellipse @x, @y, @r*2, @r*2
28+
stroke 0
29+
fill 175
30+
ellipse @x, @y, @r*2, @r*2
3231
end
3332
end
3433

0 commit comments

Comments
 (0)