1- require 'ruby-processing'
21
3- class ZoogWithFunctions < Processing ::App
2+ def setup
3+ size 200 , 200
4+ @x , @y = 100 , 100
5+ @w , @h = 60 , 60
6+ @eye_size = 16
7+ @speed = 1
8+ ellipse_mode CENTER
9+ rect_mode CENTER
10+ stroke 0
11+ smooth 4
12+ end
413
5- def setup
6- @x , @y = 100 , 100
7- @w , @h = 60 , 60
8- @eye_size = 16
9- @speed = 1
10- ellipse_mode CENTER
11- rect_mode CENTER
12- stroke 0
13- smooth
14- end
14+ def draw
15+ background 255 # Draw a black background
1516
16- def draw
17- background 255 # Draw a black background
18-
19- # mouse_x position determines speed factor for moveZoog function
20- factor = constrain mouse_x /10 , 0 , 5
21-
22- # The code for changing the variables associated with Zoog and
23- # displaying Zoog is moved outside of draw and into functions
24- # called here. The functions are given arguments, such as
25- # "Jiggle Zoog by the following factor" and "draw Zoog with
26- # the following eye color".
27- jiggle_zoog factor
28-
29- # pass in a color to the draw_zoog function for eye color
30- d = dist ( @x , @y , mouse_x , mouse_y )
31- c = color ( d )
32- draw_zoog c
33- end
17+ # mouse_x position determines speed factor for moveZoog function
18+ factor = constrain mouse_x /10 , 0 , 5
3419
35- def jiggle_zoog speed
36- # Change the x and y location of Zoog randomly
37- @x = @x + random ( -1 , 1 ) * speed
38- @y = @y + random ( -1 , 1 ) * speed
39- # Constrain Zoog to window
40- @x = constrain @x , 0 , width
41- @y = constrain @y , 0 , height
42- end
20+ # The code for changing the variables associated with Zoog and
21+ # displaying Zoog is moved outside of draw and into methods
22+ # called here. The methods are given arguments, such as
23+ # "Jiggle Zoog by the following factor" and "draw Zoog with
24+ # the following eye color".
25+ jiggle_zoog factor
4326
44- def draw_zoog eye_color
45- # Arms are incorporated into Zoog's design with a times loop.
46- 6 . times do |i |
47- y = i * 10 + @y + 5
48- line @x -@w /3 , y , @x +@w /3 , y
49- end
27+ # pass in a color to the draw_zoog function for eye color
28+ d = dist ( @x , @y , mouse_x , mouse_y )
29+ c = color ( d )
30+ draw_zoog c
31+ end
5032
51- # Draw Zoog's body
52- fill 175
53- rect @x , @y , @w /6 , @h *2
33+ def jiggle_zoog speed
34+ # Change the x and y location of Zoog randomly
35+ @x = @x + random ( -1 , 1 ) * speed
36+ @y = @y + random ( -1 , 1 ) * speed
37+ # Constrain Zoog to window
38+ @x = constrain @x , 0 , width
39+ @y = constrain @y , 0 , height
40+ end
5441
55- # Draw Zoog's head
56- fill 255
57- ellipse @x , @y -@h /2 , @w , @h
42+ def draw_zoog eye_color
43+ # Arms are incorporated into Zoog's design with a times loop.
44+ 6 . times do |i |
45+ y = i * 10 + @y + 5
46+ line @x -@w /3 , y , @x +@w /3 , y
47+ end
5848
59- # Draw Zoog's eyes
60- fill eye_color
61- ellipse @x -@w /3 +1 , @y -@h /2 , @eye_size , @eye_size *2
62- ellipse @x +@w /3 -1 , @y -@h /2 , @eye_size , @eye_size *2
49+ # Draw Zoog's body
50+ fill 175
51+ rect @x , @y , @w /6 , @h *2
6352
64- # Draw Zoog's legs
65- line @x -@w /12 , @y +@h , @x -@w /4 , @y +@h +10
66- line @x +@w /12 , @y +@h , @x +@w /4 , @y +@h +10
67- end
68-
53+ # Draw Zoog's head
54+ fill 255
55+ ellipse @x , @y -@h /2 , @w , @h
56+
57+ # Draw Zoog's eyes
58+ fill eye_color
59+ ellipse @x -@w /3 +1 , @y -@h /2 , @eye_size , @eye_size *2
60+ ellipse @x +@w /3 -1 , @y -@h /2 , @eye_size , @eye_size *2
61+
62+ # Draw Zoog's legs
63+ line @x -@w /12 , @y +@h , @x -@w /4 , @y +@h +10
64+ line @x +@w /12 , @y +@h , @x +@w /4 , @y +@h +10
6965end
7066
71- ZoogWithFunctions . new :title => "Zoog With Functions" , :width => 200 , :height => 200
0 commit comments