ruby-processing 1.0.2 → 1.0.3
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 +12 -0
- data/lib/ruby-processing.rb +2 -1
- data/lib/ruby-processing/app.rb +7 -5
- data/lib/ruby-processing/exporters/creator.rb +7 -4
- data/lib/ruby-processing/helpers/numeric.rb +11 -0
- data/lib/ruby-processing/helpers/string.rb +8 -0
- data/lib/ruby-processing/runner.rb +11 -10
- data/lib/ruby-processing/runners/watch.rb +33 -0
- data/samples/bezier_playground.rb +243 -0
- data/samples/flight_patterns.rb +1 -1
- data/samples/learning_processing/chapter_16/01_display_video.rb +1 -1
- data/samples/learning_processing/chapter_16/02_manipulate_video_image.rb +1 -1
- data/samples/processing_app/basics/arrays/array.rb +41 -0
- data/samples/processing_app/basics/arrays/array_2d.rb +38 -0
- data/samples/processing_app/basics/arrays/array_objects.rb +67 -0
- data/samples/processing_app/basics/color/brightness.rb +30 -0
- data/samples/processing_app/basics/color/color_wheel.rb +97 -0
- data/samples/processing_app/basics/color/creating.rb +37 -0
- data/samples/processing_app/basics/color/hue.rb +29 -0
- data/samples/processing_app/basics/color/linear_gradient.rb +47 -0
- data/samples/processing_app/basics/color/radial_gradient.rb +52 -0
- data/samples/processing_app/basics/color/reading/data/cait.jpg +0 -0
- data/samples/processing_app/basics/color/reading/reading.rb +43 -0
- data/samples/processing_app/basics/color/relativity.rb +38 -0
- data/samples/processing_app/basics/color/saturation.rb +35 -0
- data/samples/processing_app/basics/color/wave_gradient.rb +45 -0
- data/samples/processing_app/basics/control/conditionals1.rb +51 -0
- data/samples/processing_app/basics/control/conditionals2.rb +44 -0
- data/samples/processing_app/basics/control/embedded_iteration.rb +42 -0
- data/samples/processing_app/basics/control/iteration.rb +61 -0
- data/samples/processing_app/basics/control/logical_operators.rb +59 -0
- data/samples/processing_app/basics/data/characters_strings/characters_strings.rb +87 -0
- data/samples/processing_app/basics/data/characters_strings/data/Eureka-90.vlw +0 -0
- data/samples/processing_app/basics/data/characters_strings/data/rathausFrog.jpg +0 -0
- data/samples/processing_app/basics/data/datatype_conversion.rb +45 -0
- data/samples/processing_app/basics/data/integers_floats.rb +34 -0
- data/samples/processing_app/basics/data/true_false.rb +37 -0
- data/samples/processing_app/basics/data/variable_scope.rb +80 -0
- data/samples/processing_app/basics/data/variables.rb +40 -0
- data/samples/processing_app/basics/form/bezier.rb +23 -0
- data/samples/processing_app/basics/form/bezier_ellipse.rb +110 -0
- data/samples/processing_app/basics/form/pie_chart.rb +31 -0
- data/samples/processing_app/basics/form/points_lines.rb +37 -0
- data/samples/processing_app/basics/form/shape_primitives.rb +25 -0
- data/samples/processing_app/basics/form/triangle_strip.rb +43 -0
- data/samples/processing_app/basics/form/vertices.rb +51 -0
- data/samples/processing_app/basics/image/alphamask.rb +23 -0
- data/samples/processing_app/basics/image/background_image.rb +30 -0
- data/samples/processing_app/basics/image/create_image.rb +23 -0
- data/samples/processing_app/basics/image/data/construct.jpg +0 -0
- data/samples/processing_app/basics/image/data/eames.jpg +0 -0
- data/samples/processing_app/basics/image/data/jelly.jpg +0 -0
- data/samples/processing_app/basics/image/data/mask.jpg +0 -0
- data/samples/processing_app/basics/image/data/milan_rubbish.jpg +0 -0
- data/samples/processing_app/basics/image/data/teddy.gif +0 -0
- data/samples/processing_app/basics/image/data/test.jpg +0 -0
- data/samples/processing_app/basics/image/data/wash.jpg +0 -0
- data/samples/processing_app/basics/image/load_display_image.rb +25 -0
- data/samples/processing_app/basics/image/pointillism.rb +30 -0
- data/samples/processing_app/basics/image/request_image.rb +71 -0
- data/samples/processing_app/basics/image/sprite.rb +38 -0
- data/samples/processing_app/basics/image/transparency.rb +27 -0
- data/samples/processing_app/basics/input/clock.rb +46 -0
- data/samples/processing_app/basics/input/constrain.rb +39 -0
- data/samples/processing_app/basics/input/easing.rb +33 -0
- data/samples/processing_app/basics/input/keyboard.rb +36 -0
- data/samples/processing_app/basics/input/keyboard_2.rb +45 -0
- data/samples/processing_app/basics/input/keyboard_functions.rb +87 -0
- data/samples/processing_app/basics/input/milliseconds.rb +25 -0
- data/samples/processing_app/basics/input/mouse_1d.rb +34 -0
- data/samples/processing_app/basics/input/mouse_2d.rb +25 -0
- data/samples/processing_app/basics/input/mouse_functions.rb +73 -0
- data/samples/processing_app/basics/input/mouse_press.rb +21 -0
- data/samples/processing_app/basics/input/mouse_signals.rb +45 -0
- data/samples/processing_app/basics/input/storing_input.rb +39 -0
- data/samples/processing_app/basics/math/additive_wave.rb +66 -0
- data/samples/processing_app/basics/math/arctangent.rb +54 -0
- data/samples/processing_app/topics/simulate/chain.rb +82 -0
- data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +166 -0
- data/samples/processing_app/topics/simulate/simple_particle_system.rb +113 -0
- data/samples/processing_app/topics/simulate/spring.rb +85 -0
- data/samples/processing_app/topics/simulate/springs.rb +125 -0
- metadata +93 -2
Binary file
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# An image is recreated from its individual component colors.
|
4
|
+
# The many colors of the image are created through modulating the
|
5
|
+
# red, green, and blue values. This is an exageration of an LCD display.
|
6
|
+
|
7
|
+
class Reading < Processing::App
|
8
|
+
|
9
|
+
def setup
|
10
|
+
no_stroke
|
11
|
+
background 0
|
12
|
+
|
13
|
+
c = load_image "cait.jpg"
|
14
|
+
|
15
|
+
xoff, yoff = 0, 0
|
16
|
+
p = 2
|
17
|
+
pix = p * 3
|
18
|
+
|
19
|
+
(c.width * c.height).times do |i|
|
20
|
+
|
21
|
+
pixel = c.pixels[i]
|
22
|
+
|
23
|
+
fill red( pixel ), 0, 0
|
24
|
+
rect xoff, yoff, p, pix
|
25
|
+
|
26
|
+
fill 0, green( pixel ), 0
|
27
|
+
rect xoff+p, yoff, p, pix
|
28
|
+
|
29
|
+
fill 0, 0, blue( pixel )
|
30
|
+
rect xoff+p*2, yoff, p, pix
|
31
|
+
|
32
|
+
xoff += pix
|
33
|
+
if xoff >= (width-pix)
|
34
|
+
xoff = 0
|
35
|
+
yoff += pix
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
Reading.new :title => "Reading", :width => 200, :height => 200
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Each color is perceived in relation to other colors.
|
4
|
+
# The top and bottom bars each contain the same component colors,
|
5
|
+
# but a different display order causes individual colors to appear differently.
|
6
|
+
|
7
|
+
class Relativity < Processing::App
|
8
|
+
|
9
|
+
def setup
|
10
|
+
|
11
|
+
a = color 165, 167, 20
|
12
|
+
b = color 77, 86, 59
|
13
|
+
c = color 42, 106, 105
|
14
|
+
d = color 165, 89, 20
|
15
|
+
e = color 146, 150, 127
|
16
|
+
|
17
|
+
no_stroke
|
18
|
+
|
19
|
+
draw_band [a, b, c, d, e], 0, 4
|
20
|
+
draw_band [c, a, d, b, e], height/2, 4
|
21
|
+
end
|
22
|
+
|
23
|
+
def draw_band( color_order, ypos, bar_width )
|
24
|
+
|
25
|
+
num = color_order.length
|
26
|
+
|
27
|
+
(0...width).step(bar_width * num) do |i|
|
28
|
+
num.times do |j|
|
29
|
+
fill color_order[j]
|
30
|
+
rect i + j*bar_width, ypos, bar_width, height/2
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
Relativity.new :title => "Relativity", :width => 200, :height => 200
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Saturation is the strength or purity of the color and represents the
|
4
|
+
# amount of gray in proportion to the hue. A "saturated" color is pure
|
5
|
+
# and an "unsaturated" color has a large percentage of gray.
|
6
|
+
# Move the cursor vertically over each bar to alter its saturation.
|
7
|
+
|
8
|
+
class Saturation < Processing::App
|
9
|
+
|
10
|
+
def setup
|
11
|
+
|
12
|
+
color_mode HSB, 360, height, height
|
13
|
+
no_stroke
|
14
|
+
|
15
|
+
@bar_width = 5
|
16
|
+
@saturation = Array.new( width/@bar_width, 0 )
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def draw
|
21
|
+
i = 0; j = 0; while i <= (width-@bar_width)
|
22
|
+
|
23
|
+
if (mouseX > i) && (mouseX < (i+@bar_width))
|
24
|
+
@saturation[j] = mouseY
|
25
|
+
end
|
26
|
+
|
27
|
+
fill i, @saturation[j], height/1.5
|
28
|
+
rect i, 0, @bar_width, height
|
29
|
+
|
30
|
+
j += 1; i += @bar_width; end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
Saturation.new :title => "Saturation", :width => 200, :height => 200
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Wave Gradient
|
4
|
+
# by Ira Greenberg.
|
5
|
+
#
|
6
|
+
# Generate a gradient along a sin() wave.
|
7
|
+
|
8
|
+
class WaveGradient < Processing::App
|
9
|
+
|
10
|
+
def setup
|
11
|
+
|
12
|
+
background 200
|
13
|
+
|
14
|
+
angle = 0.0
|
15
|
+
px, py = 0.0, 0.0
|
16
|
+
amplitude = 30.0
|
17
|
+
frequency = 0.0
|
18
|
+
fill_gap = 2.5
|
19
|
+
|
20
|
+
(height + 150).times do |i|
|
21
|
+
i -= 75
|
22
|
+
angle = 0.0
|
23
|
+
frequency += 0.006
|
24
|
+
|
25
|
+
(width + 75).times do |j|
|
26
|
+
py = i + sin( angle.radians ) * amplitude
|
27
|
+
angle += frequency
|
28
|
+
|
29
|
+
c = color((py-i).abs * 255/amplitude,
|
30
|
+
255 - (py-i).abs * 255/amplitude,
|
31
|
+
j * (255.0/(width+50)))
|
32
|
+
|
33
|
+
fill_gap.ceil.times do |filler|
|
34
|
+
set j-filler, py.to_i-filler, c
|
35
|
+
set j, py.to_i, c
|
36
|
+
set j+filler, py.to_i+filler, c
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
WaveGradient.new :title => "Wave Gradient", :width => 200, :height => 200
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Conditions are like questions.
|
4
|
+
# They allow a program to decide to take one action if
|
5
|
+
# the answer to a question is true or to do another action
|
6
|
+
# if the answer to the question is false.
|
7
|
+
# The questions asked within a program are always logical
|
8
|
+
# or relational statements. For example, if the variable 'i' is
|
9
|
+
# equal to zero then draw a line.
|
10
|
+
|
11
|
+
class Conditionals1 < Processing::App
|
12
|
+
|
13
|
+
def setup
|
14
|
+
background 0
|
15
|
+
|
16
|
+
1.upto( width / 10 ) do |i|
|
17
|
+
# If 'i' is even then draw the first line otherwise draw the second line
|
18
|
+
i.even? ? draw_short(i) : draw_long(i)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def draw_short(i)
|
23
|
+
stroke 153
|
24
|
+
line i*10, 40, i*10, height/2
|
25
|
+
end
|
26
|
+
|
27
|
+
def draw_long(i)
|
28
|
+
stroke 102
|
29
|
+
line i*10, 20, i*10, 180
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
# Ruby allows us to extend base classes, such as numbers, with
|
35
|
+
# methods of our choosing. In this case we'll add methods that tell
|
36
|
+
# you whether a given integer is even or odd.
|
37
|
+
|
38
|
+
class Fixnum
|
39
|
+
|
40
|
+
def even?
|
41
|
+
self % 2 == 0
|
42
|
+
end
|
43
|
+
|
44
|
+
def odd?
|
45
|
+
!even?
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
Conditionals1.new :title => "Conditionals1", :width => 200, :height => 200
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# We extend the language of conditionals by adding the
|
4
|
+
# keyword "elsif". This allows conditionals to ask
|
5
|
+
# two or more sequential questions, each with a different
|
6
|
+
# action.
|
7
|
+
|
8
|
+
class Conditionals2 < Processing::App
|
9
|
+
|
10
|
+
def setup
|
11
|
+
|
12
|
+
background 0
|
13
|
+
|
14
|
+
1.upto( width / 2 ) do |i|
|
15
|
+
|
16
|
+
# If 'i' divides by 10 with no remainder
|
17
|
+
# draw the first line ..
|
18
|
+
# else if 'i' devides by 5 with no remainder
|
19
|
+
# draw second line else draw third
|
20
|
+
|
21
|
+
if (i % 10) == 0
|
22
|
+
|
23
|
+
stroke 255
|
24
|
+
line i*2, 40, i*2, height/2
|
25
|
+
|
26
|
+
elsif (i % 5) == 0
|
27
|
+
|
28
|
+
stroke 153
|
29
|
+
line i*2, 20, i*2, 180
|
30
|
+
|
31
|
+
else
|
32
|
+
|
33
|
+
stroke 102
|
34
|
+
line i*2, height/2, i*2, height-40
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
Conditionals2.new :title => "Conditionals2", :width => 200, :height => 200
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Embedding "for" structures allows repetition in two dimensions.
|
4
|
+
|
5
|
+
class EmbeddedIteration < Processing::App
|
6
|
+
|
7
|
+
def setup
|
8
|
+
|
9
|
+
background 0
|
10
|
+
no_stroke
|
11
|
+
|
12
|
+
box_size = 11.0
|
13
|
+
box_space = 12.0
|
14
|
+
margin = 7
|
15
|
+
|
16
|
+
# Draw gray boxes
|
17
|
+
|
18
|
+
(margin...height-margin).step(box_space) do |i|
|
19
|
+
# or, if you feel more java-loopy:
|
20
|
+
# i = margin; while i < height-margin
|
21
|
+
|
22
|
+
if box_size > 0
|
23
|
+
|
24
|
+
(margin...width-margin).step(box_space) do |j|
|
25
|
+
|
26
|
+
fill( 255 - box_size * 10 )
|
27
|
+
rect j, i, box_size, box_size
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
box_size -= 0.6
|
33
|
+
|
34
|
+
# for java loops, don't forget to increment with while:
|
35
|
+
# i += box_space
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
EmbeddedIteration.new :title => "Embedded Iteration", :width => 200, :height => 200
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Iteration in Ruby differs from that in Processing / Java.
|
4
|
+
# Where you'd mainly use for- and while constructs in the later
|
5
|
+
# you'd be using iterators with blocks (do-end, {}) in Ruby.
|
6
|
+
|
7
|
+
class Iteration < Processing::App
|
8
|
+
|
9
|
+
def setup
|
10
|
+
background 102
|
11
|
+
no_stroke
|
12
|
+
|
13
|
+
xpos1 = 100
|
14
|
+
xpos2 = 118
|
15
|
+
count = 0
|
16
|
+
timey = 0
|
17
|
+
num = 12
|
18
|
+
|
19
|
+
# Draw white bars
|
20
|
+
|
21
|
+
fill 255
|
22
|
+
k = 60
|
23
|
+
|
24
|
+
(num/3).times do |i| # Loop using the "times" iterator of a number
|
25
|
+
rect 25, k, 155, 5
|
26
|
+
k += 10
|
27
|
+
end
|
28
|
+
|
29
|
+
# Dark grey bars
|
30
|
+
|
31
|
+
fill 51
|
32
|
+
k = 40
|
33
|
+
|
34
|
+
0.upto( num-1 ) do |i| # Loop using "upto" of a number
|
35
|
+
rect 105, k, 30, 5
|
36
|
+
k += 10
|
37
|
+
end
|
38
|
+
|
39
|
+
k = 15
|
40
|
+
|
41
|
+
arr = (0...num).to_a # Create an array with increasing numbers from a Range to
|
42
|
+
|
43
|
+
arr.each do |i| # use with the "each" iterator of Array
|
44
|
+
rect 125, k, 30, 5
|
45
|
+
k += 10
|
46
|
+
end
|
47
|
+
|
48
|
+
# Thin lines
|
49
|
+
|
50
|
+
fill 0
|
51
|
+
k = 42
|
52
|
+
|
53
|
+
(0...num-1).step(1) do |i| # Loop using a Range with its "step" iterator
|
54
|
+
rect 36, k, 20, 1
|
55
|
+
k += 10
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
Iteration.new :title => "Iteration", :width => 200, :height => 200
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# The logical operators for AND (&&) and OR (||) are used to
|
4
|
+
# combine simple relational statements into more complex expressions.
|
5
|
+
# The NOT (!) operator is used to negate a boolean statement.
|
6
|
+
|
7
|
+
class LogicalOperators < Processing::App
|
8
|
+
|
9
|
+
def setup
|
10
|
+
|
11
|
+
background 126
|
12
|
+
|
13
|
+
op = false
|
14
|
+
|
15
|
+
(5..195).step(5) do |i|
|
16
|
+
|
17
|
+
stroke 0
|
18
|
+
|
19
|
+
# Logical AND
|
20
|
+
|
21
|
+
if (i > 35) && (i < 100)
|
22
|
+
|
23
|
+
line 5, i, 95, i
|
24
|
+
op = false
|
25
|
+
end
|
26
|
+
|
27
|
+
stroke 76
|
28
|
+
|
29
|
+
# Logical OR
|
30
|
+
|
31
|
+
if (i <= 35) || (i >= 100)
|
32
|
+
line 105, i, 195, i
|
33
|
+
op = true
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
# Testing if a boolean value is "true"
|
38
|
+
# The expression "if op" is equivalent to "if (op == true)"
|
39
|
+
|
40
|
+
if op
|
41
|
+
stroke 0
|
42
|
+
point width/2, i
|
43
|
+
end
|
44
|
+
|
45
|
+
# Testing if a boolean value is "false"
|
46
|
+
# The expressions "unless op" or "if !op" are equivalent to "if (op == false)"
|
47
|
+
|
48
|
+
unless op
|
49
|
+
stroke 255
|
50
|
+
point width/4, i
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
LogicalOperators.new :title => "Logical Operators", :width => 200, :height => 200
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# * Click on the image to give it focus and then type letters to
|
4
|
+
# * shift the location of the image.
|
5
|
+
# * Characters are typographic symbols such as A, d, and %.
|
6
|
+
# * The character datatype, abbreviated as char, stores letters and
|
7
|
+
# * symbols in the Unicode format, a coding system developed to support
|
8
|
+
# * a variety of world languages. Characters are distinguished from other
|
9
|
+
# * symbols by putting them between single quotes ('P').
|
10
|
+
# * A string is a sequence of characters. A string is noted by surrounding
|
11
|
+
# * a group of letters with double quotes ("Processing").
|
12
|
+
# * Chars and strings are most often used with the keyboard methods,
|
13
|
+
# * to display text to the screen, and to load images or files.
|
14
|
+
|
15
|
+
class CharactersStrings < Processing::App
|
16
|
+
|
17
|
+
def setup
|
18
|
+
|
19
|
+
render_mode P2D
|
20
|
+
|
21
|
+
@xoffset = 0
|
22
|
+
@letter = ""
|
23
|
+
|
24
|
+
@font = load_font "Eureka-90.vlw"
|
25
|
+
text_font @font
|
26
|
+
|
27
|
+
# Draw text more accurately and efficiently.
|
28
|
+
|
29
|
+
text_mode SCREEN
|
30
|
+
text_align CENTER
|
31
|
+
|
32
|
+
# A String is actually a class with its own methods, some of which are
|
33
|
+
# featured below.
|
34
|
+
|
35
|
+
name = "rathausFrog"
|
36
|
+
extension = ".jpg"
|
37
|
+
|
38
|
+
puts "The length of #{name} is #{name.length}." # "puts" is Ruby for "println"
|
39
|
+
|
40
|
+
name = name + extension
|
41
|
+
puts "The length of #{name} is #{name.length}."
|
42
|
+
|
43
|
+
# The parameter for the loadImage() method must be a string
|
44
|
+
# This line could also be written "frog = load_image "rathausFrog.jpg"
|
45
|
+
|
46
|
+
@frog = load_image name
|
47
|
+
end
|
48
|
+
|
49
|
+
def draw
|
50
|
+
background 51 # Set background to dark gray
|
51
|
+
|
52
|
+
# Same as "image @frog, @xoffset, 0", but more efficient
|
53
|
+
# because no transformations or "tint" or "smooth" are used.
|
54
|
+
set @xoffset, 0, @frog
|
55
|
+
|
56
|
+
# Draw an X
|
57
|
+
line 0, 0, width, height
|
58
|
+
line 0, height, width, 0
|
59
|
+
|
60
|
+
# Draw the letter to the center of the screen
|
61
|
+
text @letter, width/2, height/2
|
62
|
+
end
|
63
|
+
|
64
|
+
def key_pressed
|
65
|
+
# The variable "key" always contains the value of the most recent key pressed.
|
66
|
+
# If the key is an upper or lowercase letter between 'A' and 'z'
|
67
|
+
# the image is shifted to the corresponding value of that key
|
68
|
+
|
69
|
+
if (key >= "A"[0]) and (key <= "z"[0]) # should become "A".ord and "z".ord in Ruby 1.9
|
70
|
+
|
71
|
+
# Map the index of the key pressed from the range between 'A' and 'z',
|
72
|
+
# into a position for the left edge of the image. The maximum xoffset
|
73
|
+
# is the width of the drawing area minus the size of the image.
|
74
|
+
|
75
|
+
@xoffset = map( key, "A"[0], "z"[0], 0, width - @frog.width ).to_i
|
76
|
+
|
77
|
+
# Update the letter shown to the screen
|
78
|
+
@letter = key.chr
|
79
|
+
|
80
|
+
# Write the letter to the console
|
81
|
+
puts key
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
CharactersStrings.new :title => "Characters Strings", :width => 200, :height => 200
|