secotr timing with poor code, could've done better probably
This commit is contained in:
6 files changed
+73
-20
No files matched your search
@@ -2,9 +2,10 @@
|
|||||||
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
|
||||||
|
import time
|
||||||
|
|
||||||
class Window:
|
class Window:
|
||||||
def __init__(self, caption, size):
|
def __init__(self, caption, size) -> None:
|
||||||
self.caption = caption
|
self.caption = caption
|
||||||
self.size = size
|
self.size = size
|
||||||
self.background = Background('track.jpg', [0, 0], size)
|
self.background = Background('track.jpg', [0, 0], size)
|
||||||
@@ -17,18 +18,30 @@ class Window:
|
|||||||
self.track = Track('track_mask.png', [0, 0], size)
|
self.track = Track('track_mask.png', [0, 0], size)
|
||||||
self.car = Car(880, 300)
|
self.car = Car(880, 300)
|
||||||
|
|
||||||
def main_loop(self):
|
self.sectors = {i: Track(f'sector_{i}.png', [0,0], size) for i in range(1, 4)}
|
||||||
|
self.last_sector = 3
|
||||||
|
self.sector = 3
|
||||||
|
|
||||||
|
self.fastestLap = 0
|
||||||
|
self.startTime = None
|
||||||
|
self.sectorStart = None
|
||||||
|
self.sectorEnd = None
|
||||||
|
self.sector_times = {i: 0 for i in range(1, 4)}
|
||||||
|
self.lapTime = 0
|
||||||
|
self.lapTimeValid = True
|
||||||
|
|
||||||
|
def main_loop(self) -> None:
|
||||||
self.running = True
|
self.running = True
|
||||||
while self.running:
|
while self.running:
|
||||||
self._handle_input()
|
self._handle_input()
|
||||||
self._process_game_logic()
|
self._process_game_logic()
|
||||||
self._draw()
|
self._draw()
|
||||||
|
|
||||||
def _init_pygame(self):
|
def _init_pygame(self) -> None:
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption(self.caption)
|
pygame.display.set_caption(self.caption)
|
||||||
|
|
||||||
def _handle_input(self):
|
def _handle_input(self) -> None:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT or (
|
if event.type == pygame.QUIT or (
|
||||||
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
|
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
|
||||||
@@ -48,16 +61,54 @@ class Window:
|
|||||||
if pressed[pygame.K_LEFT] and round(self.car.speed, 2) > 0.55: self.car.angle += a
|
if pressed[pygame.K_LEFT] and round(self.car.speed, 2) > 0.55: self.car.angle += a
|
||||||
if pressed[pygame.K_RIGHT] and round(self.car.speed, 2) > 0.55: self.car.angle -= a
|
if pressed[pygame.K_RIGHT] and round(self.car.speed, 2) > 0.55: self.car.angle -= a
|
||||||
|
|
||||||
def _process_game_logic(self):
|
def _process_game_logic(self) -> None:
|
||||||
self.car.update()
|
t = time.time()
|
||||||
self.track.detectCar(self.car)
|
# if self.startTime != None: print(round(t - self.startTime, 3))
|
||||||
|
|
||||||
def _draw(self):
|
|
||||||
|
self.car.update()
|
||||||
|
onTrack = self.track.detectCar(self.car)
|
||||||
|
if not onTrack:
|
||||||
|
self.lapTimeValid = False
|
||||||
|
|
||||||
|
prev_sector = self.sector
|
||||||
|
for i in range(1, 4): self.sector = i if self.sectors[i].detectCar(self.car) else self.sector
|
||||||
|
self.last_sector = prev_sector if prev_sector != self.sector else self.last_sector
|
||||||
|
|
||||||
|
if self.sector != prev_sector:
|
||||||
|
if (self.last_sector == 1 and self.sector == 3) or (self.last_sector == 2 and self.sector == 1): self.lapTimeValid = False
|
||||||
|
print('Now in sector ', self.sector, ' Last sector was', self.last_sector)
|
||||||
|
|
||||||
|
if self.sector == 1 and self.last_sector == 3 and self.startTime != None and prev_sector != self.sector:
|
||||||
|
self.sectorEnd = time.time()
|
||||||
|
self.lapTime = round(self.sectorEnd - self.startTime, 3)
|
||||||
|
self.startTime = self.sectorEnd
|
||||||
|
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
||||||
|
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
||||||
|
valid = "" if self.lapTimeValid else "( Invalid )"
|
||||||
|
self.lapTimeValid = True
|
||||||
|
print(f"Lap Time: {self.lapTime} {valid}")
|
||||||
|
|
||||||
|
if self.sector == 2 and self.last_sector == 1 and prev_sector != self.sector:
|
||||||
|
self.sectorEnd = time.time()
|
||||||
|
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
||||||
|
self.sectorStart = self.sectorEnd
|
||||||
|
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:
|
||||||
|
self.sectorEnd = time.time()
|
||||||
|
self.sector_times[self.last_sector] = round(self.sectorEnd - self.sectorStart, 3)
|
||||||
|
self.sectorStart = self.sectorEnd
|
||||||
|
print(f"Sector {self.last_sector}: {self.sector_times[self.last_sector]}")
|
||||||
|
|
||||||
|
if self.sector == 1 and self.last_sector == 3 and self.startTime == None:
|
||||||
|
self.startTime = time.time()
|
||||||
|
self.sectorStart = self.startTime
|
||||||
|
|
||||||
|
def _draw(self) -> None:
|
||||||
self.screen.fill((0, 0, 0))
|
self.screen.fill((0, 0, 0))
|
||||||
self.background.draw(self.screen)
|
self.background.draw(self.screen)
|
||||||
# ---- debug mask ----
|
self.track.draw(self.screen)
|
||||||
# self.screen.blit(self.track.mask_image, (0, 0))
|
|
||||||
# self.screen.blit(self.car.mask_image, self.car.mask_rect)
|
|
||||||
self.car.draw(self.screen)
|
self.car.draw(self.screen)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
self.clock.tick(60)
|
self.clock.tick(60)
|
||||||
@@ -5,18 +5,18 @@ from util import load_image
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
class Background(sprite.Sprite):
|
class Background(sprite.Sprite):
|
||||||
def __init__(self, filename, location, size):
|
def __init__(self, filename, location, size) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.image = load_image(filename)
|
self.image = load_image(filename)
|
||||||
self.image = scale(self.image, size)
|
self.image = scale(self.image, size)
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.rect.left, self.rect.top = location
|
self.rect.left, self.rect.top = location
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window) -> None:
|
||||||
window.blit(self.image, self.rect)
|
window.blit(self.image, self.rect)
|
||||||
|
|
||||||
class Car:
|
class Car:
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y) -> None:
|
||||||
# Load image and set to scale
|
# Load image and set to scale
|
||||||
img = load_image('car.png')
|
img = load_image('car.png')
|
||||||
img = rotate(img, 90)
|
img = rotate(img, 90)
|
||||||
@@ -38,11 +38,11 @@ class Car:
|
|||||||
self.angle = 180
|
self.angle = 180
|
||||||
self.speed = 0
|
self.speed = 0
|
||||||
|
|
||||||
def update(self):
|
def update(self) -> None:
|
||||||
self.x -= self.speed * math.sin(math.radians(self.angle))
|
self.x -= self.speed * math.sin(math.radians(self.angle))
|
||||||
self.y -= self.speed * math.cos(math.radians(-self.angle))
|
self.y -= self.speed * math.cos(math.radians(-self.angle))
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window) -> None:
|
||||||
self.rect.topleft = (int(self.x), int(self.y))
|
self.rect.topleft = (int(self.x), int(self.y))
|
||||||
rotated = rotate(self.surface, self.angle)
|
rotated = rotate(self.surface, self.angle)
|
||||||
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
|
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
|
||||||
@@ -54,7 +54,7 @@ class Car:
|
|||||||
self.mask_rect = self.mask_image.get_rect(center = surface_rect.center)
|
self.mask_rect = self.mask_image.get_rect(center = surface_rect.center)
|
||||||
|
|
||||||
class Track:
|
class Track:
|
||||||
def __init__(self, filename, location, size):
|
def __init__(self, filename, location, size) -> None:
|
||||||
self.image = load_image(filename).convert_alpha()
|
self.image = load_image(filename).convert_alpha()
|
||||||
self.image = scale(self.image, size)
|
self.image = scale(self.image, size)
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
@@ -63,6 +63,8 @@ class Track:
|
|||||||
self.mask = mask.from_surface(self.image)
|
self.mask = mask.from_surface(self.image)
|
||||||
self.mask_image = self.mask.to_surface()
|
self.mask_image = self.mask.to_surface()
|
||||||
|
|
||||||
def detectCar(self, car: Car):
|
def draw(self, window) -> None:
|
||||||
if not self.mask.overlap(car.mask, (car.x, car.y)):
|
window.blit(self.image, self.rect)
|
||||||
pass
|
|
||||||
|
def detectCar(self, car: Car) -> bool:
|
||||||
|
return self.mask.overlap(car.mask, (car.x, car.y))
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 61 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 170 KiB After Width: | Height: | Size: 153 KiB |
Reference in new issue
Block a user