forked from x2ever/Autonomous-Car-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParking.py
111 lines (94 loc) · 3.09 KB
/
Parking.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from V2X import V2X
from Car import CarSprite
import pygame
import sys
class Parking(V2X):
def __init__(self, position, width, height, stay_time=30):
V2X.__init__(self, position, name="Parking")
self.mission_complete = False
self.stay_time = stay_time
self.time_left = stay_time
self.position = position
self.width = width
self.height = height
self.data = [
self.name, self.position,
self.width, self.height,
self.mission_complete, self.time_left
]
def update(self, car: CarSprite):
left, top, width, height = car.rect
p0 = (left, top)
p1 = (left + width, top)
p2 = (left, top + height)
p3 = (left + width, top + height)
points = [p0, p1, p2, p3]
in_mission = True
for point in points:
if not self.is_in_parking_lot(point):
in_mission = False
break
if in_mission:
self.time_left -= 1
if self.time_left < 0:
self.mission_complete = True
self.stay_time = -1
else:
self.time_left = self.stay_time
self.data = [
self.name, self.position,
self.width, self.height,
self.mission_complete, self.time_left
]
def draw(self, screen):
x, y = self.position
if not self.mission_complete:
pygame.draw.rect(
screen,
(50, 50, 255),
[x, y, self.width, self.height],
0
)
font_size = int((self.width + self.height) / 6)
font = pygame.font.Font('freesansbold.ttf', font_size)
text = font.render("P", 1, (250, 250, 250))
textpos = text.get_rect()
screen.blit(
text,
[x + self.width / 2 - textpos[2] / 2,
y + self.height / 2 - textpos[3] / 2,
textpos[2],
textpos[3]]
)
else:
pygame.draw.rect(
screen,
(50, 50, 50),
[x, y, self.width, self.height],
0
)
def is_in_parking_lot(self, point: tuple):
x, y = point
if (self.position[0] < x <
self.position[0] + self.width) and\
(self.position[1] < y <
self.position[1] + self.height):
return True
else:
return False
if __name__ == "__main__":
pygame.init()
pygame.display.set_caption("Crosswalk Example")
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
c = CarSprite('images/car.png', (320, 240))
p = Parking((320, 240), 100, 60)
while True:
clock.tick(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill((0, 0, 0))
p.update(c)
p.draw(screen)
pygame.display.flip()