diff --git a/Car.py b/Car.py new file mode 100644 index 0000000..30cde09 --- /dev/null +++ b/Car.py @@ -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) + diff --git a/CarController.py b/CarController.py new file mode 100644 index 0000000..37086f5 --- /dev/null +++ b/CarController.py @@ -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)) diff --git a/Map.py b/Map.py index 186f30c..79b5378 100644 --- a/Map.py +++ b/Map.py @@ -1,6 +1,6 @@ # Code pour gerer le reseau (la carte) # 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 pygame as pg @@ -14,7 +14,6 @@ class Map: self.net = sumolib.net.readNet(path,withInternal=True) def draw(self,screen): - self.minVal=0 for edge in self.net.getEdges(): color=(255,255,255) if(edge.getFunction()=="internal"): @@ -25,6 +24,12 @@ class Map: pg.draw.lines(self.surf,color,False,list(pts)) 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): bounds=self.net.getBoundary() diff --git a/main.py b/main.py index ff798f2..8af0e3e 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ import pygame as pg from pygame.locals import * from Map import Map +from CarController import CarController pg.init() @@ -17,10 +18,15 @@ pg.display.set_caption("Traffic") clk = pg.time.Clock() -netSurf = pg.Surface(screen.get_size()) +netSurf = pg.Surface(screen.get_size(),pg.SRCALPHA) netSurf = netSurf.convert() 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 while running: for event in pg.event.get(): @@ -32,6 +38,7 @@ while running: running = False m.draw(screen) + cc.draw(screen) pg.display.flip() clk.tick(60) diff --git a/test3.rou.xml b/test3.rou.xml new file mode 100644 index 0000000..2ce4dc4 --- /dev/null +++ b/test3.rou.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + +