Class: Kame

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Kame

Returns a new instance of Kame.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kame.rb', line 5

def initialize(options = {}, &block)
  @lines = []
  @state = {
    :pen_down => false,
    :x => 0,
    :y => 0,
    :rotation => 0,
    :line_colour => KameColours::lookup[:black]
  }

  opts = {
    :width => 640,
    :height => 480,
    :paper => :white,
    :title => 'Kame',
    :speed => 10,
    :grid_size => 8,
    :grid_colour => :silver,
    :grid => false
  }.merge(options)
  
  instance_eval(&block)
  
  KameWindow.new(@lines, opts).show
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



82
83
84
# File 'lib/kame.rb', line 82

def method_missing(method_name, *args)
  puts "Sorry, but I don't know what '#{method_name}' means?"
end

Instance Method Details

#backward(distance) ⇒ Object



78
79
80
# File 'lib/kame.rb', line 78

def backward(distance)
  self.forward(-distance)
end

#colour(colour) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/kame.rb', line 52

def colour(colour)
  new_colour = KameColours::lookup[colour]
  if new_colour.nil?
    new_colour = KameColours::lookup[:black]
    puts "Can't find the colour '#{colour.to_s}', using black instead."
  end  
  @state[:line_colour] = new_colour
end

#forward(distance) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kame.rb', line 61

def forward(distance)
  theta = @state[:rotation].degrees_to_radians
  x = @state[:x] + distance * Math::cos(theta)
  y = @state[:y] + distance * Math::sin(theta)
  
  if pen_down?
    @lines << { 
      :colour => @state[:line_colour], 
      :from => { :x => @state[:x], :y => @state[:y] },
      :to => { :x => x, :y => y } 
    }
  end
  
  @state[:x] = x
  @state[:y] = y
end

#pen_down(colour = nil) ⇒ Object



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

def pen_down(colour = nil)
  @state[:pen_down] = true
  colour(colour) unless colour.nil?
end

#pen_down?Boolean

Returns:

  • (Boolean)


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

def pen_down?
  @state[:pen_down]
end

#pen_upObject



31
32
33
# File 'lib/kame.rb', line 31

def pen_up
  @state[:pen_down] = false
end

#turn_left(degrees) ⇒ Object



44
45
46
# File 'lib/kame.rb', line 44

def turn_left(degrees)
  @state[:rotation] -= degrees
end

#turn_right(degrees) ⇒ Object



48
49
50
# File 'lib/kame.rb', line 48

def turn_right(degrees)
  @state[:rotation] += degrees
end