-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.py
42 lines (35 loc) · 1.47 KB
/
car.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import pygame
from os import walk
from random import randint
from custom_enum import SpriteType
class Car(pygame.sprite.Sprite):
def __init__(self, groups, position):
super().__init__(groups)
self.type = SpriteType.CAR
self.image = self._random_car()
self.rect = self.image.get_rect(center=position)
self.position = pygame.math.Vector2(self.rect.center)
self._car_direction()
self.speed = 300
self.hitbox = self.rect.inflate(0, -self.rect.height/2)
def update(self, delta_time):
self._move(delta_time=delta_time)
self._clear()
def _random_car(self):
for path, _, file_names in walk("graphics/cars"):
random_file_name = file_names[randint(0, 2)]
return pygame.image.load(f"{path}/{random_file_name}").convert_alpha()
def _car_direction(self):
position_x = self.position[0]
if position_x < 0:
self.direction = pygame.math.Vector2((1, 0))
else:
self.direction = pygame.math.Vector2((-1, 0))
self.image = pygame.transform.flip(surface=self.image, flip_x=True, flip_y=False)
def _move(self, delta_time):
self.position += self.direction * self.speed * delta_time
self.hitbox.center = (round(self.position.x), round(self.position.y)) # ne pas oublier le round()
self.rect.center = self.hitbox.center
def _clear(self):
if not -200 < self.rect.x < 3400:
self.kill()