on as grosièrement le déplacement selon la route

This commit is contained in:
leo 2022-02-07 17:55:12 +01:00
parent a6339aecba
commit bde77c760d
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
4 changed files with 41 additions and 10 deletions

30
Car.py
View File

@ -1,5 +1,6 @@
import sumolib
import pygame as pg
import math
class Car:
def initPath(self):
@ -18,20 +19,41 @@ class Car:
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

View File

@ -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
View File

@ -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):

View File

@ -37,7 +37,9 @@ while running:
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()