Note that this function is also a part of the Vehicle class defined above and needs to be indented accordingly. Now let’s talk about the move() function, which is one of the most important piece of code in our simulation. ![]() This is done to avoid overlapping of newly generated vehicles with the existing vehicles when there are a lot of vehicles stopped at a red light. Once this is done, we update the coordinates from where the vehicles are generated. This value of stop is used to control where the vehicles will stop when the signal is red. If there is no vehicle ahead already, then the stop value is set equal to defaultStop. If yes, we need to set the value of ‘ stop’ of the current vehicle taking into consideration the value of ‘ stop’ and the width/height of the vehicle ahead of it, as well as the stoppingGap. In the constructor, after initializing all the variables, we are checking if there are vehicles already present in the same direction and lane as the current vehicle. Let us understand the latter part of the constructor. move(): To control the movement of the vehicle according to the traffic light and the vehicles aheadĬlass Vehicle(): def _init_(self, lane, vehicleClass, direction_number, direction): ._init_(self) self.lane = lane self.vehicleClass = vehicleClass self.speed = speeds self.direction_number = direction_number self.direction = direction self.x = x self.y = y self.crossed = 0 vehicles.append(self) self.index = len(vehicles) - 1 path = "images/" + direction + "/" + vehicleClass + ".png" self.image = (path) if(len(vehicles)>1 and vehicles.crossed=0): if(direction='right'): self.stop = vehicles.stop - _rect().width - stoppingGap elif(direction='left'): self.stop = vehicles.stop + _rect().width + stoppingGap elif(direction='down'): self.stop = vehicles.stop - _rect().height - stoppingGap elif(direction='up'): self.stop = vehicles.stop + _rect().height + stoppingGap else: self.stop = defaultStop if(direction='right'): temp = _rect().width + stoppingGap x -= temp elif(direction='left'): temp = _rect().width + stoppingGap x += temp elif(direction='down'): temp = _rect().height + stoppingGap y -= temp elif(direction='up'): temp = _rect().height + stoppingGap y += temp simulation.add(self) def render(self, screen): screen.blit(self.image, (self.x, self.y)).render(): To display the image on screen.image: Represents the image to be rendered.index: Represents the relative position of the vehicle among the vehicles moving in the same direction and the same lane.crossed: Represents whether the vehicle has crossed the signal or not.y: Represents the current y-coordinate of the vehicle.x: Represents the current x-coordinate of the vehicle.direction: Represents the direction in text format.direction_number: Represents the direction - 0 for right, 1 for down, 2 for left, and 3 for up. ![]() ![]() speed: Represents the speed of the vehicle according to its class.vehicleClass: Represents the class of the vehicle such as car, bus, truck, or bike.The Vehicle class has the following attributes and methods: Vehicle: This is a class that represents objects of vehicles that we will be generating in the simulation. ![]() So we build a TrafficSignal class that has the following attributes:Ĭlass TrafficSignal: def _init_(self, red, yellow, green): self.red = red self.yellow = yellow een = green self.signalText = ""Ģ. Traffic Signal: We need to generate 4 traffic signals for our simulation.We have 2 classes that we need to define. Now let us build some classes whose objects we will generate in the simulation. Next, we initialize Pygame with the following code: pygame.init() simulation = () Defining Classes # Default values of signal timers in seconds defaultGreen = # Gap between vehicles stoppingGap = 15 # stopping gap movingGap = 15 # moving gap Initializing Pygame Next, we define some constants that will be used in the movement of vehicles in the simulation as well as in control of traffic signal timers. Folder structure of the project Defining constants
0 Comments
Leave a Reply. |