如果我没有以正确的方式问我的问题,请提前道歉。
试图让这段 Ruby 代码工作。我不明白的是如何让点击在底部调用收益函数(它是一个红绿灯),以便点击将循环通过收益选项。真、假、假将意味着灯是红色的,因为它在顶部,而底部的 2 个是假的。我也很难理解枚举器和产量。
class TrafficLight
include Enumerable
include TL
def each
yield [true, false, false]
yield [true, true, false]
yield [false, false, true]
yield [false, true, false]
end
end
class Bulb < Shoes::Shape
attr_accessor :stack
attr_accessor :left
attr_accessor :top
attr_accessor :switched_on
def initialize(stack, left, top, switched_on = false)
self.stack = stack #don't change.
self.left = left
self.top = top
self.switched_on = switched_on
draw left, top, bulb_colour
end
# HINT: Shouldn't need to change this method
def draw(left, top, colour
)
stack.app do
fill colour
stack.append do
oval left, top, 50
end
end
end
def bulb_colour
"#999999"
end
end
class GoBulb < Bulb
def bulb_colour
"#00FF30"
end
end
class WaitBulb < Bulb
def bulb_colour
"#FFFC00"
end
end
class StopBulb < Bulb
def bulb_colour
"#FF0000"
end
end
module TL
Go = "#00FF30"
Wait = "#FFFC00"
Stop = "#FF0000"
end
Shoes.app :title => "My Amazing Traffic Light", :width => 150, :height => 250 do
background "000", :curve => 10, :margin => 25
stroke black
@traffic_light = TrafficLight.new
@top = StopBulb.new self, 50, 40, true
@middle = Bulb.new self, 50, 100, true
@bottom = Bulb.new self, 50, 160, true
click do #make this switch on/off depending on what you click. Only 1 should be on
end
end
我已经用谷歌搜索并搜索过,但我得到的枚举器示例不允许我做我需要做的事情。非常感谢任何见解。