load les definitions des voitures

This commit is contained in:
leo 2022-02-07 16:35:04 +01:00
parent 57bd97644a
commit 21e4d6a1db
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
5 changed files with 101 additions and 3 deletions

15
Car.py Normal file
View File

@ -0,0 +1,15 @@
import sumolib
import pygame as pg
class Car:
def __init__(self,carID,route,parentMap,surface):
self.id=carID
self.route=route
self.map=parentMap
self.pos=self.map.getEdge(route[0]).getFromNode().getCoord()
self.surf=surface
def draw(self):
pg.draw.circle(self.surf,(255,0,0),self.map.convertPos(self.pos),5)

18
CarController.py Normal file
View File

@ -0,0 +1,18 @@
import sumolib
from Car import Car
class CarController:
def __init__(self,path,parentMap,surface):
self.map=parentMap
self.surf=surface
self.cars=[]
self.fromPath(path)
def fromPath(self,path):
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):
for car in self.cars:
car.draw()
screen.blit(self.surf,(0,0))

9
Map.py
View File

@ -1,6 +1,6 @@
# Code pour gerer le reseau (la carte) # Code pour gerer le reseau (la carte)
# Pour l'instant c'est juste un wrapper autour de sumolib # Pour l'instant c'est juste un wrapper autour de sumolib
# mais si on se decide à utiliser notre propre format dans le futur ça faciliterat la transition # mais si on se decide à utiliser notre propre format dans le futur ça facilitera la transition
import sumolib import sumolib
import pygame as pg import pygame as pg
@ -14,7 +14,6 @@ class Map:
self.net = sumolib.net.readNet(path,withInternal=True) self.net = sumolib.net.readNet(path,withInternal=True)
def draw(self,screen): def draw(self,screen):
self.minVal=0
for edge in self.net.getEdges(): for edge in self.net.getEdges():
color=(255,255,255) color=(255,255,255)
if(edge.getFunction()=="internal"): if(edge.getFunction()=="internal"):
@ -26,6 +25,12 @@ class Map:
screen.blit(self.surf,(0,0)) screen.blit(self.surf,(0,0))
def getEdge(self,edgeID):
return self.net.getEdge(edgeID)
def getNode(self,nodeID):
return self.net.getNode(nodeID)
def convertPos(self,pos): def convertPos(self,pos):
bounds=self.net.getBoundary() bounds=self.net.getBoundary()
bounds[0]-=20 bounds[0]-=20

View File

@ -9,6 +9,7 @@ import pygame as pg
from pygame.locals import * from pygame.locals import *
from Map import Map from Map import Map
from CarController import CarController
pg.init() pg.init()
@ -17,10 +18,15 @@ pg.display.set_caption("Traffic")
clk = pg.time.Clock() clk = pg.time.Clock()
netSurf = pg.Surface(screen.get_size()) netSurf = pg.Surface(screen.get_size(),pg.SRCALPHA)
netSurf = netSurf.convert() netSurf = netSurf.convert()
m = Map("test.net.xml",netSurf) m = Map("test.net.xml",netSurf)
carSurf = pg.Surface(screen.get_size())
carSurf.set_colorkey((0, 0, 0))
carSurf = carSurf.convert()
cc = CarController("test3.rou.xml",m,carSurf)
running = True running = True
while running: while running:
for event in pg.event.get(): for event in pg.event.get():
@ -32,6 +38,7 @@ while running:
running = False running = False
m.draw(screen) m.draw(screen)
cc.draw(screen)
pg.display.flip() pg.display.flip()
clk.tick(60) clk.tick(60)

53
test3.rou.xml Normal file
View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- generated on 2022-02-07 08:16:35 by Eclipse SUMO netedit Version 1.10.0
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/netconvertConfiguration.xsd">
<input>
<sumo-net-file value="/home/leo/confPeip/test/test.net.xml"/>
</input>
<output>
<output-file value="/home/leo/confPeip/test/test.net.xml"/>
</output>
<processing>
<geometry.min-radius.fix.railways value="false"/>
<geometry.max-grade.fix value="false"/>
<offset.disable-normalization value="true"/>
<lefthand value="false"/>
</processing>
<junctions>
<no-turnarounds value="true"/>
<junctions.corner-detail value="5"/>
<junctions.limit-turn-speed value="5.5"/>
<rectangular-lane-cut value="false"/>
</junctions>
<pedestrian>
<walkingareas value="false"/>
</pedestrian>
<netedit>
<route-files value="/home/leo/confPeip/test/test3.rou.xml"/>
</netedit>
<report>
<aggregate-warnings value="5"/>
</report>
</configuration>
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vehicle id="vehicle_0" depart="0.00">
<route edges="gneE19 gneE20 gneE21 gneE22 gneE23 gneE25 gneE27 gneE28 gneE29 gneE30 gneE31" color="cyan"/>
</vehicle>
<vehicle id="vehicle_1" depart="0.00">
<route edges="gneE20 gneE21 gneE22 gneE23 gneE25 gneE25 gneE27 gneE28 gneE29 gneE30 gneE31" color="cyan"/>
</vehicle>
<vehicle id="vehicle_2" depart="0.00">
<route edges="gneE15 gneE16 gneE17 gneE18 gneE19 gneE20 gneE21 gneE22 gneE23 gneE24" color="cyan"/>
</vehicle>
</routes>