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.



818
819
820
821
822
823
824
# File 'lib/pixelflow_canvas.rb', line 818

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.



826
827
828
# File 'lib/pixelflow_canvas.rb', line 826

def color
  @color
end

#pen_downObject

Returns the value of attribute pen_down.



826
827
828
# File 'lib/pixelflow_canvas.rb', line 826

def pen_down
  @pen_down
end

#phiObject

Returns the value of attribute phi.



826
827
828
# File 'lib/pixelflow_canvas.rb', line 826

def phi
  @phi
end

#xObject

Returns the value of attribute x.



826
827
828
# File 'lib/pixelflow_canvas.rb', line 826

def x
  @x
end

#yObject

Returns the value of attribute y.



826
827
828
# File 'lib/pixelflow_canvas.rb', line 826

def y
  @y
end

Instance Method Details

#forward(l) ⇒ Object



828
829
830
831
832
833
834
# File 'lib/pixelflow_canvas.rb', line 828

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



836
837
838
# File 'lib/pixelflow_canvas.rb', line 836

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

#turn_left(phi) ⇒ Object



840
841
842
# File 'lib/pixelflow_canvas.rb', line 840

def turn_left(phi)
    @phi -= phi
end

#turn_right(phi) ⇒ Object



844
845
846
# File 'lib/pixelflow_canvas.rb', line 844

def turn_right(phi)
    @phi += phi
end