ruby-processing 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +8 -0
- data/lib/patches/JRubyApplet.diff +0 -0
- data/lib/patches/PATCHES.txt +0 -0
- data/lib/patches/PApplet.diff +0 -0
- data/lib/ruby-processing.rb +1 -1
- data/lib/ruby-processing/app.rb +20 -1
- data/lib/ruby-processing/exporters/base_exporter.rb +1 -1
- data/lib/ruby-processing/exporters/creator.rb +1 -2
- data/lib/ruby-processing/runner.rb +4 -3
- data/lib/ruby-processing/runners/watch.rb +2 -2
- data/lib/templates/applet/index.html.erb +1 -1
- metadata +2 -145
- data/samples/learning_processing/ABOUT +0 -11
- data/samples/learning_processing/chapter_01/1_stroke_and_fill.rb +0 -18
- data/samples/learning_processing/chapter_01/2_nofill.rb +0 -20
- data/samples/learning_processing/chapter_01/3_rgb_color.rb +0 -19
- data/samples/learning_processing/chapter_01/4_alpha_transparency.rb +0 -42
- data/samples/learning_processing/chapter_01/5_zoog.rb +0 -24
- data/samples/learning_processing/chapter_02/1_zoog_again.rb +0 -41
- data/samples/learning_processing/chapter_03/1_zoog_as_dynamic_sketch.rb +0 -44
- data/samples/learning_processing/chapter_03/2_mouse_x_and_mouse_y.rb +0 -26
- data/samples/learning_processing/chapter_03/3_zoog_as_dynamic_sketch_with_variation.rb +0 -41
- data/samples/learning_processing/chapter_03/4_drawing_a_continuous_line.rb +0 -18
- data/samples/learning_processing/chapter_03/5_mouse_pressed_and_key_pressed.rb +0 -29
- data/samples/learning_processing/chapter_03/6_interactive_zoog.rb +0 -42
- data/samples/learning_processing/chapter_04/1_variable_declaration.rb +0 -24
- data/samples/learning_processing/chapter_04/2_using_variables.rb +0 -27
- data/samples/learning_processing/chapter_04/3_varying_variables.rb +0 -27
- data/samples/learning_processing/chapter_04/4_many_variables.rb +0 -38
- data/samples/learning_processing/chapter_04/5_using_system_variables.rb +0 -28
- data/samples/learning_processing/chapter_04/6_ellipse_with_variables.rb +0 -24
- data/samples/learning_processing/chapter_04/7_filling_variables_with_random_values.rb +0 -27
- data/samples/learning_processing/chapter_04/8_variable_zoog.rb +0 -50
- data/samples/learning_processing/chapter_05/01_conditionals.rb +0 -31
- data/samples/learning_processing/chapter_05/02_more_conditionals.rb +0 -38
- data/samples/learning_processing/chapter_05/03_rollovers.rb +0 -34
- data/samples/learning_processing/chapter_05/04_hold_down_the_button.rb +0 -33
- data/samples/learning_processing/chapter_05/05_button_as_switch.rb +0 -39
- data/samples/learning_processing/chapter_05/06_bouncing_ball.rb +0 -33
- data/samples/learning_processing/chapter_05/07_bouncing_color.rb +0 -37
- data/samples/learning_processing/chapter_05/08_square_following_edge.rb +0 -59
- data/samples/learning_processing/chapter_05/09_simple_gravity.rb +0 -43
- data/samples/learning_processing/chapter_05/10_zoog_and_conditionals.rb +0 -53
- data/samples/learning_processing/chapter_06/01_many_lines.rb +0 -29
- data/samples/learning_processing/chapter_06/02_many_lines_with_variables.rb +0 -53
- data/samples/learning_processing/chapter_06/03_while_loop.rb +0 -30
- data/samples/learning_processing/chapter_06/04_infinite_loop.rb +0 -25
- data/samples/learning_processing/chapter_06/05_another_infinite_loop.rb +0 -33
- data/samples/learning_processing/chapter_06/06_legs_with_a_for_loop.rb +0 -41
- data/samples/learning_processing/chapter_06/07_local_variables.rb +0 -39
- data/samples/learning_processing/chapter_06/08_lines_one_at_a_time.rb +0 -30
- data/samples/learning_processing/chapter_06/09_simple_while_loop_with_interactivity.rb +0 -41
- data/samples/learning_processing/chapter_06/10_zoog_with_arms.rb +0 -56
- data/samples/learning_processing/chapter_06/11_multiple_zoogs.rb +0 -46
- data/samples/learning_processing/chapter_07/01_defining_a_function.rb +0 -22
- data/samples/learning_processing/chapter_07/02_calling_a_function.rb +0 -22
- data/samples/learning_processing/chapter_07/03_bouncing_ball_with_functions.rb +0 -49
- data/samples/learning_processing/chapter_07/04_function_returns_distance.rb +0 -48
- data/samples/learning_processing/chapter_07/05_zoog_with_functions.rb +0 -71
- data/samples/learning_processing/chapter_08/01_car_class_and_car_variable.rb +0 -45
- data/samples/learning_processing/chapter_08/02_two_car_objects.rb +0 -60
- data/samples/learning_processing/chapter_08/03_object_oriented_zoog.rb +0 -81
- data/samples/learning_processing/chapter_10/01_catcher.rb +0 -35
- data/samples/learning_processing/chapter_10/02_bouncing_ball.rb +0 -44
- data/samples/learning_processing/chapter_10/03_bouncing_balls_with_intersection.rb +0 -69
- data/samples/learning_processing/chapter_10/04_a_timer.rb +0 -24
- data/samples/learning_processing/chapter_10/05_object_oriented_timer.rb +0 -42
- data/samples/learning_processing/chapter_10/06_simple_raindrop.rb +0 -23
- data/samples/learning_processing/chapter_10/07_drops_one_at_a_time.rb +0 -63
- data/samples/learning_processing/chapter_13/02_random_number_distribution.rb +0 -24
- data/samples/learning_processing/chapter_13/03_probabilities.rb +0 -33
- data/samples/learning_processing/chapter_13/04_perlin_noise.rb +0 -22
- data/samples/learning_processing/chapter_13/06_oscillation.rb +0 -29
- data/samples/learning_processing/chapter_13/07_wave.rb +0 -31
- data/samples/learning_processing/chapter_13/08_recursion.rb +0 -30
- data/samples/learning_processing/chapter_13/09_two_dimensional_array.rb +0 -21
- data/samples/learning_processing/chapter_13/10_two_dimensional_array_with_objects.rb +0 -54
- data/samples/learning_processing/chapter_14/01_growing_rectangle.rb +0 -26
- data/samples/learning_processing/chapter_14/02_multiple_translations.rb +0 -37
- data/samples/learning_processing/chapter_14/03_depth_rectangle.rb +0 -29
- data/samples/learning_processing/chapter_14/04_pyramid_using_beingshape.rb +0 -55
- data/samples/learning_processing/chapter_14/05_rotate_rectangle_around_center.rb +0 -28
- data/samples/learning_processing/chapter_14/06_rotate_z.rb +0 -24
- data/samples/learning_processing/chapter_14/07_rotate_x.rb +0 -24
- data/samples/learning_processing/chapter_14/08_rotate_y.rb +0 -24
- data/samples/learning_processing/chapter_14/09_rotate_all.rb +0 -24
- data/samples/learning_processing/chapter_14/10_draw_pyramid.rb +0 -59
- data/samples/learning_processing/chapter_14/12_rotating_one_square.rb +0 -25
- data/samples/learning_processing/chapter_14/13_rotating_another_square.rb +0 -25
- data/samples/learning_processing/chapter_14/14_rotating_both_squares.rb +0 -43
- data/samples/learning_processing/chapter_14/15_rotating_many_things.rb +0 -56
- data/samples/learning_processing/chapter_14/16_simple_solar_system.rb +0 -53
- data/samples/learning_processing/chapter_14/17_nested_push_and_pop.rb +0 -50
- data/samples/learning_processing/chapter_14/18_object_oriented_solar_system.rb +0 -69
- data/samples/learning_processing/chapter_15/01_hello_world_image.rb +0 -20
- data/samples/learning_processing/chapter_15/02_image_sprite.rb +0 -29
- data/samples/learning_processing/chapter_15/03_swapping_images.rb +0 -25
- data/samples/learning_processing/chapter_15/04_image_sequence.rb +0 -22
- data/samples/learning_processing/chapter_15/05_setting_pixels.rb +0 -19
- data/samples/learning_processing/chapter_15/06_pixels_2d.rb +0 -29
- data/samples/learning_processing/chapter_15/07_image_pixels.rb +0 -40
- data/samples/learning_processing/chapter_15/08_image_brightness.rb +0 -29
- data/samples/learning_processing/chapter_15/09_flashlight_effect.rb +0 -42
- data/samples/learning_processing/chapter_15/10_brightness_threshold.rb +0 -33
- data/samples/learning_processing/chapter_15/11_brightness_threshold_with_filter.rb +0 -20
- data/samples/learning_processing/chapter_15/12_pixel_neighbour_differences.rb +0 -43
- data/samples/learning_processing/chapter_15/14_pointillism.rb +0 -34
- data/samples/learning_processing/chapter_15/15_2d_image_mapped_to_3d.rb +0 -43
- data/samples/learning_processing/chapter_15/data/animal0.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal1.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal2.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal3.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal4.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal5.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal6.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/face.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/mysummervacation.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/sunflower.jpg +0 -0
- data/samples/learning_processing/chapter_16/01_display_video.rb +0 -25
- data/samples/learning_processing/chapter_16/02_manipulate_video_image.rb +0 -24
- data/samples/learning_processing/chapter_16/03_adjust_video_brightness.rb +0 -49
- data/samples/learning_processing/chapter_17/01_simple_displaying_text.rb +0 -14
- data/samples/learning_processing/chapter_17/02_text_align.rb +0 -21
- data/samples/learning_processing/chapter_17/03_scrolling_headlines.rb +0 -35
- data/samples/learning_processing/chapter_17/04_text_mirror.rb +0 -68
- data/samples/learning_processing/chapter_17/05_rotating_text.rb +0 -22
- data/samples/learning_processing/chapter_17/06_text_breaking_up.rb +0 -73
- data/samples/learning_processing/chapter_17/07_boxes_along_a_curve.rb +0 -49
- data/samples/learning_processing/chapter_17/08_characters_along_a_curve.rb +0 -51
- data/samples/learning_processing/chapter_17/data/ArialMT-16.vlw +0 -0
- data/samples/learning_processing/chapter_17/data/Courier-Bold-20.vlw +0 -0
- data/samples/learning_processing/chapter_18/01_user_input.rb +0 -39
- data/samples/learning_processing/chapter_18/02_graphing_comma_separated_numbers_from_a_text_file.rb +0 -28
- data/samples/learning_processing/chapter_18/03_creating_object_from_a_text_file.rb +0 -64
- data/samples/learning_processing/chapter_18/data/data-1.txt +0 -1
- data/samples/learning_processing/chapter_18/data/data-2.txt +0 -10
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
# NEW FEATURE #1: Zoog will rise from below the screen and fly off into space.
|
4
|
-
# NEW FEATURE #2: Zoog's eyes will be colored randomly as Zoog moves.
|
5
|
-
class VariableZoog < Processing::App
|
6
|
-
|
7
|
-
def setup
|
8
|
-
# Since in Ruby-Processing you don't ever call the size method,
|
9
|
-
# you can initialize zoog_x and zoog_y straight off.
|
10
|
-
@zoog_x, @zoog_y = width/2, height+100
|
11
|
-
smooth
|
12
|
-
end
|
13
|
-
|
14
|
-
def draw
|
15
|
-
background 255 # Draw a white background
|
16
|
-
|
17
|
-
# Set ellipses and rects to CENTER mode
|
18
|
-
ellipse_mode CENTER
|
19
|
-
rect_mode CENTER
|
20
|
-
|
21
|
-
# Draw Zoog's body
|
22
|
-
stroke 0
|
23
|
-
fill 150
|
24
|
-
# Feature #1: zoog_x and zoog_y are used for the location
|
25
|
-
rect @zoog_x, @zoog_y, 20, 100
|
26
|
-
|
27
|
-
# Draw Zoog's head
|
28
|
-
stroke 0
|
29
|
-
fill 255
|
30
|
-
ellipse @zoog_x, @zoog_y-30, 60, 60
|
31
|
-
|
32
|
-
# Draw Zoog's eyes
|
33
|
-
# Feature #2: Zoog's eyes are colored with random values.
|
34
|
-
fill random(255), random(255), random(255)
|
35
|
-
ellipse @zoog_x-19, @zoog_y-30, 16, 32
|
36
|
-
ellipse @zoog_x+19, @zoog_y-30, 16, 32
|
37
|
-
|
38
|
-
# Draw Zoog's legs
|
39
|
-
stroke 150
|
40
|
-
line @zoog_x-10, @zoog_y+50, @zoog_x-10, height
|
41
|
-
line @zoog_x+10, @zoog_y+50, @zoog_x+10, height
|
42
|
-
|
43
|
-
# Zoog moves up
|
44
|
-
@zoog_y -= 1
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
VariableZoog.new :title => "Variable Zoog", :width => 200, :height => 200
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class Conditionals < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
# Variables
|
7
|
-
@r, @g, @b = 150, 0, 0
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
#Draw stuff
|
12
|
-
background @r, @g, @b
|
13
|
-
stroke 255
|
14
|
-
line width/2, 0, width/2, height
|
15
|
-
|
16
|
-
# The following checks use the "ternary operator" which is a compact way
|
17
|
-
# of saying, "if this is true ? do this : otherwise this"
|
18
|
-
|
19
|
-
# If the mouse is on the right side of the screen is equivalent to
|
20
|
-
# "if mouse_x is greater than width divided by 2."
|
21
|
-
(mouse_x > width/2) ? @r += 1 : @r -=1
|
22
|
-
|
23
|
-
# If r is greater than 255, set it back to 255.
|
24
|
-
# If r is less than 0, set it back to 0.
|
25
|
-
@r = 255 if @r > 255
|
26
|
-
@r = 0 if @r < 0
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
Conditionals.new :title => "Conditionals", :width => 200, :height => 200
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class MoreConditionals < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
# Three variables for the background color.
|
7
|
-
@r, @g, @b = 0, 0, 0
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
# Color the background and draw lines to divide the window in quadrants.
|
12
|
-
background @r, @g, @b
|
13
|
-
stroke 0
|
14
|
-
line width/2, 0, width/2, height
|
15
|
-
line 0, height/2, width, height/2
|
16
|
-
|
17
|
-
# The following checks use the "ternary operator" which is a compact way
|
18
|
-
# of saying, "if this is true ? do this : otherwise this"
|
19
|
-
|
20
|
-
# If the mouse is on the right hand side of the window, increase red.
|
21
|
-
# Otherwise, it is on the left hand side and decrease red.
|
22
|
-
(mouse_x > width / 2) ? @r += 1 : @r -= 1
|
23
|
-
|
24
|
-
# If the mouse is on the bottom of the window, increase blue.
|
25
|
-
# Otherwise, it is on the top and decrease blue.
|
26
|
-
(mouse_y > height/2) ? @b += 1 : @b -= 1
|
27
|
-
|
28
|
-
# If the mouse is pressed. (for green)
|
29
|
-
mouse_pressed? ? @g += 1 : @g -= 1
|
30
|
-
|
31
|
-
# Constrain all color values to between 0 and 255.
|
32
|
-
@r = constrain(@r, 0, 255)
|
33
|
-
@g = constrain(@g, 0, 255)
|
34
|
-
@b = constrain(@b, 0, 255)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
MoreConditionals.new :title => "More Conditionals", :width => 200, :height => 200
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class Rollovers < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
def draw
|
10
|
-
background 255
|
11
|
-
stroke 0
|
12
|
-
line 100, 0, 100, 200
|
13
|
-
line 0, 100, 200, 100
|
14
|
-
|
15
|
-
# Fill a black color
|
16
|
-
no_stroke
|
17
|
-
fill 0
|
18
|
-
|
19
|
-
# Depending on the mouse location, a different rectangle is displayed.
|
20
|
-
if mouse_x < 100 && mouse_y < 100
|
21
|
-
rect 0, 0, 100, 100
|
22
|
-
elsif mouse_x > 100 && mouse_y < 100
|
23
|
-
rect 100, 0, 100, 100
|
24
|
-
elsif mouse_x < 100 && mouse_y > 100
|
25
|
-
rect 0, 100, 100, 100
|
26
|
-
elsif mouse_x > 100 && mouse_y > 100
|
27
|
-
rect 100, 100, 100, 100
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
Rollovers.new :title => "Rollovers", :width => 200, :height => 200
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class HoldDownTheButton < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@x, @y = 50, 50
|
7
|
-
@w, @h = 100, 75
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
# The button is pressed if (mouse_x, mouse_y) is inside
|
12
|
-
# the rectangle and mouse_pressed? is true.
|
13
|
-
if mouse_x > @x && mouse_x < @x+@w && mouse_y > @y && mouse_y < @y+@h && mouse_pressed?
|
14
|
-
button = true
|
15
|
-
else
|
16
|
-
button = false
|
17
|
-
end
|
18
|
-
|
19
|
-
if button
|
20
|
-
background 255
|
21
|
-
stroke 0
|
22
|
-
else
|
23
|
-
background 0
|
24
|
-
stroke 255
|
25
|
-
end
|
26
|
-
|
27
|
-
fill 175
|
28
|
-
rect @x, @y, @w, @h
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
HoldDownTheButton.new :title => "Hold Down The Button", :width => 200, :height => 200
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class ButtonAsSwitch < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@x, @y = 50, 50
|
7
|
-
@w, @h = 100, 75
|
8
|
-
@button = false
|
9
|
-
end
|
10
|
-
|
11
|
-
def draw
|
12
|
-
if @button
|
13
|
-
background 255
|
14
|
-
stroke 0
|
15
|
-
else
|
16
|
-
background 0
|
17
|
-
stroke 255
|
18
|
-
end
|
19
|
-
|
20
|
-
fill 175
|
21
|
-
rect @x, @y, @w, @h
|
22
|
-
end
|
23
|
-
|
24
|
-
# When the mouse is pressed, the state of the button is toggled.
|
25
|
-
# Try moving this code to draw() like in the rollover example. What goes wrong?
|
26
|
-
|
27
|
-
# The following method uses Ruby ranges, which look like 1..10, and
|
28
|
-
# stand for the range of things between the start and end element.
|
29
|
-
# You can ask a range if it includes a value, in this case whether
|
30
|
-
# or not mouse_x is between @x and @x+@w.
|
31
|
-
def mouse_pressed
|
32
|
-
if (@x..@x+@w).include?(mouse_x) && (@y..@y+@h).include?(mouse_y)
|
33
|
-
@button = !@button
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
ButtonAsSwitch.new :title => "Button As Switch", :width => 200, :height => 200
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class BouncingBall < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@x = 0
|
7
|
-
@speed = 1
|
8
|
-
smooth
|
9
|
-
end
|
10
|
-
|
11
|
-
def draw
|
12
|
-
background 255
|
13
|
-
|
14
|
-
# Add the current speed to the x location.
|
15
|
-
# += means x = x + speed
|
16
|
-
@x += @speed
|
17
|
-
|
18
|
-
# Remember, || means "or" and && means "and".
|
19
|
-
# speed *= -1 means (speed = speed * -1)
|
20
|
-
# this one line if statement works the way it looks
|
21
|
-
# (do this thing) if (this thing is true)
|
22
|
-
@speed *= -1 if @x > width || @x < 0
|
23
|
-
|
24
|
-
# Draw a circle at @x location.
|
25
|
-
stroke 0
|
26
|
-
fill 175
|
27
|
-
ellipse @x, 100, 32, 32
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
BouncingBall.new :title => "Bouncing Ball", :width => 200, :height => 200
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class BouncingColor < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@c1, @c2 = 1.0, 255.0
|
7
|
-
# Start by incrementing c1 and decrementing c2.
|
8
|
-
@c1_dir, @c2_dir = 0.1, -0.1
|
9
|
-
end
|
10
|
-
|
11
|
-
def draw
|
12
|
-
no_stroke
|
13
|
-
|
14
|
-
# Draw rectangle on left
|
15
|
-
fill @c1, 0, @c2
|
16
|
-
rect 0, 0, 100, 200
|
17
|
-
|
18
|
-
# Draw rectangle on right
|
19
|
-
fill @c2, 0, @c1
|
20
|
-
rect 100, 0, 100, 200
|
21
|
-
|
22
|
-
# Adjust color values
|
23
|
-
@c1 += @c1_dir
|
24
|
-
@c2 += @c2_dir
|
25
|
-
|
26
|
-
# Instead of reaching the edge of a window, these variables reach the "edge" of color:
|
27
|
-
# 0 for no color and 255 for full color.
|
28
|
-
# When this happens, just like with the bouncing ball, the direction is reversed.
|
29
|
-
#
|
30
|
-
# The following lines use Ranges and "unless" (which is the opposite of if).
|
31
|
-
@c1_dir *= -1 unless (0..255).include?(@c1)
|
32
|
-
@c2_dir *= -1 unless (0..255).include?(@c1)
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
BouncingColor.new :title => "Bouncing Color", :width => 200, :height => 200
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class SquareFollowingEdge < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@x, @y = 0, 0 # x and y locations of the square
|
7
|
-
@speed = 5 # speed of square
|
8
|
-
|
9
|
-
# A variable to keep track of the square's "state."
|
10
|
-
# Depending on the value of its state, it will either move right, down, left, or up.
|
11
|
-
@state = 0
|
12
|
-
end
|
13
|
-
|
14
|
-
def draw
|
15
|
-
background 255
|
16
|
-
|
17
|
-
# Display the square.
|
18
|
-
stroke 0
|
19
|
-
fill 175
|
20
|
-
rect @x, @y, 9, 9
|
21
|
-
|
22
|
-
# The following section uses a "case" statement. Cases let you
|
23
|
-
# set up different blocks of code for specific values of variables,
|
24
|
-
# as an alternative to a long if, elsif, else, kind of structure.
|
25
|
-
|
26
|
-
# If the state is 0, move to the right.
|
27
|
-
case @state
|
28
|
-
when 0
|
29
|
-
@x = @x + @speed
|
30
|
-
# If, while the state is 0, it reaches the right side of the window, change the state to 1
|
31
|
-
# Repeat this same logic for all states!?
|
32
|
-
if @x > width-10
|
33
|
-
@x = width-10
|
34
|
-
@state = 1
|
35
|
-
end
|
36
|
-
when 1
|
37
|
-
@y = @y + @speed
|
38
|
-
if @y > height-10
|
39
|
-
@y = height-10
|
40
|
-
@state = 2
|
41
|
-
end
|
42
|
-
when 2
|
43
|
-
@x = @x - @speed
|
44
|
-
if @x < 0
|
45
|
-
@x = 0
|
46
|
-
@state = 3
|
47
|
-
end
|
48
|
-
when 3
|
49
|
-
@y = @y - @speed;
|
50
|
-
if @y < 0
|
51
|
-
@y = 0
|
52
|
-
@state = 0
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
SquareFollowingEdge.new :title => "Square Following Edge", :width => 200, :height => 200
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class SimpleGravity < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@x, @y = 100, 0 # x and y locations of square
|
7
|
-
@speed = 0.0 # speed of square
|
8
|
-
|
9
|
-
# A new variable, for gravity (i.e. acceleration).
|
10
|
-
# We use a relatively small number (0.1) because this
|
11
|
-
# accelerations accumulates over time, increasing the speed.
|
12
|
-
# Try changing this number to 2.0 and see what happens.
|
13
|
-
@gravity = 0.1
|
14
|
-
end
|
15
|
-
|
16
|
-
def draw
|
17
|
-
background 255
|
18
|
-
|
19
|
-
# Display the square
|
20
|
-
fill 175
|
21
|
-
stroke 0
|
22
|
-
rect_mode CENTER
|
23
|
-
rect @x, @y, 10, 10
|
24
|
-
|
25
|
-
# Add speed to location
|
26
|
-
@y += @speed
|
27
|
-
|
28
|
-
# Add gravity to speed
|
29
|
-
@speed += @gravity
|
30
|
-
|
31
|
-
# If square reaches the bottom
|
32
|
-
# Reverse speed
|
33
|
-
@speed *= -0.95 if @y > @height
|
34
|
-
# Multiplying by -0.95 instead of -1 slows the square down
|
35
|
-
# each time it bounces (by decreasing speed).
|
36
|
-
# This is known as a "dampening" effect and is a more realistic
|
37
|
-
# simulation of the real world (without it, a ball would bounce forever).
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
SimpleGravity.new :title => "Simple Gravity", :width => 200, :height => 200
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class ZoogAndConditionals < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@x, @y, @w, @h = 100.0, 100.0, 60.0, 60.0
|
7
|
-
@eye_size = 16.0
|
8
|
-
|
9
|
-
# Zoog has variables for speed in the horizontal and vertical direction.
|
10
|
-
@x_speed, @y_speed = 3.0, 1.0
|
11
|
-
smooth
|
12
|
-
end
|
13
|
-
|
14
|
-
def draw
|
15
|
-
# Change the location of Zoog by speed
|
16
|
-
@x += @x_speed
|
17
|
-
@y += @y_speed
|
18
|
-
|
19
|
-
# An IF statement with a logical OR determines if Zoog has reached either
|
20
|
-
# the right or left edge of the screen.
|
21
|
-
# When this is true, we multiply speed by -1, reversing Zoog's direction!
|
22
|
-
# Identical logic is applied to the y direction as well.
|
23
|
-
@x_speed *= -1 if @x > width || @x < 0
|
24
|
-
@y_speed *= -1 if @y > height || @y < 0
|
25
|
-
|
26
|
-
background 255
|
27
|
-
ellipse_mode CENTER
|
28
|
-
rect_mode CENTER
|
29
|
-
|
30
|
-
# Draw Zoog's body
|
31
|
-
stroke 0
|
32
|
-
fill 150
|
33
|
-
rect @x, @y, @w/6, @h*2
|
34
|
-
|
35
|
-
# Draw Zoog's head
|
36
|
-
# added () to make order of operations a bit more clear
|
37
|
-
fill 255
|
38
|
-
ellipse @x, @y-@h/2, @w, @h
|
39
|
-
|
40
|
-
# Draw Zoog's eyes
|
41
|
-
fill 0
|
42
|
-
ellipse @x-@w/3+1, @y-@h/2, @eye_size, @eye_size*2
|
43
|
-
ellipse @x+@w/3-1, @y-@h/2, @eye_size, @eye_size*2
|
44
|
-
|
45
|
-
# Draw Zoog's legs
|
46
|
-
stroke 0
|
47
|
-
line @x-@w/12, @y+@h, @x-@w/4, @y+@h+10
|
48
|
-
line @x+@w/12, @y+@h, @x+@w/4, @y+@h+10
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
ZoogAndConditionals.new :title => "Zoog And Conditionals", :width => 200, :height => 200
|