📄 stoplight.rb
字号:
@_canvas.itemconfigure(@_south_light['GREEN'], 'fill' => 'green') elsif color == 'yellow' then @_canvas.itemconfigure(@_north_light['GREEN'], 'fill' => 'white') @_canvas.itemconfigure(@_south_light['GREEN'], 'fill' => 'white') @_canvas.itemconfigure(@_north_light['YELLOW'], 'fill' => 'yellow') @_canvas.itemconfigure(@_south_light['YELLOW'], 'fill' => 'yellow') end end end def SetTimer(timer) @_timerID = TkAfter.new(@_timeouts[timer], 1, proc { timeout } ) @_timerID.start end def StopTimer() unless @_timerID.nil? then @_timerID.cancel @_timerID = nil end end def timeout() @_timerID = nil @_fsm.Timeout end def ResetLights() @_canvas.itemconfigure(@_east_light['YELLOW'], 'fill' => 'white') @_canvas.itemconfigure(@_west_light['YELLOW'], 'fill' => 'white') @_canvas.itemconfigure(@_east_light['RED'], 'fill' => 'white') @_canvas.itemconfigure(@_west_light['RED'], 'fill' => 'white') @_canvas.itemconfigure(@_east_light['GREEN'], 'fill' => 'white') @_canvas.itemconfigure(@_west_light['GREEN'], 'fill' => 'white') @_canvas.itemconfigure(@_north_light['YELLOW'], 'fill' => 'white') @_canvas.itemconfigure(@_south_light['YELLOW'], 'fill' => 'white') @_canvas.itemconfigure(@_north_light['RED'], 'fill' => 'white') @_canvas.itemconfigure(@_south_light['RED'], 'fill' => 'white') @_canvas.itemconfigure(@_north_light['GREEN'], 'fill' => 'white') @_canvas.itemconfigure(@_south_light['GREEN'], 'fill' => 'white') end # InformVehicles -- # # Tell the vehicles that were waiting on the green light # that they can go now. # # Arguments: # direction Which light turned green. def InformVehicles(direction) if direction == 'north' then for vehicle in @_northVehicleList do vehicle.lightGreen end @_northVehicleList = [] elsif direction == 'south' then for vehicle in @_southVehicleList do vehicle.lightGreen end @_southVehicleList = [] elsif direction == 'east' then for vehicle in @_eastVehicleList do vehicle.lightGreen end @_eastVehicleList = [] elsif direction == 'west' then for vehicle in @_westVehicleList do vehicle.lightGreen end @_westVehicleList = [] end end def drawRoads() # The roads are drawn as follows: # # (x2,y1) (x4,y1) # | | | # | | # | | | # (x1,y2) | | (x5,y2) # ------------+ | +------------ # (x2,y2) (x4,y2) # - - - - - - - - - - - - # (x2,y4) (x4,y4) (x5,y4) # ------------+ +------------ # (x1,y4) | | | # | | # | | | # | | # (x2,y5) |(x4,y5) # Calculate the line segment's length. xLength = getRoadLengthX / 2 - @_roadWidth / 2 yLength = getRoadLengthY / 2 - @_roadWidth / 2 # Calculate the major coordinates. x1 = 0 y1 = 0 x2 = xLength y2 = yLength x3 = @_canvas.cget('width').to_i / 2 y3 = @_canvas.cget('height').to_i / 2 x4 = @_canvas.cget('width').to_i - xLength y4 = @_canvas.cget('height').to_i - yLength x5 = @_canvas.cget('width').to_i y5 = @_canvas.cget('height').to_i # Put green lawns around the road. TkcRectangle.new(@_canvas, x1, y1, x2, y2, 'outline' => "", 'fill' => 'green' ) TkcRectangle.new(@_canvas, x1, y4, x2, y5, 'outline' => "", 'fill' => 'green' ) TkcRectangle.new(@_canvas, x4, y4, x5, y5, 'outline' => "", 'fill' => 'green' ) TkcRectangle.new(@_canvas, x4, y1, x5, y2, 'outline' => "", 'fill' => 'green' ) # Draw four connected lines where each drawing uses three # coordinates. TkcLine.new(@_canvas, x1, y2, x2, y2, x2, y1) TkcLine.new(@_canvas, x4, y1, x4, y2, x5, y2) TkcLine.new(@_canvas, x1, y4, x2, y4, x2, y5) TkcLine.new(@_canvas, x4, y5, x4, y4, x5, y4) # Now draw the lane markings. TkcLine.new(@_canvas, x1, y3, x2, y3) TkcLine.new(@_canvas, x3, y1, x3, y2) TkcLine.new(@_canvas, x4, y3, x5, y3) TkcLine.new(@_canvas, x3, y4, x3, y5) end def drawLights() # The lights are drawns as follows: # # y1 +---+ # | o |green # | o |yellow # | o |red # y2 +-------+---+-------+ # | o o o | | o o o | # y3 +-------+---+-------+ # | o |red # | o |yellow # | o |green # y4 +---+ # # x1 x2 x3 x4 # Store each light as a separate element in a table. # Figure out the coordinates for the stoplights. x1 = @_canvas.cget('width').to_i / 2 - @_lightWidth / 2 - @_lightHeight y1 = @_canvas.cget('height').to_i / 2 - @_lightWidth / 2 - @_lightHeight x2 = x1 + @_lightHeight y2 = y1 + @_lightHeight x3 = x2 + @_lightWidth y3 = y2 + @_lightWidth x4 = x3 + @_lightHeight y4 = y3 + @_lightHeight # Draw the four stop lights boxes. TkcRectangle.new(@_canvas, x2, y1, x3, y2, 'outline' => 'black', 'fill' => 'black', 'width' => 1 ) TkcRectangle.new(@_canvas, x1, y2, x2, y3, 'outline' => 'black', 'fill' => 'black', 'width' => 1 ) TkcRectangle.new(@_canvas, x2, y3, x3, y4, 'outline' => 'black', 'fill' => 'black', 'width' => 1 ) TkcRectangle.new(@_canvas, x3, y2, x4, y3, 'outline' => 'black', 'fill' => 'black', 'width' => 1 ) # Draw the lights within the stoplights. Save the # canvas items into an array because they will be # referenced later. Because there are two lights @_north_light['RED'] = TkcOval.new(@_canvas, x2 + @_lightSpace, y1 + @_lightSpace, x3 - @_lightSpace, y1 + @_lightSpace + @_lightDiameter, 'outline' => 'black', 'fill' => 'white' ) @_north_light['YELLOW'] = TkcOval.new(@_canvas, x2 + @_lightSpace, y1 + @_lightSpace * 2 + @_lightDiameter, x3 - @_lightSpace, y1 + @_lightSpace * 2 + @_lightDiameter * 2, 'outline' => 'black', 'fill' => 'white' ) @_north_light['GREEN'] = TkcOval.new(@_canvas, x2 + @_lightSpace, y1 + @_lightSpace * 3 + @_lightDiameter * 2, x3 - @_lightSpace, y1 + @_lightSpace * 3 + @_lightDiameter * 3, 'outline' => 'black', 'fill' => 'white' ) @_west_light['RED'] = TkcOval.new(@_canvas, x1 + @_lightSpace, y2 + @_lightSpace, x1 + @_lightSpace + @_lightDiameter, y3 - @_lightSpace, 'outline' => 'black', 'fill' => 'white' ) @_west_light['YELLOW'] = TkcOval.new(@_canvas, x1 + @_lightSpace * 2 + @_lightDiameter, y2 + @_lightSpace, x1 + @_lightSpace * 2 + @_lightDiameter * 2, y3 - @_lightSpace, 'outline' => 'black', 'fill' => 'white' ) @_west_light['GREEN'] = TkcOval.new(@_canvas, x1 + @_lightSpace * 3 + @_lightDiameter * 2, y2 + @_lightSpace, x1 + @_lightSpace * 3 + @_lightDiameter * 3, y3 - @_lightSpace, 'outline' => 'black', 'fill' => 'white' ) @_south_light['GREEN'] = TkcOval.new(@_canvas, x2 + @_lightSpace, y3 + @_lightSpace, x3 - @_lightSpace, y3 + @_lightSpace + @_lightDiameter, 'outline' => 'black', 'fill' => 'white' ) @_south_light['YELLOW'] = TkcOval.new(@_canvas, x2 + @_lightSpace, y3 + @_lightSpace * 2 + @_lightDiameter, x3 - @_lightSpace, y3 + @_lightSpace * 2 + @_lightDiameter * 2, 'outline' => 'black', 'fill' => 'white' ) @_south_light['RED'] = TkcOval.new(@_canvas, x2 + @_lightSpace, y3 + @_lightSpace * 3 + @_lightDiameter * 2, x3 - @_lightSpace, y3 + @_lightSpace * 3 + @_lightDiameter * 3, 'outline' => 'black', 'fill' => 'white' ) @_east_light['GREEN'] = TkcOval.new(@_canvas, x3 + @_lightSpace, y2 + @_lightSpace, x3 + @_lightSpace + @_lightDiameter, y3 - @_lightSpace, 'outline' => 'black', 'fill' => 'white' ) @_east_light['YELLOW'] = TkcOval.new(@_canvas, x3 + @_lightSpace * 2 + @_lightDiameter, y2 + @_lightSpace, x3 + @_lightSpace * 2 + @_lightDiameter * 2, y3 - @_lightSpace, 'outline' => 'black', 'fill' => 'white' ) @_east_light['RED'] = TkcOval.new(@_canvas, x3 + @_lightSpace * 3 + @_lightDiameter * 2, y2 + @_lightSpace, x3 + @_lightSpace * 3 + @_lightDiameter * 3, y3 - @_lightSpace, 'outline' => 'black', 'fill' => 'white' ) endend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -