⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stoplight.py

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 PY
📖 第 1 页 / 共 2 页
字号:
				cv.itemconfigure(self._south_light['GREEN'], fill='green')			elif color == 'yellow':				cv.itemconfigure(self._north_light['GREEN'], fill='white')				cv.itemconfigure(self._south_light['GREEN'], fill='white')				cv.itemconfigure(self._north_light['YELLOW'], fill='yellow')				cv.itemconfigure(self._south_light['YELLOW'], fill='yellow')	def SetTimer(self, timer):		self._timerID = self._canvas.after(self._timeouts[timer], self.Timeout)	def StopTimer(self):		if self._timerID >= 0:			self._canvas.after_cancel(self._timerID)			self._timerID = -1	def Timeout(self):		self._timerID = -1		self._fsm.Timeout()	def ResetLights(self):		cv = self._canvas		cv.itemconfigure(self._east_light['YELLOW'], fill='white')		cv.itemconfigure(self._west_light['YELLOW'], fill='white')		cv.itemconfigure(self._east_light['RED'], fill='white')		cv.itemconfigure(self._west_light['RED'], fill='white')		cv.itemconfigure(self._east_light['GREEN'], fill='white')		cv.itemconfigure(self._west_light['GREEN'], fill='white')		cv.itemconfigure(self._north_light['YELLOW'], fill='white')		cv.itemconfigure(self._south_light['YELLOW'], fill='white')		cv.itemconfigure(self._north_light['RED'], fill='white')		cv.itemconfigure(self._south_light['RED'], fill='white')		cv.itemconfigure(self._north_light['GREEN'], fill='white')		cv.itemconfigure(self._south_light['GREEN'], fill='white')	# InformVehicles --	#	#   Tell the vehicles that were waiting on the green light	#   that they can go now.	#	# Arguments:	#   direction   Which light turned green.	def InformVehicles(self, direction):		if      direction == 'north':			for vehicle in self._northVehicleList:				vehicle.lightGreen()			self._northVehicleList = []		elif direction == 'south':			for vehicle in self._southVehicleList:				vehicle.lightGreen()			self._southVehicleList = []		elif direction == 'east':			for vehicle in self._eastVehicleList:				vehicle.lightGreen()			self._eastVehicleList = []		elif direction == 'west':			for vehicle in self._westVehicleList:				vehicle.lightGreen()			self._westVehicleList = []	def DrawRoads(self):		cv = self._canvas		# 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 = (self.getRoadLengthX() / 2) - self._roadWidth / 2		YLength = (self.getRoadLengthY() / 2) - self._roadWidth / 2		# Calculate the major coordinates.		X1 = 0		Y1 = 0		X2 = XLength		Y2 = YLength		X3 = int(cv.cget('width')) / 2		Y3 = int(cv.cget('height')) / 2		X4 = int(cv.cget('width')) - XLength		Y4 = int(cv.cget('height')) - YLength		X5 = int(cv.cget('width'))		Y5 = int(cv.cget('height'))		# Put green lawns around the road.		cv.create_rectangle(X1, Y1, X2, Y2,			outline="",			fill='green',		)		cv.create_rectangle(X1, Y4, X2, Y5,			outline="",			fill='green',		)		cv.create_rectangle(X4, Y4, X5, Y5,			outline="",			fill='green',		)		cv.create_rectangle(X4, Y1, X5, Y2,			outline="",			fill='green',		)		# Draw four connected lines where each drawing uses three		# coordinates.		cv.create_line(X1, Y2, X2, Y2, X2, Y1)		cv.create_line(X4, Y1, X4, Y2, X5, Y2)		cv.create_line(X1, Y4, X2, Y4, X2, Y5)		cv.create_line(X4, Y5, X4, Y4, X5, Y4)		# Now draw the lane markings.		cv.create_line(X1, Y3, X2, Y3)		cv.create_line(X3, Y1, X3, Y2)		cv.create_line(X4, Y3, X5, Y3)		cv.create_line(X3, Y4, X3, Y5)	def DrawLights(self):		cv = self._canvas		# 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 = int(cv.cget('width')) / 2 - self._lightWidth / 2 - self._lightHeight		Y1 = int(cv.cget('height')) / 2 - self._lightWidth / 2 - self._lightHeight		X2 = X1 + self._lightHeight		Y2 = Y1 + self._lightHeight		X3 = X2 + self._lightWidth		Y3 = Y2 + self._lightWidth		X4 = X3 + self._lightHeight		Y4 = Y3 + self._lightHeight		# Draw the four stop lights boxes.		cv.create_rectangle(X2, Y1, X3, Y2,				outline='black',				fill='black',				width=1,		)		cv.create_rectangle(X1, Y2, X2, Y3,				outline='black',				fill='black',				width=1,		)		cv.create_rectangle(X2, Y3, X3, Y4,				outline='black',				fill='black',				width=1,		)		cv.create_rectangle(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		self._north_light['RED'] = cv.create_oval(				X2 + self._lightSpace,				Y1 + self._lightSpace,				X3 - self._lightSpace,				Y1 + self._lightSpace + self._lightDiameter,				outline='black',				fill='white'		)		self._north_light['YELLOW'] = cv.create_oval(				X2 + self._lightSpace,				Y1 + self._lightSpace * 2 + self._lightDiameter,				X3 - self._lightSpace,				Y1 + self._lightSpace * 2 + self._lightDiameter * 2,				outline='black',				fill='white'		)		self._north_light['GREEN'] = cv.create_oval(				X2 + self._lightSpace,				Y1 + self._lightSpace * 3 + self._lightDiameter * 2,				X3 - self._lightSpace,				Y1 + self._lightSpace * 3 + self._lightDiameter * 3,				outline='black',				fill='white'		)		self._west_light['RED'] = cv.create_oval(				X1 + self._lightSpace,				Y2 + self._lightSpace,				X1 + self._lightSpace + self._lightDiameter,				Y3 - self._lightSpace,				outline='black',				fill='white'		)		self._west_light['YELLOW'] = cv.create_oval(				X1 + self._lightSpace * 2 + self._lightDiameter,				Y2 + self._lightSpace,				X1 + self._lightSpace * 2 + self._lightDiameter * 2,				Y3 - self._lightSpace,				outline='black',				fill='white'		)		self._west_light['GREEN'] = cv.create_oval(				X1 + self._lightSpace * 3 + self._lightDiameter * 2,				Y2 + self._lightSpace,				X1 + self._lightSpace * 3 + self._lightDiameter * 3,				Y3 - self._lightSpace,				outline='black',				fill='white'		)		self._south_light['GREEN'] = cv.create_oval(				X2 + self._lightSpace,				Y3 + self._lightSpace,				X3 - self._lightSpace,				Y3 + self._lightSpace + self._lightDiameter,				outline='black',				fill='white'		)		self._south_light['YELLOW'] = cv.create_oval(				X2 + self._lightSpace,				Y3 + self._lightSpace * 2 + self._lightDiameter,				X3 - self._lightSpace,				Y3 + self._lightSpace * 2 + self._lightDiameter * 2,				outline='black',				fill='white'		)		self._south_light['RED'] = cv.create_oval(				X2 + self._lightSpace,				Y3 + self._lightSpace * 3 + self._lightDiameter * 2,				X3 - self._lightSpace,				Y3 + self._lightSpace * 3 + self._lightDiameter * 3,				outline='black',				fill='white'		)		self._east_light['GREEN'] = cv.create_oval(				X3 + self._lightSpace,				Y2 + self._lightSpace,				X3 + self._lightSpace + self._lightDiameter,				Y3 - self._lightSpace,				outline='black',				fill='white'		)		self._east_light['YELLOW'] = cv.create_oval(				X3 + self._lightSpace * 2 + self._lightDiameter,				Y2 + self._lightSpace,				X3 + self._lightSpace * 2 + self._lightDiameter * 2,				Y3 - self._lightSpace,				outline='black',				fill='white'		)		self._east_light['RED'] = cv.create_oval(				X3 + self._lightSpace * 3 + self._lightDiameter * 2,				Y2 + self._lightSpace,				X3 + self._lightSpace * 3 + self._lightDiameter * 3,				Y3 - self._lightSpace,				outline='black',				fill='white'		)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -