-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
78 lines (56 loc) · 2.17 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
76
77
78
from util import absolute_path_for
import pygame
import time
class Car:
image_path = absolute_path_for('/assets/sprites/cars/animated/police/')
acceleration = 0.2
car_image = pygame.image.load(image_path + "idle.png")
car_animation = [
pygame.image.load(image_path + '1.png'),
pygame.image.load(image_path + '2.png'),
pygame.image.load(image_path + '3.png')
]
def __init__(self, surface):
self.surface_width = surface.get_width()
self.surface_height = surface.get_height()
self.is_light_on = False
self.speed = 0
self.max_speed = 200
self.odometer = 0
self.speed_time_delta = time.time()
self.rect = pygame.Rect((self.surface_width / 2.0) - (self.car_image.get_width() / 2.0),
500,
self.car_image.get_width(),
self.car_image.get_height())
self.repeat = 0
def turn_lights_on(self, screen):
screen.blit(self.car_animation[0], (self.rect.left, self.rect.top))
self.repeat += 1
if self.repeat % 5 == 0:
self.car_animation = self.car_animation[1:] + self.car_animation[:1]
def turn_lights_off(self, screen):
screen.blit(self.car_image, (self.rect.left, self.rect.top))
def toggle_lights(self):
self.is_light_on = not self.is_light_on
def animate(self, screen):
if self.is_light_on:
self.turn_lights_off(screen)
else:
self.turn_lights_on(screen)
if self.speed > 0:
self.update_odometer()
def turn_left(self):
self.rect.left -= abs(self.speed / 1)
def turn_right(self):
self.rect.left += abs(self.speed / 1)
def increase_speed(self):
if self.speed < self.max_speed:
self.speed += self.acceleration
def reduce_speed(self):
self.speed -= self.acceleration
def update_odometer(self):
actual_time = time.time()
# in hours
delta_time = (time.time() - self.speed_time_delta) / 3600
self.speed_time_delta = actual_time
self.odometer += (self.speed * delta_time) * 1000