1- require 'ruby-processing'
21
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
2+ def setup
3+ size 200 , 200
4+ # Three variables for the background color.
5+ @r , @g , @b = 0 , 0 , 0
6+ end
97
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
8+ def draw
9+ size 200 , 200
10+ # Color the background and draw lines to divide the window in quadrants.
11+ background @r , @g , @b
12+ stroke 0
13+ line width /2 , 0 , width / 2 , height
14+ line 0 , height / 2 , width , height / 2
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 hand side of the window, increase red.
20+ # Otherwise, it is on the left hand side and decrease red.
21+ ( mouse_x > width / 2 ) ? @r += 1 : @r -= 1
22+
23+ # If the mouse is on the bottom of the window, increase blue.
24+ # Otherwise, it is on the top and decrease blue.
25+ ( mouse_y > height / 2 ) ? @b += 1 : @b -= 1
26+
27+ # If the mouse is pressed. (for green)
28+ mouse_pressed? ? @g += 1 : @g -= 1
29+
30+ # Constrain all color values to between 0 and 255.
31+ @r = constrain ( @r , 0 , 255 )
32+ @g = constrain ( @g , 0 , 255 )
33+ @b = constrain ( @b , 0 , 255 )
3634end
3735
38- MoreConditionals . new :title => "More Conditionals" , :width => 200 , :height => 200
0 commit comments