diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..763624e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/* \ No newline at end of file diff --git a/__pycache__/__main__.cpython-311.pyc b/__pycache__/__main__.cpython-311.pyc deleted file mode 100644 index 8b0fb48..0000000 Binary files a/__pycache__/__main__.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/car.cpython-311.pyc b/__pycache__/car.cpython-311.pyc deleted file mode 100644 index 94de20b..0000000 Binary files a/__pycache__/car.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/game.cpython-311.pyc b/__pycache__/game.cpython-311.pyc deleted file mode 100644 index d88ddae..0000000 Binary files a/__pycache__/game.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/models.cpython-311.pyc b/__pycache__/models.cpython-311.pyc deleted file mode 100644 index 4f1c7a9..0000000 Binary files a/__pycache__/models.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/util.cpython-311.pyc b/__pycache__/util.cpython-311.pyc deleted file mode 100644 index 7fec2ca..0000000 Binary files a/__pycache__/util.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/utils.cpython-311.pyc b/__pycache__/utils.cpython-311.pyc deleted file mode 100644 index 7d8bbd5..0000000 Binary files a/__pycache__/utils.cpython-311.pyc and /dev/null differ diff --git a/game.py b/game.py index acb98ea..c8c54c0 100644 --- a/game.py +++ b/game.py @@ -1,19 +1,8 @@ #!/usr/bin/env python3.11 import pygame -from models import Car +from models import Car, Background, Track 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: def __init__(self, caption, size): self.caption = caption @@ -25,6 +14,7 @@ class Window: self.screen = pygame.display.set_mode((self.size)) self.clock = pygame.time.Clock() + self.track = Track('track_mask.png', [0, 0], size) self.car = Car(880, 300) def main_loop(self): @@ -60,6 +50,7 @@ class Window: def _process_game_logic(self): self.car.update() + self.track.detectCar(self.car) def _draw(self): self.screen.fill((0, 0, 0)) diff --git a/models.py b/models.py index 3054413..b84bbe6 100644 --- a/models.py +++ b/models.py @@ -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) \ No newline at end of file + 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') diff --git a/resources/track_mask.png b/resources/track_mask.png new file mode 100644 index 0000000..701c21c Binary files /dev/null and b/resources/track_mask.png differ