display sector times
This commit is contained in:
2 files changed
+31
-3
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3.11
|
#!/usr/bin/env python3.11
|
||||||
import pygame
|
import pygame
|
||||||
from models import Car, Background, Track
|
from models import Car, Background, Track
|
||||||
from util import load_image, limit
|
from util import load_image, limit, colors
|
||||||
import time
|
import time
|
||||||
|
|
||||||
class Window:
|
class Window:
|
||||||
@@ -12,6 +12,7 @@ class Window:
|
|||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
self._init_pygame()
|
self._init_pygame()
|
||||||
|
self.font = pygame.font.Font('freesansbold.ttf', 24)
|
||||||
self.screen = pygame.display.set_mode((self.size))
|
self.screen = pygame.display.set_mode((self.size))
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
@@ -22,10 +23,12 @@ class Window:
|
|||||||
self.last_sector = 3
|
self.last_sector = 3
|
||||||
self.sector = 3
|
self.sector = 3
|
||||||
|
|
||||||
|
self.lastLap = 0
|
||||||
self.fastestLap = 0
|
self.fastestLap = 0
|
||||||
self.startTime = None
|
self.startTime = None
|
||||||
self.sectorStart = None
|
self.sectorStart = None
|
||||||
self.sectorEnd = None
|
self.sectorEnd = None
|
||||||
|
self.last_sector_times = {i: 0 for i in range(1, 4)}
|
||||||
self.sector_times = {i: 0 for i in range(1, 4)}
|
self.sector_times = {i: 0 for i in range(1, 4)}
|
||||||
self.lapTime = 0
|
self.lapTime = 0
|
||||||
self.lapTimeValid = True
|
self.lapTimeValid = True
|
||||||
@@ -65,7 +68,6 @@ class Window:
|
|||||||
t = time.time()
|
t = time.time()
|
||||||
# if self.startTime != None: print(round(t - self.startTime, 3))
|
# if self.startTime != None: print(round(t - self.startTime, 3))
|
||||||
|
|
||||||
|
|
||||||
self.car.update()
|
self.car.update()
|
||||||
onTrack = self.track.detectCar(self.car)
|
onTrack = self.track.detectCar(self.car)
|
||||||
if not onTrack:
|
if not onTrack:
|
||||||
@@ -82,7 +84,9 @@ class Window:
|
|||||||
if self.sector == 1 and self.last_sector == 3 and self.startTime != None and prev_sector != self.sector:
|
if self.sector == 1 and self.last_sector == 3 and self.startTime != None and prev_sector != self.sector:
|
||||||
self.sectorEnd = time.time()
|
self.sectorEnd = time.time()
|
||||||
self.lapTime = round(self.sectorEnd - self.startTime, 3)
|
self.lapTime = round(self.sectorEnd - self.startTime, 3)
|
||||||
|
self.fastestLap = self.lapTime if (self.lapTime <= self.fastestLap or self.fastestLap == 0) and self.lapTimeValid else self.fastestLap
|
||||||
self.startTime = self.sectorEnd
|
self.startTime = self.sectorEnd
|
||||||
|
self.last_sector_times[self.last_sector] = self.sector_times[self.last_sector]
|
||||||
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
||||||
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
||||||
valid = "" if self.lapTimeValid else "( Invalid )"
|
valid = "" if self.lapTimeValid else "( Invalid )"
|
||||||
@@ -91,12 +95,14 @@ class Window:
|
|||||||
|
|
||||||
if self.sector == 2 and self.last_sector == 1 and prev_sector != self.sector:
|
if self.sector == 2 and self.last_sector == 1 and prev_sector != self.sector:
|
||||||
self.sectorEnd = time.time()
|
self.sectorEnd = time.time()
|
||||||
|
self.last_sector_times[self.last_sector] = self.sector_times[self.last_sector]
|
||||||
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
||||||
self.sectorStart = self.sectorEnd
|
self.sectorStart = self.sectorEnd
|
||||||
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
||||||
|
|
||||||
if self.sector == 3 and self.last_sector == 2 and prev_sector != self.sector:
|
if self.sector == 3 and self.last_sector == 2 and prev_sector != self.sector:
|
||||||
self.sectorEnd = time.time()
|
self.sectorEnd = time.time()
|
||||||
|
self.last_sector_times[self.last_sector] = self.sector_times[self.last_sector]
|
||||||
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
||||||
self.sectorStart = self.sectorEnd
|
self.sectorStart = self.sectorEnd
|
||||||
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
||||||
@@ -106,9 +112,21 @@ class Window:
|
|||||||
self.sectorStart = self.startTime
|
self.sectorStart = self.startTime
|
||||||
|
|
||||||
def _draw(self) -> None:
|
def _draw(self) -> None:
|
||||||
self.screen.fill((0, 0, 0))
|
self.screen.fill(colors.Black)
|
||||||
self.background.draw(self.screen)
|
self.background.draw(self.screen)
|
||||||
self.track.draw(self.screen)
|
self.track.draw(self.screen)
|
||||||
self.car.draw(self.screen)
|
self.car.draw(self.screen)
|
||||||
|
|
||||||
|
text = self.font.render(f"Fastest Lap: {self.fastestLap:.3f}", True, colors.Green, colors.Black)
|
||||||
|
self.screen.blit(text, text.get_rect())
|
||||||
|
for i in range(1, len(self.sector_times) + 1):
|
||||||
|
faster = self.sector_times[i] <= self.last_sector_times[i]
|
||||||
|
color = colors.Green if faster else colors.Yellow
|
||||||
|
diff = f"{'-' if faster else '+'}{abs(self.last_sector_times[i]-self.sector_times[i]):.3f}"
|
||||||
|
text = self.font.render(f"Sector {i}: {self.sector_times[i]:.3f} | {diff}", True, color, colors.Black)
|
||||||
|
textRect = text.get_rect()
|
||||||
|
textRect.center = (textRect.center[0], textRect.center[1] + i * 24)
|
||||||
|
self.screen.blit(text, textRect)
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
self.clock.tick(60)
|
self.clock.tick(60)
|
||||||
@@ -1,8 +1,18 @@
|
|||||||
#!/usr/bin/env python3.11
|
#!/usr/bin/env python3.11
|
||||||
from pygame.image import load
|
from pygame.image import load
|
||||||
from pygame import Surface
|
from pygame import Surface
|
||||||
|
from dataclasses import dataclass
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Colors:
|
||||||
|
White: tuple = (255, 255, 255)
|
||||||
|
Green: tuple = (0, 255, 0)
|
||||||
|
Yellow: tuple = (230, 193, 32)
|
||||||
|
Black: tuple = (0, 0, 0)
|
||||||
|
|
||||||
|
colors = Colors()
|
||||||
|
|
||||||
def load_image(filename: str) -> Surface:
|
def load_image(filename: str) -> Surface:
|
||||||
if '.' in filename and filename.split('.')[1] not in ['png', 'jpg', 'jpeg']:
|
if '.' in filename and filename.split('.')[1] not in ['png', 'jpg', 'jpeg']:
|
||||||
raise Exception(f'Invalid filename: must be of filetype \'.png\' \'.jpg\' or \'.jpeg\' not of .{filename.split(".")[1]}')
|
raise Exception(f'Invalid filename: must be of filetype \'.png\' \'.jpg\' or \'.jpeg\' not of .{filename.split(".")[1]}')
|
||||||
|
|||||||
Reference in new issue
Block a user