better turning and mask handling
This commit is contained in:
3 files changed
+16
-9
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
|
from util import load_image, limit
|
||||||
|
|
||||||
class Window:
|
class Window:
|
||||||
def __init__(self, caption, size):
|
def __init__(self, caption, size):
|
||||||
@@ -41,12 +41,12 @@ class Window:
|
|||||||
if pressed[pygame.K_DOWN]: self.car.speed -= 0.2
|
if pressed[pygame.K_DOWN]: self.car.speed -= 0.2
|
||||||
if round(self.car.speed, 1) == 0.1: self.car.speed = 0
|
if round(self.car.speed, 1) == 0.1: self.car.speed = 0
|
||||||
|
|
||||||
c = 5
|
a = 5 / (abs(self.car.speed) + 1)
|
||||||
a = c / (self.car.speed + 1)
|
|
||||||
if self.car.speed == 0: a = 0
|
if self.car.speed == 0: a = 0
|
||||||
|
a = limit(a, 0, 3.5)
|
||||||
|
|
||||||
if pressed[pygame.K_LEFT]: 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]: 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):
|
||||||
self.car.update()
|
self.car.update()
|
||||||
@@ -54,7 +54,9 @@ class Window:
|
|||||||
|
|
||||||
def _draw(self):
|
def _draw(self):
|
||||||
self.screen.fill((0, 0, 0))
|
self.screen.fill((0, 0, 0))
|
||||||
self.background.draw(self.screen)
|
# self.background.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)
|
||||||
@@ -33,6 +33,7 @@ class Car:
|
|||||||
|
|
||||||
self.mask = mask.from_surface(self.surface)
|
self.mask = mask.from_surface(self.surface)
|
||||||
self.mask_image = self.mask.to_surface()
|
self.mask_image = self.mask.to_surface()
|
||||||
|
self.mask_rect = self.mask_image.get_rect(center = self.rect.center)
|
||||||
|
|
||||||
self.angle = 180
|
self.angle = 180
|
||||||
self.speed = 0
|
self.speed = 0
|
||||||
@@ -44,12 +45,14 @@ 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)
|
||||||
|
|
||||||
|
self.mask = mask.from_surface(rotated)
|
||||||
|
self.mask_image = self.mask.to_surface()
|
||||||
|
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):
|
||||||
self.image = load_image(filename).convert_alpha()
|
self.image = load_image(filename).convert_alpha()
|
||||||
@@ -62,4 +65,4 @@ class Track:
|
|||||||
|
|
||||||
def detectCar(self, car: Car):
|
def detectCar(self, car: Car):
|
||||||
if not self.mask.overlap(car.mask, (car.x, car.y)):
|
if not self.mask.overlap(car.mask, (car.x, car.y)):
|
||||||
print('Off Track')
|
pass
|
||||||
@@ -15,3 +15,5 @@ def load_image(filename: str) -> Surface:
|
|||||||
raise Exception(f'Filepath not found: \'{path}\' cannot be found')
|
raise Exception(f'Filepath not found: \'{path}\' cannot be found')
|
||||||
|
|
||||||
return load(path)
|
return load(path)
|
||||||
|
|
||||||
|
def limit(num, minimum=1, maximum=255): return max(min(num, maximum), minimum)
|
||||||
Reference in new issue
Block a user