-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
75 lines (60 loc) · 2.59 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pygame
import math
from map import Coordinate
car_image = pygame.transform.scale(pygame.image.load('car.png'), (33, 22))
class Car(pygame.sprite.Sprite):
MAX_VELOCITY_FORWARD = 200
MAX_VELOCITY_BACKWARD = 20
ACCELERATION_FACTOR_FORWARD = 100
ACCELERATION_FACTOR_BACKWARD = 100
BRAKING_FACTOR = 100
IDLE_DECAY_FACTOR = 5
STEER_DECAY_FACTOR = 5
def __init__(self, position: Coordinate, angle: float = 0):
super().__init__()
self.base_image = car_image
self.position = list(position)
self.velocity: float = 0
self.angle = angle # radians
def update(self, dt: float):
# speed decay
self.brake(dt * Car.IDLE_DECAY_FACTOR)
self.position[0] += self.velocity * math.sin(self.angle) * dt
self.position[1] += self.velocity * math.cos(self.angle) * dt
self.image = pygame.transform.rotate(self.base_image, math.degrees(self.angle) - 90)
self.rect = self.image.get_rect(center=self.position)
def accelerate(self, value: float):
self.velocity += value
def brake(self, value: float):
s = math.copysign(1.0, self.velocity)
self.velocity = s * max(0, abs(self.velocity) - value)
def rotate(self, angle_delta: float):
self.angle += angle_delta
class CarController():
def __init__(self, car: Car):
self.car = car
self.brake: float = 0
self.gas: float = 0
self.steer: float = 0
def update(self, dt: float, *args, **kwargs):
self.brake = max(0, min(1, self.brake))
self.gas = max(-1, min(1, self.gas))
self.steer = max(-1, min(1, self.steer))
# TODO: smooth gas
if self.gas > 0:
if self.car.velocity < 0:
self.car.brake(dt * Car.BRAKING_FACTOR)
else:
self.car.accelerate(dt * Car.ACCELERATION_FACTOR_FORWARD * (1.1 - self.car.velocity / Car.MAX_VELOCITY_FORWARD))
self.car.velocity = min(self.car.velocity, Car.MAX_VELOCITY_FORWARD)
elif self.gas < 0:
if self.car.velocity > 0:
self.car.brake(dt * Car.BRAKING_FACTOR)
else:
self.car.accelerate(-dt * Car.ACCELERATION_FACTOR_BACKWARD * (1.1 - self.car.velocity / Car.MAX_VELOCITY_BACKWARD))
self.car.velocity = max(-Car.MAX_VELOCITY_BACKWARD, self.car.velocity)
if self.brake > 0:
self.car.brake(dt * Car.BRAKING_FACTOR * self.brake)
if self.steer != 0:
self.car.rotate(math.radians(dt * 100) * self.steer)
self.car.brake(dt * Car.STEER_DECAY_FACTOR)