import pygame import random import pickle import utils roadWidth=int(utils.meterToPixel(4)) class Road(object): def __init__(self, text_Name): self.LoadNodesFromFile(text_Name) def Draw(self, screen, pygame): self.DrawList(screen,pygame,self.roads['Main'],(125,125,125)) self.DrawNodeLines(screen,pygame,self.roads['Start'],self.roads['Main'],(125,175,125)) self.DrawNodeLines(screen,pygame,self.roads['End'],self.roads['Main'],(175,125,125)) def DrawList(self,screen,pygame,node_List,color): pygame.draw.lines(screen,color,True,node_List,roadWidth) def DrawNodeLines(self,screen,pygame,node_List_With_Index,node_List,color): for iNode in range(0,len(node_List_With_Index)): pygame.draw.line(screen,color,node_List_With_Index[iNode][0],node_List[node_List_With_Index[iNode][1]],roadWidth) def LoadNodesFromFile(self, text_Name): f = open(text_Name) self.roads = pickle.load(f) self.nNodes=len(self.roads['Main']) self.nEntrances=len(self.roads['Start'])
def update(self, vehicles, delta_t): acceleration = 0 distance = self.check_obstacles(vehicles) if self.velocity < self.max_velocity and self.RightOfPassage == 1: acceleration = self.acceleration elif self.velocity < self.max_velocity and self.RightOfPassage == 0 and distance < utils.meterToPixel(10.0): acceleration = 0 elif self.velocity < self.max_velocity and self.RightOfPassage == 0 and distance > utils.meterToPixel(10.0): acceleration = self.acceleration if distance < 1000 and distance > 0: acceleration = -2.0 * (self.velocity * self.velocity) / (distance) if distance < 0: self.velocity = 0 self.velocity = self.velocity + acceleration * delta_t if distance > 0 and distance < 5: self.velocity = 0 if self.velocity < self.min_velocity and self.RightOfPassage == 1: self.velocity = self.min_velocity elif self.velocity < 0: self.velocity = 0 self.update_next_node(delta_t) if self.active: self.direction = self.get_direction() self.position = ( self.position[0] + math.cos(self.direction) * self.velocity * delta_t, self.position[1] + math.sin(self.direction) * self.velocity * delta_t, )
import math import utils import random from vehicle import Vehicle max_velocity = utils.meterToPixel(5) # Class variable shared by all instances min_velocity = utils.meterToPixel(1) max_acceleration = 0.1 exit_probability = 0.25 # Set to other then 0 when Active flag is in play range_of_sight = utils.meterToPixel(4.0) vision_angle = math.pi/5 length=utils.meterToPixel(0.5)/2 width=utils.meterToPixel(0.3)/2 length=utils.meterToPixel(1.5)/2 width=utils.meterToPixel(1)/2 class Pedrestian(Vehicle): pedrestian_number = 0 def __init__(self, road): super(Pedrestian, self).__init__(road, "Pedrestian") Pedrestian.pedrestian_number += 1 def spawn(self): self.startNode=0 self.nextNode=1 self.RightOfPassage=1 self.stopped=0 self.velocity=max_velocity
import math import utils import random max_acceleration = utils.meterToPixel(20) exit_probability = 0.25 # Set to other then 0 when Active flag is in play vision_angle = math.pi / 5 vision_angle_entrance = math.pi / 2 class Vehicle(object): vehicle_number = 0 def __init__(self, road, vehicle_type): self.vehicle_type = vehicle_type self.road = road self.vision_angle_entrance = vision_angle_entrance self.RightOfPassage = 0 self.spawn() self.number = Vehicle.vehicle_number Vehicle.vehicle_number += 1 def spawn(self): self.position = self.road.GetNodePosition(self.startNode) self.active = True self.acceleration = max_acceleration self.direction = self.get_direction() def update(self, vehicles, delta_t): acceleration = 0
import math import utils import random from vehicle import Vehicle max_velocity = 200 # Class variable shared by all instances min_velocity = 0 max_acceleration = 200 exit_probability = 0.25 # Set to other then 0 when Active flag is in play range_of_sight = utils.meterToPixel(10) vision_angle = math.pi/8 stop_time = 1 length=utils.meterToPixel(12)/2 width=utils.meterToPixel(2.5)/2 class Pedestrian(Vehicle): pedestrian_number = 0 def __init__(self, road): super(pedestrian, self).__init__(road, "Pedestrian") Pedestrian.pedestrian_number += 1 def spawn(self): self.startNode=0 self.nextNode=1 self.RightOfPassage=1 self.stopTimer=stop_time self.stopped=0 self.max_velocity = min_velocity + (max_velocity-min_velocity)*(0.5+0.5*random.random())