Skip to content

Commit 10b1cb8

Browse files
author
monkstone
committed
chapt 7
1 parent 4c51622 commit 10b1cb8

File tree

6 files changed

+192
-182
lines changed

6 files changed

+192
-182
lines changed
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
require 'ruby-processing'
1+
# Strictly there are only bound and unbound methods in ruby
2+
# "setup, draw and draw_black_circle" are all bound to a Sketch object
23

3-
class DefiningAFunction < Processing::App
4+
def setup
5+
size 200, 200
6+
end
7+
8+
def draw
49

5-
def setup
6-
7-
end
8-
9-
def draw
10-
11-
end
12-
13-
# This example defines a function, but it is
14-
# not called. So nothing happens.
15-
def draw_black_circle
16-
fill 0
17-
ellipse 50, 50, 20, 20
18-
end
19-
2010
end
2111

22-
DefiningAFunction.new :title => "Defining A Function", :width => 200, :height => 200
12+
# This example defines a function, but it is
13+
# not called. So nothing happens.
14+
def draw_black_circle
15+
fill 0
16+
ellipse 50, 50, 20, 20
17+
end
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
require 'ruby-processing'
1+
# Many languages distinguish between functions, which have no associated object,
2+
# and methods, which are invoked on a receiver object. Strictly in ruby there are
3+
# no functions, they are either bound or unbound methods.
4+
# all the below are bound methods (bound to an object of the Sketch class)
25

3-
class CallingAFunction < Processing::App
6+
def setup
7+
size 100, 100
8+
smooth 4
9+
draw_black_circle
10+
end
11+
12+
def draw
13+
background 255
14+
draw_black_circle
15+
end
416

5-
def setup
6-
smooth
7-
draw_black_circle
8-
end
9-
10-
def draw
11-
background 255
12-
draw_black_circle
13-
end
14-
15-
def draw_black_circle
16-
fill 0
17-
ellipse 50, 50, 20, 20
18-
end
19-
17+
def draw_black_circle
18+
fill 0
19+
ellipse 50, 50, 20, 20
2020
end
2121

22-
CallingAFunction.new :title => "Calling A Function", :width => 100, :height => 100
Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
1-
require 'ruby-processing'
2-
3-
class BouncingBallWithFunctions < Processing::App
4-
5-
def setup
6-
# Declare variables
7-
@x = 1
8-
@speed = 1
9-
smooth
10-
end
11-
12-
def draw
13-
background 255
14-
# Instead of writing out all the code about the ball
15-
# in draw, we simply call three functions. How do
16-
# we know the names of these functions? We made them up!
17-
move
18-
bounce
19-
display
20-
end
21-
22-
# Where should functions be placed?
23-
# You can define your functions anywhere in the code outside of setup and draw.
24-
# However, the convention is to place your function definitions below draw.
25-
26-
# A function to move the ball
27-
def move
28-
# Change the x location by speed
29-
@x += @speed
30-
end
31-
32-
# A function to bounce the ball
33-
def bounce
34-
# If we’ve reached an edge, reverse speed
35-
# (0..width) is a Ruby range, representing the numbers from 0 to width (aka 200).
36-
# You can ask a range if it includes another number or not.
37-
@speed *= -1 unless (0..width).include? @x
38-
end
39-
40-
# A function to display the ball
41-
def display
42-
stroke 0
43-
fill 175
44-
ellipse @x, 100, 32, 32
45-
end
46-
1+
2+
3+
def setup
4+
size 200, 200
5+
@x = 1
6+
@speed = 1
7+
smooth
8+
end
9+
10+
def draw
11+
background 255
12+
# Instead of writing out all the code about the ball
13+
# in draw, we simply call three methods. How do
14+
# we know the names of these methods? We made them up!
15+
move
16+
bounce
17+
display
18+
end
19+
20+
# Where should methods be placed?
21+
# You can define your methods anywhere in the code outside of setup and draw.
22+
# However, the convention is to place your method definitions below draw.
23+
# These methods all get wrapped in the Sketch class at runtime.
24+
25+
# A method to move the ball
26+
def move
27+
# Change the x location by speed
28+
@x += @speed
29+
end
30+
31+
# A method to bounce the ball
32+
def bounce
33+
# If we’ve reached an edge, reverse speed
34+
# (0..width) is a Ruby range, representing the numbers from 0 to width (aka 200).
35+
# You can ask a range if it includes another number or not.
36+
@speed *= -1 unless (0..width).include? @x
37+
end
38+
39+
# A method to display the ball
40+
def display
41+
stroke 0
42+
fill 175
43+
ellipse @x, 100, 32, 32
4744
end
4845

49-
BouncingBallWithFunctions.new :title => "Bouncing Ball With Functions", :width => 200, :height => 200
Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
1-
require 'ruby-processing'
21

3-
class FunctionReturnsDistance < Processing::App
42

5-
def setup
3+
def setup
4+
size 200, 200
5+
end
6+
7+
def draw
8+
background 0
9+
stroke 0
610

7-
end
11+
# Set up width/2-1 and height/2-1 variables, for convenience.
12+
w1, h1 = width/2-1, height/2-1
813

9-
def draw
10-
background 0
11-
stroke 0
12-
13-
# Set up width/2-1 and height/2-1 variables, for convenience.
14-
w1, h1 = width/2-1, height/2-1
15-
16-
# Top left square
17-
# Our distance function is used to calculate a brightness value
18-
# for each quadrant. We could use the built-in function dist
19-
# instead, but we are learning how to write our own functions.
20-
fill distance(0, 0, mouse_x ,mouse_y)
21-
rect 0, 0, w1, h1
22-
23-
# Top right square
24-
fill distance(width, 0, mouse_x, mouse_y)
25-
rect width/2, 0, w1, h1
26-
27-
# Bottom left square
28-
fill distance(0, height, mouse_x, mouse_y)
29-
rect 0, height/2, w1, h1
30-
31-
# Bottom right square
32-
fill distance(width, height, mouse_x, mouse_y)
33-
rect width/2, height/2, w1, h1
14+
# Top left square
15+
# Our distance method is used to calculate a brightness value
16+
# for each quadrant. We could use the built-in method dist
17+
# instead, but we are learning how to write our own methods.
18+
fill distance(0, 0, mouse_x ,mouse_y)
19+
rect 0, 0, w1, h1
3420

35-
end
21+
# Top right square
22+
fill distance(width, 0, mouse_x, mouse_y)
23+
rect width/2, 0, w1, h1
3624

37-
# Defining the distance method.
38-
# In Ruby we don't have to write "return", because Ruby implicitly
39-
# returns the last thing that was evaluated from any method.
40-
def distance x1, y1, x2, y2
41-
dx = x1 - x2
42-
dy = y1 - y2
43-
sqrt(dx*dx + dy*dy)
44-
end
25+
# Bottom left square
26+
fill distance(0, height, mouse_x, mouse_y)
27+
rect 0, height/2, w1, h1
4528

29+
# Bottom right square
30+
fill distance(width, height, mouse_x, mouse_y)
31+
rect width/2, height/2, w1, h1
32+
33+
end
34+
35+
# Defining the distance method.
36+
# In Ruby we don't have to write "return", because Ruby implicitly
37+
# returns the last thing that was evaluated from any method.
38+
def distance x1, y1, x2, y2
39+
dx = x1 - x2
40+
dy = y1 - y2
41+
sqrt(dx*dx + dy*dy)
4642
end
4743

48-
FunctionReturnsDistance.new :title => "Function Returns Distance", :width => 200, :height => 200
Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
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
6965
end
7066

71-
ZoogWithFunctions.new :title => "Zoog With Functions", :width => 200, :height => 200

0 commit comments

Comments
 (0)