forked from x2ever/Autonomous-Car-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrosswalk.py
86 lines (74 loc) · 2.83 KB
/
Crosswalk.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
import sys
import pygame
import numpy as np
from V2X import V2X
class Crosswalk(V2X, pygame.sprite.Sprite):
hit = pygame.image.load('images/collision.png')
def __init__(self, position, width, height, interval=20, phase=0):
V2X.__init__(self, position, name="Crosswalk")
pygame.sprite.Sprite.__init__(self)
red_wall = [255, 0, 0] * np.ones((width, height, 3))
self.normal = pygame.surfarray.make_surface(red_wall)
self.rect = pygame.Rect(self.normal.get_rect())
self.rect.center = position
self.width = width
self.height = height
self.position = position
self.time_left = interval + phase
self.interval = interval
self.color = "red"
self.data =\
[self.name, self.color, self.position,
self.width, self.height, self.time_left, self.interval]
def update(self):
self.time_left -= 1
if self.time_left <= 0:
self.time_left = self.interval
if self.color == "red":
self.color = "green"
green_wall = [0, 255, 0] *\
np.ones((self.width, self.height, 3))
self.normal = pygame.surfarray.make_surface(green_wall)
self.rect = pygame.Rect(self.normal.get_rect())
self.rect.center = self.position
else:
self.color = "red"
red_wall = [255, 0, 0] * np.ones((self.width, self.height, 3))
self.normal = pygame.surfarray.make_surface(red_wall)
self.rect = pygame.Rect(self.normal.get_rect())
self.rect.center = self.position
self.image = self.normal
self.data = \
[self.name, self.color, self.position,
self.width, self.height, self.time_left, self.interval]
if __name__ == "__main__":
pygame.init()
pygame.display.set_caption("Crosswalk Example")
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
c1 = Crosswalk((300, 200), 50, 10)
c2 = Crosswalk((100, 400), 10, 50)
crosswalks = pygame.sprite.Group()
crosswalks.add(c1)
crosswalks.add(c2)
while True:
clock.tick(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill((0, 0, 0))
crosswalks.update([c1])
print(c1.data, c2.data)
crosswalks.draw(screen)
pygame.draw.rect(screen, (50, 50, 255), [200, 200, 100, 200], 0)
font = pygame.font.Font('freesansbold.ttf', 50)
text = font.render("P", 1, (255, 255, 255))
textpos = text.get_rect()
screen.blit(
text,
[250 - textpos[2] / 2,
300 - textpos[3] / 2,
textpos[2],
textpos[3]]
)
pygame.display.flip()