I am currently working on a traffic light simulator with lanes. Currently, I have phases that work through the lanes and the respective lights but I want to make the lights smarter. I want to know how to skip past the lane if it is empty of cars (move past a list if it is empty).
I believe an if statement is sufficient enough but I don't know the syntax of moving past an empty list. Here's all the code I believe that is relevant
#Initialise each controller, one for each direction
northController = TrafficLightController("NORTH")
eastController = TrafficLightController("EAST")
southController = TrafficLightController("SOUTH")
westController = TrafficLightController("WEST")
#Create a lane as a list to store cars for each direction
northLane = []
eastLane = []
southLane = []
westLane = []
""" A function to remove cars from a lane based on their intended direction. """
def removeCars(lane,direction):
for i in range(0,len(lane)):
car = lane[i]
if car.getDirection()==direction:
lane.remove(car)
break
""" A function which tells the car to move depending on the sensor. """
def sensor(controller,lane):
#If there are still more cars in the lane
if len(lane) > 0:
#Find out which lights are on
matrix = controller.getTL().whatsOn()
#If the green light and turning green light are on, or the orange light and turning orange light are on, remove the next car in the lane
if (matrix[2]==True and matrix[3]==True) or (matrix[4]==True and matrix[5]==True):
del lane[0]
#If the green turning light or the orange turning light are on, remove the next car in the lane which is turning right
elif matrix[3]==True or matrix[5]==True:
removeCars(lane,"RIGHT")
#If the green light or the orange light are on, remove the cars which are going straight or turning left from the lane
elif matrix[2]==True or matrix[4]==True:
removeCars(lane,"STRAIGHT")
removeCars(lane,"LEFT")
""" A function which determines which lightSignals to set off and when. """
def lightSignals():
while True:
northController.turningCycle()
northController.straightAheadCycle()
eastController.turningCycle()
eastController.straightAheadCycle()
southController.turningCycle()
southController.straightAheadCycle()
westController.turningCycle()
westController.straightAheadCycle()
if len(lane) == 0:
#Code to make the lights smarter goes here.
Aucun commentaire:
Enregistrer un commentaire