📄 stoplight.py
字号:
## The contents of this file are subject to the Mozilla Public# License Version 1.1 (the "License"); you may not use this file# except in compliance with the License. You may obtain a copy of# the License at http://www.mozilla.org/MPL/## Software distributed under the License is distributed on an "AS# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or# implied. See the License for the specific language governing# rights and limitations under the License.## The Original Code is State Machine Compiler (SMC).## The Initial Developer of the Original Code is Charles W. Rapp.# Portions created by Charles W. Rapp are# Copyright (C) 2000 - 2003 Charles W. Rapp.# All Rights Reserved.## Contributor(s):# Port to Python by Francois Perrad, francois.perrad@gadz.org## Stoplight --## When a timer goes off, change the light's color as per the# state machine.## RCS ID# $Id: Stoplight.py,v 1.1 2005/05/28 17:48:29 cwrapp Exp $## CHANGE LOG# $Log: Stoplight.py,v $# Revision 1.1 2005/05/28 17:48:29 cwrapp# Added Python examples 1 - 4 and 7.##import Stoplight_smclass Stoplight: def __init__(self, canvas): self._canvas = canvas # Create the stop light's state machine. self._fsm = Stoplight_sm.Stoplight_sm(self) self._east_light = dict() self._west_light = dict() self._north_light = dict() self._south_light = dict() self._roadWidth = 38 self._lightDiameter = 6 self._lightSpace = 2 # Set the light height and width. self._lightWidth = self._lightDiameter + self._lightSpace * 2 self._lightHeight = self._lightDiameter * 3 + self._lightSpace * 4 self._northVehicleList = [] self._southVehicleList = [] self._eastVehicleList = [] self._westVehicleList = [] # Create the stoplight GUI. Draw the roads. self.DrawRoads() # Draw the stoplights. self.DrawLights() # Set each light timer. self._timeouts = { 'NSGreenTimer': 7000, 'EWGreenTimer': 5000, 'YellowTimer': 2000, } self._timerID = -1 # Uncomment to see debug output. #self._fsm.setDebugFlag(True) # getRoadLengthX -- # # Return the road's length in X direction. # # Arguments: # None. # # Results: # Pixel length of road in X direction. def getRoadLengthX(self): return int(self._canvas.cget('width')) # getRoadLengthY -- # # Return the road's length in Y direction in pixels. # # Arguments: # None. # # Results: # Pixel length of road in Y direction. def getRoadLengthY(self): return int(self._canvas.cget('height')) # getRoadWidth -- # # Return road's width in pixels. # # Arguments: # None. # # Results: # Road's width in pixels. def getRoadWidth(self): return self._roadWidth # getLight -- # # Return a specified stop lights color. # # Arguments: # direction Must be either north, south east or west. # # Results: # Returns the color for that direction. def getLight(self, direction): cv = self._canvas # The direction represents which way the vehicle # is facing. This is the opposite direction in which # the light is facing. if direction == 'north': RedLight = cv.itemcget(self._south_light['RED'], 'fill') YellowLight = cv.itemcget(self._south_light['YELLOW'], 'fill') GreenLight = cv.itemcget(self._south_light['GREEN'], 'fill') elif direction == 'south': RedLight = cv.itemcget(self._north_light['RED'], 'fill') YellowLight = cv.itemcget(self._north_light['YELLOW'], 'fill') GreenLight = cv.itemcget(self._north_light['GREEN'], 'fill') elif direction == 'east': RedLight = cv.itemcget(self._west_light['RED'], 'fill') YellowLight = cv.itemcget(self._west_light['YELLOW'], 'fill') GreenLight = cv.itemcget(self._west_light['GREEN'], 'fill') elif direction == 'west': RedLight = cv.itemcget(self._east_light['RED'], 'fill') YellowLight = cv.itemcget(self._east_light['YELLOW'], 'fill') GreenLight = cv.itemcget(self._east_light['GREEN'], 'fill') if RedLight == 'red': return 'red' elif YellowLight == 'yellow': return 'yellow' else: return 'green' # registerVehicle -- # # A vehicle is waiting for this light to turn green. # Add it to the list. When the light turns green, # the vehicle will be told about it. # # Arguments: # vehicle A vehicle object name. # direction The direction the vehicle is moving. def registerVehicle(self, vehicle, direction): if direction == 'north': self._northVehicleList.append(vehicle) elif direction == 'south': self._southVehicleList.append(vehicle) elif direction == 'east': self._eastVehicleList.append(vehicle) elif direction == 'west': self._westVehicleList.append(vehicle) # getQueueSize -- # # Return the number of vehicles waiting on a red in # a particular direction. # # Arguments: # direction The direction the vehicle is moving. # # Results: # The size of the red light queue for that direction. def getQueueSize(self, direction): if direction == 'north': return len(self._northVehicleList) elif direction == 'south': return len(self._southVehicleList) elif direction == 'east': return len(self._eastVehicleList) elif direction == 'west': return len(self._westVehicleList) # setLightTimer -- # # Set a particular light's timer. The value is given in # seconds, so convert to milliseconds. # # Arguments: # light NSGreenTimer, EWGreenTimer or YellowTimer. # time Light time in seconds. def setLightTimer(self, light, time): self._timeouts[light] = time * 1000 # start -- # # Start the demo running. # # Arguments: # None. def Start(self): self._fsm.Start() # pause -- # # Pause this demo. # # Arguments: # None. def Pause(self): self._fsm.Pause() # continue -- # # Continue this demo. # # Arguments: # None. def Continue(self): self._fsm.Continue() # stop -- # # Stop this demo. # # Arguments: # None. def Stop(self): self._fsm.Stop() # State Machine Actions. # # The following methods are called by the state machine.. def TurnLight(self, direction, color): cv = self._canvas if direction == 'EWLIGHT': if color == 'red': cv.itemconfigure(self._east_light['YELLOW'], fill='white') cv.itemconfigure(self._west_light['YELLOW'], fill='white') cv.itemconfigure(self._east_light['RED'], fill='red') cv.itemconfigure(self._west_light['RED'], fill='red') elif color == 'green': cv.itemconfigure(self._east_light['RED'], fill='white') cv.itemconfigure(self._west_light['RED'], fill='white') cv.itemconfigure(self._east_light['GREEN'], fill='green') cv.itemconfigure(self._west_light['GREEN'], fill='green') elif color == 'yellow': cv.itemconfigure(self._east_light['GREEN'], fill='white') cv.itemconfigure(self._west_light['GREEN'], fill='white') cv.itemconfigure(self._east_light['YELLOW'], fill='yellow') cv.itemconfigure(self._west_light['YELLOW'], fill='yellow') elif direction == 'NSLIGHT': if color == 'red': cv.itemconfigure(self._north_light['YELLOW'], fill='white') cv.itemconfigure(self._south_light['YELLOW'], fill='white') cv.itemconfigure(self._north_light['RED'], fill='red') cv.itemconfigure(self._south_light['RED'], fill='red') elif color == 'green': cv.itemconfigure(self._north_light['RED'], fill='white') cv.itemconfigure(self._south_light['RED'], fill='white') cv.itemconfigure(self._north_light['GREEN'], fill='green')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -