To install the gem, use:
$ gem install r2d
Shapes and images can be drawn procedurally using the methods below:
x = an 'x' coordinate (number)
y = an 'y' coordinate (number)
s = the size (number)
w = the width (number)
h = the height (number)
c = the color (string)
or...
c = [r, g, b, a]
where...
r = red (number) 0..255
g = green (number) 0..255
b = blue (number) 0..255
a = alpha (number) 0..255
draw_line(x1, y1, x2, y2, c)
draw_triangle(x1, y1, x2, y2, x3, y3, c)
draw_square(x, y, s, c)
draw_rect(x, y, w, h, c)
draw_text(x, y, string, c)
draw_line(100, 150, 300, 350, "red")
draw_triangle(200, 50, 100, 300, 400, 300, "orange")
draw_square(10, 30, 50, [255, 255, 100, 150])
draw_rect(20, 20, 40, 40, [0, 255, 0, 255])
draw_text(50, 50, "Hello world!", "blue")
draw_img(x, y, img_path)
draw_img(50, 100, "galaxy.jpg")
Each shape has an associated class. Shapes can be instantiated and added to the window using Window.add(shape)
and removed with Window.remove(shape)
.
l = Line.new(x1, y1, x2, y2, c)
l.x1 = 10
l.y1 = 10
l.color = "yellow"
Window.add(l)
Window.remove(l)
t = Triangle.new(x1, y1, x2, y2, x3, y3, c)
t.color = "green"
t.x3 = 75
t.y3 = 150
s = Square.new(x, y, s)
s = Square.new(x, y, s, c)
s.width = 25
s.height = 50
s.color = "red"
Window.add(s)
r = Rectangle.new(x, y, w, h)
r = Rectangle.new(x, y, w, h, c)
r.x = 10
r.y = 20
r.width = 200
r.height = 50
r.color = "blue"
Window.add(r)
img = Image.new(x, y, img_path)
img.x = 10
img.y = 20
These are long audio clips. (More soon.)
s = Song.new("track.mp3")
s.play
s.playing? # true
s.pause
s.paused? # true
s.stop
s.loop
Sounds are short audio clips. (More soon.)
s = Sound.new("snare.mp3")
s.play
Attributes change the appearance and behavior of the window. These must be applied before displaying the window with Window.show
.
Setting the window size:
Window.width = w
Window.height = h
Changing the background color, where color
is a string or an array of [r, g, b, a]
values.
Window.background = color
Showing the cursor over the window (default = true
):
Window.cursor = false
Setting the title (default = "Learn Ruby"
):
Window.title = "Hello world!"
Objects can be added and removed from the window using:
r = Rectangle.new(50, 50, 100, 100)
Window.add(r)
Window.remove(r)
Additionally, all objects have an add
and remove
convenience method which will do the same:
r = Rectangle.new(50, 50, 100, 100)
r.add
r.remove
If an object is already in the window, add
will return false
. Similarly, if the object is not in the window, remove
will return false
.
r = Rectangle.new(50, 50, 100, 100)
r.add # true
r.add # false - already in window
r.remove # true
r.remove # false - not in window
All objects can be removed from the window using:
Window.clear
To capture a single key press where <key_string>
is any valid keyboard character a..z
, A..Z
, 0..9
, and string representing arrows, alt, control, shift, etc:
Window.on_key <key_string> do
#...
end
Window.on_key 'm' do
puts "You pressed 'm'"
end
Window.on_key 'up' do
puts "Up arrow pressed"
end
Window.on_key 'space' do
puts "Spacebar pressed"
end
Mouse input can also be captured using the 'mouse_left'
and 'mouse_right'
strings.
Window.on_key 'mouse_right' do
puts "Mouse clicked in the window"
end
To detect whether a key is held down, use either...
Window.key_down <key_string> do
#...
end
or Window.update
:
Window.update do
if key_down? <key_string>
#...
end
end
The two examples below are equivalent and both execute in the main update loop.
Window.key_down 'x' do
puts "'x' pressed at #{Time.now}"
end
Window.update do
if key_down? 'x'
puts "'x' pressed at #{Time.now}"
end
end
The Window.update
block represents the main update loop. Anything in this block will update 60 times per second (by default):
Window.update do
#...
end
Mouse positions are relative to the top left corner of the window.
Window.mouse_x # current 'x' position
Window.mouse_y # current 'y' position
Use Window.show
to display the window. This should only be called once.
s = Square.new(100, 100, 50, "red")
s.add
Window.show
The Window
class has a number of helpers aliased to aid learning:
- update
- on_key
- key_down
update do
#...
end
key_down 'x' do
#...
end
Visual shapes and images also have knowledge of the Window
class, so they can be added and removed from the window using their helper methods.
s = Square.new(x, y, s)
# Add and remove calling the `Window` class
Window.add(s)
Window.remove(s)
# Add and remove using the helper methods
s.add
s.remove
x = Window.mouse_x
Window.update do
if Window.mouse_x != x
puts x = Window.mouse_x
end
end