off track detection

This commit is contained in:
SowinskiBraeden committed 2023-05-24 12:16:24 -07:00
1 parent c7439e4057
commit 9a86377866
10 files changed
+39 -15

No files matched your search

+35 -3
View File
@@ -1,8 +1,19 @@
#!/usr/bin/env python3.11
from pygame import Rect, Surface, SRCALPHA
from pygame import Rect, Surface, SRCALPHA, mask, sprite
from pygame.transform import rotate, scale
from util import load_image
import math, os
import math
class Background(sprite.Sprite):
def __init__(self, filename, location, size):
super().__init__()
self.image = load_image(filename)
self.image = scale(self.image, size)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
def draw(self, window):
window.blit(self.image, self.rect)
class Car:
def __init__(self, x, y):
@@ -15,9 +26,14 @@ class Car:
self.height = img.get_width()
self.x = x - self.width / 2
self.y = y - self.height / 2
self.rect = Rect(x, y, self.height, self.width)
self.surface = Surface((self.height, self.width), SRCALPHA, 32).convert_alpha()
self.surface.blit(img, (0, 0))
self.mask = mask.from_surface(self.surface)
self.mask_image = self.mask.to_surface()
self.angle = 180
self.speed = 0
@@ -28,6 +44,22 @@ class Car:
def draw(self, window):
self.rect.topleft = (int(self.x), int(self.y))
rotated = rotate(self.surface, self.angle)
self.mask = mask.from_surface(rotated)
self.mask_image = self.mask.to_surface()
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
new_rect = rotated.get_rect(center = surface_rect.center)
window.blit(rotated, new_rect.topleft)
window.blit(rotated, new_rect.topleft)
class Track:
def __init__(self, filename, location, size):
self.image = load_image(filename).convert_alpha()
self.image = scale(self.image, size)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.mask = mask.from_surface(self.image)
self.mask_image = self.mask.to_surface()
def detectCar(self, car: Car):
if not self.mask.overlap(car.mask, (car.x, car.y)):
print('Off Track')