on as grosièrement le déplacement selon la route
This commit is contained in:
parent
a6339aecba
commit
bde77c760d
32
Car.py
32
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
|
||||
|
@ -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))
|
||||
|
4
Map.py
4
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user