diff --git a/Car.py b/Car.py index 002620d..defcec5 100644 --- a/Car.py +++ b/Car.py @@ -1,5 +1,6 @@ import sumolib import pygame as pg +import math class Car: def initPath(self): @@ -12,26 +13,47 @@ class Car: self.laneShape=startEdge.getLane(laneId).getShape() if(inverted): self.laneShape.reverse() - + def __init__(self,carID,route,parentMap,surface): self.id=carID self.route=route self.map=parentMap self.index=0 + self.laneInd=0 startEdge=self.map.getEdge(route[0]) nextEdge=self.map.getEdge(route[1]) self.initPath() - self.pos=self.laneShape[0] + self.pos=list(self.laneShape[0]) + self.v=50 + self.a=0 self.surf=surface - def draw(self): - pg.draw.circle(self.surf,(255,0,0),self.map.convertPos(self.pos),5) + def draw(self,col): + pg.draw.circle(self.surf,col,self.map.convertPos(self.pos),5) def update(self,dt): lgt=self.v*dt while(lgt>0): - pass + endPos=self.laneShape[self.laneInd+1] + l=math.dist(self.pos,self.laneShape[self.laneInd+1]) + if lgt>=l: + lgt-=l + pos=list(self.laneShape[-1]) + self.laneInd+=1 + if(self.laneInd>=len(self.laneShape)-2): + self.laneInd=0 + self.index+=1 + if(self.index>=len(self.route)-2): + self.index=0 + + self.initPath() + self.pos=list(self.laneShape[self.laneInd]) + continue + adv=lgt/l + self.pos[0]+=(endPos[0]-self.pos[0])*adv + self.pos[1]+=(endPos[1]-self.pos[1])*adv + lgt=0 diff --git a/CarController.py b/CarController.py index 37086f5..d17a122 100644 --- a/CarController.py +++ b/CarController.py @@ -1,5 +1,6 @@ import sumolib from Car import Car +from matplotlib import cm class CarController: def __init__(self,path,parentMap,surface): @@ -12,7 +13,13 @@ class CarController: for vehicle in sumolib.xml.parse(path,"vehicle"): self.cars.append(Car(vehicle.id,vehicle.route[0].edges.split(),self.map,self.surf)) - def draw(self,screen): + def update(self): for car in self.cars: - car.draw() + car.update(1.0/60) + + def draw(self,screen): + cmap = cm.get_cmap('Spectral') + for ind,car in enumerate(self.cars): + color=cmap(ind/len(self.cars)) + car.draw([a*255 for a in color[0:3]]) screen.blit(self.surf,(0,0)) diff --git a/Map.py b/Map.py index 79b5378..1bf4fc6 100644 --- a/Map.py +++ b/Map.py @@ -6,11 +6,11 @@ import sumolib import pygame as pg class Map: - def __init__(self,path : str,surface : pg.Surface): + def __init__(self,path,surface): self.fromPath(path) self.surf=surface - def fromPath(self,path : str): + def fromPath(self,path): self.net = sumolib.net.readNet(path,withInternal=True) def draw(self,screen): diff --git a/main.py b/main.py index 8af0e3e..cecac5b 100644 --- a/main.py +++ b/main.py @@ -36,8 +36,10 @@ while running: running = False elif event.type == pg.KEYDOWN and event.key == pg.K_q: running = False - + + carSurf.fill((0,0,0)) m.draw(screen) + cc.update() cc.draw(screen) pg.display.flip()