Class: Trtl

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @color = options[:color] || COLORS.sample
  @interactive = options[:interactive]
  @canvas = options[:canvas] || self.class.canvas
  @width = options[:width] || 1
  @drawing = true
  home
  draw
end

Instance Attribute Details

#canvasObject (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

#headingObject

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

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/trtl.rb', line 13

def x
  @x
end

#yObject

Returns the value of attribute y.



13
14
15
# File 'lib/trtl.rb', line 13

def y
  @y
end

Class Method Details

.canvasObject



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_drawnObject



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

#homeObject



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

Returns:

  • (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_downObject Also known as: pd, pendown, down



40
41
42
# File 'lib/trtl.rb', line 40

def pen_down
  @drawing = true
end

#pen_upObject Also known as: pu, penup, up



36
37
38
# File 'lib/trtl.rb', line 36

def pen_up
  @drawing = false
end

#positionObject 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

#waitObject



117
118
119
# File 'lib/trtl.rb', line 117

def wait
  ensure_drawn and gets
end