Class: Trtl
- Inherits:
-
Object
- Object
- Trtl
- Defined in:
- lib/trtl.rb
Constant Summary collapse
- VERSION =
"0.0.1"
- CANVAS_WIDTH =
800
- CANVAS_HEIGHT =
600
- HOME_X =
CANVAS_WIDTH / 2
- HOME_Y =
CANVAS_HEIGHT / 2
- COLORS =
%w{red blue green white cyan pink yellow}
- DEG =
Math::PI / 180.0
Instance Attribute Summary collapse
-
#canvas ⇒ Object
readonly
Returns the value of attribute canvas.
- #color(color) ⇒ Object (also: #pencolor)
-
#heading ⇒ Object
Returns the value of attribute heading.
- #width(width) ⇒ Object
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #back(amount = 20) ⇒ Object (also: #bk, #backward)
-
#circle(radius, extent = 360, steps = 360) ⇒ Object
TODO / TOFIX: This is horribly wrong with the fewer steps due to circumference varying ;-).
- #dot(size = nil) ⇒ Object
- #ensure_drawn ⇒ Object
- #forward(amount = 20) ⇒ Object (also: #fd)
- #home ⇒ Object
-
#initialize(options = {}) ⇒ Trtl
constructor
A new instance of Trtl.
- #is_drawing? ⇒ Boolean
- #left(offset) ⇒ Object (also: #lt, #turnleft)
- #move(new_x, new_y) ⇒ Object (also: #goto, #setpos, #setposition)
- #pen_down ⇒ Object (also: #pd, #pendown, #down)
- #pen_up ⇒ Object (also: #pu, #penup, #up)
- #position ⇒ Object (also: #pos)
- #right(offset) ⇒ Object (also: #rt, #turnright)
- #sleep(time = 100000) ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Trtl
Returns a new instance of Trtl.
17 18 19 20 21 22 23 24 25 |
# File 'lib/trtl.rb', line 17 def initialize( = {}) @color = [:color] || COLORS.sample @interactive = [:interactive] @canvas = [:canvas] || self.class.canvas @width = [:width] || 1 @drawing = true home draw end |
Instance Attribute Details
#canvas ⇒ Object (readonly)
Returns the value of attribute canvas.
15 16 17 |
# File 'lib/trtl.rb', line 15 def canvas @canvas end |
#color(color) ⇒ Object Also known as: pencolor
48 49 50 |
# File 'lib/trtl.rb', line 48 def color(color) @color = color.to_s end |
#heading ⇒ Object
Returns the value of attribute heading.
13 14 15 |
# File 'lib/trtl.rb', line 13 def heading @heading end |
#width(width) ⇒ Object
52 53 54 |
# File 'lib/trtl.rb', line 52 def width(width) @width = width end |
#x ⇒ Object
Returns the value of attribute x.
13 14 15 |
# File 'lib/trtl.rb', line 13 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
13 14 15 |
# File 'lib/trtl.rb', line 13 def y @y end |
Class Method Details
.canvas ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/trtl.rb', line 27 def self.canvas return @canvas if @canvas root = TkRoot.new(:title => 'trtl', :minsize => [CANVAS_WIDTH, CANVAS_HEIGHT]) @canvas = TkCanvas.new(root, :bg => 'black', :highlightthickness => 0, :width => CANVAS_WIDTH, :height => CANVAS_HEIGHT) @canvas.pack(:fill => 'both', :expand => 1) @canvas end |
Instance Method Details
#back(amount = 20) ⇒ Object Also known as: bk, backward
62 63 64 65 66 |
# File 'lib/trtl.rb', line 62 def back(amount = 20) new_x = (@x - dx * amount) new_y = (@y - dy * amount) move(new_x, new_y) end |
#circle(radius, extent = 360, steps = 360) ⇒ Object
TODO / TOFIX: This is horribly wrong with the fewer steps due to circumference varying ;-)
90 91 92 93 94 95 96 |
# File 'lib/trtl.rb', line 90 def circle(radius, extent = 360, steps = 360) circumference = (Math::PI * 2 * radius) * (extent / 360.0) steps.times do left extent / steps.to_f forward circumference / steps.to_f end end |
#dot(size = nil) ⇒ Object
84 85 86 87 |
# File 'lib/trtl.rb', line 84 def dot(size = nil) size ||= [@width + 4, @width * 2].max TkcOval.new(canvas, @x - size / 2, @y - size / 2, @x + size / 2, @y + size / 2, :fill => @color, :outline => @color) end |
#ensure_drawn ⇒ Object
113 114 115 |
# File 'lib/trtl.rb', line 113 def ensure_drawn sleep 30 end |
#forward(amount = 20) ⇒ Object Also known as: fd
56 57 58 59 60 |
# File 'lib/trtl.rb', line 56 def forward(amount = 20) new_x = (@x + dx * amount) new_y = (@y + dy * amount) move(new_x, new_y) end |
#home ⇒ Object
102 103 104 105 106 107 |
# File 'lib/trtl.rb', line 102 def home @x = HOME_X @y = HOME_Y @heading = 0 draw end |
#is_drawing? ⇒ Boolean
44 45 46 |
# File 'lib/trtl.rb', line 44 def is_drawing? @drawing end |
#left(offset) ⇒ Object Also known as: lt, turnleft
79 80 81 82 |
# File 'lib/trtl.rb', line 79 def left(offset) @heading = (@heading - offset) % 360 draw end |
#move(new_x, new_y) ⇒ Object Also known as: goto, setpos, setposition
68 69 70 71 72 |
# File 'lib/trtl.rb', line 68 def move(new_x, new_y) TkcLine.new(canvas, @x, @y, new_x, new_y, :width => @width, :fill => @color) if @drawing @x, @y = new_x, new_y draw end |
#pen_down ⇒ Object Also known as: pd, pendown, down
40 41 42 |
# File 'lib/trtl.rb', line 40 def pen_down @drawing = true end |
#pen_up ⇒ Object Also known as: pu, penup, up
36 37 38 |
# File 'lib/trtl.rb', line 36 def pen_up @drawing = false end |
#position ⇒ Object Also known as: pos
98 99 100 |
# File 'lib/trtl.rb', line 98 def position [@x, @y] end |
#right(offset) ⇒ Object Also known as: rt, turnright
74 75 76 77 |
# File 'lib/trtl.rb', line 74 def right(offset) @heading = (@heading + offset) % 360 draw end |
#sleep(time = 100000) ⇒ Object
109 110 111 |
# File 'lib/trtl.rb', line 109 def sleep(time = 100000) Tk.sleep(time) end |
#wait ⇒ Object
117 118 119 |
# File 'lib/trtl.rb', line 117 def wait ensure_drawn and gets end |