-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinfoDisplayer.py
47 lines (41 loc) · 1.65 KB
/
infoDisplayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pygame
class InfoDisplayer():
def __init__(self, screen, road, simulationManager):
self.screen = screen
self.road = road
self.simulationManager = simulationManager
self.string = ''
self.cells = road.getCellCount()
self.timeFactor = 0
self.font = pygame.font.SysFont("monospace", 15)
self.keysInfo = "Space - pause, M - 2x faster, N - 2x slower, S - step, D - 500 steps"
self.text = [self.keysInfo]
self.renderLabels()
def renderLabels(self):
self.labels = list(map(lambda x: self.font.render(x, 1, (255, 255, 0)), self.text))
def update(self):
totalCars, avgSpeed = self.road.getAvgCarSpeed()
deadCars = self.road.deadCars
updates = self.road.updates
congestion = totalCars*100/self.cells
text = [self.text[0]]
text.append("avg speed: {:0.3f}".format(avgSpeed, deadCars))
text.append("updates: " + str(updates))
text.append("dead cars: " + str(deadCars))
text.append("congestion: {:0.3f}".format(congestion))
text.append('')
text.append(self.keysInfo)
self.text = text
self.renderLabels()
def updateSpeed(self):
if len(self.text) == 0: return
if self.timeFactor != self.simulationManager.timeFactor:
self.timeFactor = self.simulationManager.timeFactor
self.text[0] = "speed: {0}x".format(self.timeFactor)
self.renderLabels()
def draw(self):
self.updateSpeed()
y = self.screen.get_height() - 160
for label in self.labels:
self.screen.blit(label, (20, y))
y += 20