off track detection
This commit is contained in:
10 files changed
+38
-14
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
__pycache__/*
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,19 +1,8 @@
|
|||||||
#!/usr/bin/env python3.11
|
#!/usr/bin/env python3.11
|
||||||
import pygame
|
import pygame
|
||||||
from models import Car
|
from models import Car, Background, Track
|
||||||
from util import load_image
|
from util import load_image
|
||||||
|
|
||||||
class Background(pygame.sprite.Sprite):
|
|
||||||
def __init__(self, filename, location, size):
|
|
||||||
super().__init__()
|
|
||||||
self.image = load_image(filename)
|
|
||||||
self.image = pygame.transform.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 Window:
|
class Window:
|
||||||
def __init__(self, caption, size):
|
def __init__(self, caption, size):
|
||||||
self.caption = caption
|
self.caption = caption
|
||||||
@@ -25,6 +14,7 @@ class Window:
|
|||||||
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()
|
||||||
|
|
||||||
|
self.track = Track('track_mask.png', [0, 0], size)
|
||||||
self.car = Car(880, 300)
|
self.car = Car(880, 300)
|
||||||
|
|
||||||
def main_loop(self):
|
def main_loop(self):
|
||||||
@@ -60,6 +50,7 @@ class Window:
|
|||||||
|
|
||||||
def _process_game_logic(self):
|
def _process_game_logic(self):
|
||||||
self.car.update()
|
self.car.update()
|
||||||
|
self.track.detectCar(self.car)
|
||||||
|
|
||||||
def _draw(self):
|
def _draw(self):
|
||||||
self.screen.fill((0, 0, 0))
|
self.screen.fill((0, 0, 0))
|
||||||
|
|||||||
@@ -1,8 +1,19 @@
|
|||||||
#!/usr/bin/env python3.11
|
#!/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 pygame.transform import rotate, scale
|
||||||
from util import load_image
|
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:
|
class Car:
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
@@ -15,9 +26,14 @@ class Car:
|
|||||||
self.height = img.get_width()
|
self.height = img.get_width()
|
||||||
self.x = x - self.width / 2
|
self.x = x - self.width / 2
|
||||||
self.y = y - self.height / 2
|
self.y = y - self.height / 2
|
||||||
|
|
||||||
self.rect = Rect(x, y, self.height, self.width)
|
self.rect = Rect(x, y, self.height, self.width)
|
||||||
self.surface = Surface((self.height, self.width), SRCALPHA, 32).convert_alpha()
|
self.surface = Surface((self.height, self.width), SRCALPHA, 32).convert_alpha()
|
||||||
self.surface.blit(img, (0, 0))
|
self.surface.blit(img, (0, 0))
|
||||||
|
|
||||||
|
self.mask = mask.from_surface(self.surface)
|
||||||
|
self.mask_image = self.mask.to_surface()
|
||||||
|
|
||||||
self.angle = 180
|
self.angle = 180
|
||||||
self.speed = 0
|
self.speed = 0
|
||||||
|
|
||||||
@@ -28,6 +44,22 @@ class Car:
|
|||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
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)
|
||||||
|
self.mask = mask.from_surface(rotated)
|
||||||
|
self.mask_image = self.mask.to_surface()
|
||||||
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
|
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
|
||||||
new_rect = rotated.get_rect(center = surface_rect.center)
|
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')
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
Reference in new issue
Block a user