Class: Pixelflow::Turtle

Inherits:
Object
  • Object
show all
Defined in:
lib/pixelflow_canvas.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas) ⇒ Turtle

Returns a new instance of Turtle.



852
853
854
855
856
857
858
# File 'lib/pixelflow_canvas.rb', line 852

def initialize(canvas)
    @canvas = canvas
    @x = canvas.width / 2.0
    @y = canvas.height / 2.0
    @phi = 270.0
    @pen_down = true
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



860
861
862
# File 'lib/pixelflow_canvas.rb', line 860

def color
  @color
end

#pen_downObject

Returns the value of attribute pen_down.



860
861
862
# File 'lib/pixelflow_canvas.rb', line 860

def pen_down
  @pen_down
end

#phiObject

Returns the value of attribute phi.



860
861
862
# File 'lib/pixelflow_canvas.rb', line 860

def phi
  @phi
end

#xObject

Returns the value of attribute x.



860
861
862
# File 'lib/pixelflow_canvas.rb', line 860

def x
  @x
end

#yObject

Returns the value of attribute y.



860
861
862
# File 'lib/pixelflow_canvas.rb', line 860

def y
  @y
end

Instance Method Details

#forward(l) ⇒ Object



862
863
864
865
866
867
868
# File 'lib/pixelflow_canvas.rb', line 862

def forward(l)
    nx = @x + Math.cos(@phi * Math::PI / 180.0) * l
    ny = @y + Math.sin(@phi * Math::PI / 180.0) * l
    @canvas.draw_line(@x, @y, nx, ny) if @pen_down
    @x = nx
    @y = ny
end

#set_color(r = 0, g = nil, b = nil) ⇒ Object



870
871
872
# File 'lib/pixelflow_canvas.rb', line 870

def set_color(r = 0, g = nil, b = nil)
    @canvas.set_color(r, g, b)
end

#turn_left(phi) ⇒ Object



874
875
876
# File 'lib/pixelflow_canvas.rb', line 874

def turn_left(phi)
    @phi -= phi
end

#turn_right(phi) ⇒ Object



878
879
880
# File 'lib/pixelflow_canvas.rb', line 878

def turn_right(phi)
    @phi += phi
end