forked from tdostilio/Race_Game
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGame.py
350 lines (320 loc) · 13.2 KB
/
Game.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import copy
import math
import time
import numpy as np
import pygame
from pygame.locals import (K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT, K_SPACE, K_UP,
KEYDOWN, USEREVENT)
from Car import CarSprite
from Trophy import TrophySprite
from Wall import WallSprite
class Game:
def __init__(self, walls, trophies, parkings,
crosswalks, traffic_signs, car, database):
self.init_args =\
[
copy.copy(walls),
copy.copy(trophies),
copy.copy(parkings),
copy.copy(crosswalks),
copy.copy(car),
database
]
pygame.init()
self.car = car
self.screen = pygame.display.set_mode((1000, 800))
self.traffic_signs = traffic_signs
self.clock = pygame.time.Clock()
font = pygame.font.Font(None, 75)
self.win_font = pygame.font.Font(None, 50)
self.win_condition = None
self.win_text = font.render('', True, (0, 255, 0))
self.loss_text = font.render('', True, (255, 0, 0))
self.wall_group = pygame.sprite.RenderPlain(*walls)
self.trophy_group = pygame.sprite.RenderPlain(*trophies)
self.crosswalk_group = pygame.sprite.RenderPlain(*crosswalks)
self.car_group = pygame.sprite.RenderPlain(car)
self.parkings = parkings
self.rect = self.screen.get_rect()
self.stop = False
self.car_update = True
self.database = database
def run(self, auto=False):
seconds = 0
record = False
temp_v2x_data = []
while True:
deltat = self.clock.tick(30)
seconds += 0.03
seconds = round(seconds, 2)
if self.win_condition is not None:
if not record:
record = True
result = seconds
events = pygame.event.get()
if auto:
self.car.k_right = self.car.k_left =\
self.car.k_up = self.car.k_down = 0
for event in events:
if auto:
if not hasattr(event, 'key'):
continue
if event.type != USEREVENT and (
event.key == K_RIGHT or
event.key == K_LEFT or
event.key == K_UP or
event.key == K_DOWN
):
continue
if self.win_condition is None:
if event.key == K_RIGHT:
if self.car.k_right > -8:
self.car.k_right += -1
elif event.key == K_LEFT:
if self.car.k_left < 8:
self.car.k_left += 1
elif event.key == K_UP:
if self.car.k_up < 5:
self.car.k_up += 1
elif event.key == K_DOWN:
if self.car.k_down > -5:
self.car.k_down += -1
elif event.key == K_ESCAPE:
self.database.stop = True
elif self.win_condition is True and event.key == K_SPACE:
print(result)
self.database.stop = True
time.sleep(0.1)
elif self.win_condition is False and event.key == K_SPACE:
print(result)
time.sleep(0.1)
self.database.stop = True
elif event.key == K_ESCAPE:
self.database.stop = True
print(result)
time.sleep(0.1)
else:
if not hasattr(event, 'key'):
continue
down = event.type == KEYDOWN
if self.win_condition is None:
if event.key == K_RIGHT:
self.car.k_right = down * -5
elif event.key == K_LEFT:
self.car.k_left = down * 5
elif event.key == K_UP:
self.car.k_up = down * 2
elif event.key == K_DOWN:
self.car.k_down = down * -2
elif event.key == K_ESCAPE:
self.database.stop = True
elif self.win_condition is True and event.key == K_SPACE:
print(result)
time.sleep(0.1)
self.database.stop = True
elif self.win_condition is False and event.key == K_SPACE:
print(result)
time.sleep(0.1)
self.database.stop = True
elif event.key == K_ESCAPE:
print(result)
time.sleep(0.1)
self.database.stop = True
if self.database.stop:
break
# RENDERING
self.screen.fill((0, 0, 0))
if self.car_update:
self.car_group.update(deltat)
collisions = pygame.sprite.groupcollide(
self.car_group, self.wall_group, False, False, collided=None)
if collisions != {}:
self.car_update = False
self.win_condition = False
self.car.image = pygame.image.load('images/collision.png')
self.car.MAX_FORWARD_SPEED = 0
self.car.MAX_REVERSE_SPEED = 0
self.car.k_right = 0
self.car.k_left = 0
crosswalk_collisions = pygame.sprite.groupcollide(
self.car_group,
self.crosswalk_group,
False,
False,
collided=None
)
trophy_collision = pygame.sprite.groupcollide(
self.car_group,
self.trophy_group,
False,
True
)
for colled_crosswalk in crosswalk_collisions.values():
if colled_crosswalk[0].color == "red":
self.car_update = False
self.win_condition = False
self.car.image = pygame.image.load('images/collision.png')
self.car.MAX_FORWARD_SPEED = 0
self.car.MAX_REVERSE_SPEED = 0
self.car.k_right = 0
self.car.k_left = 0
if trophy_collision != {}:
all_parking_done = True
for parking in self.parkings:
if not parking.mission_complete:
all_parking_done = False
break
if all_parking_done:
self.car_update = False
self.win_condition = True
else:
self.car_update = False
self.win_condition = False
self.car.MAX_FORWARD_SPEED = 0
self.car.MAX_REVERSE_SPEED = 0
if self.win_condition is True:
self.car.k_right = -5
temp_v2x_data.clear()
for parking in self.parkings:
parking.update(self.car)
parking.draw(self.screen)
if parking.is_in_range(self.car):
temp_v2x_data.append((id(parking), parking.data))
self.wall_group.update()
self.crosswalk_group.update()
self.crosswalk_group.draw(self.screen)
for crosswalk in self.crosswalk_group:
if crosswalk.is_in_range(self.car):
temp_v2x_data.append((id(crosswalk), crosswalk.data))
new_dict = dict()
for traffic_sign in self.traffic_signs:
traffic_sign.draw(self.screen)
temp_v2x_data.append((id(traffic_sign), traffic_sign.data))
for v2x_data in temp_v2x_data:
key, value = v2x_data
new_dict[key] = value
self.database.v2x_data = new_dict
# print(self.database.v2x_data)
self.wall_group.draw(self.screen)
self.car_group.draw(self.screen)
self.trophy_group.draw(self.screen)
# Counter Render
pygame.display.flip()
self.make_lidar_data()
def again(self, auto):
self.__init__(*self.init_args)
self.run(auto=auto)
def make_lidar_data(self):
lidar_data = np.zeros((360))
L = 100
array = pygame.surfarray.array3d(self.screen)
car = self.database.car
x, y = car.position
car_direction = car.direction % 360
lidar_x = int(x - 20 * math.sin(math.pi * car_direction / 180))
lidar_y = int(y - 20 * math.cos(math.pi * car_direction / 180))
for direction in range(-90 + car_direction, 90 + car_direction):
direction = direction % 360
x, y = lidar_x, lidar_y
m = math.tan(math.pi * direction / 180)
if direction == 0:
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
x = x
y -= 1
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif (0 < direction < 45) or (315 <= direction < 360):
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
y -= 1
x = (m) * (y - lidar_y) + lidar_x
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif (45 <= direction < 90) or (90 < direction < 135):
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
x -= 1
y = (1 / m) * (x - lidar_x) + lidar_y
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif direction == 90:
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
x -= 1
y = y
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif (135 <= direction < 180) or (180 < direction < 225):
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
y += 1
x = (m) * (y - lidar_y) + lidar_x
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif direction == 180:
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
x = x
y += 1
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif (225 <= direction < 270) or (270 < direction < 315):
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
x += 1
y = (1 / m) * (x - lidar_x) + lidar_y
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
elif direction == 270:
while (math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2) < L):
x += 1
y = y
try:
if (array[int(x)][int(y)] == 255).all():
break
except IndexError:
break
else:
print(f"Uncatched Case: {direction}")
length = math.sqrt((x - lidar_x) ** 2 + (y - lidar_y) ** 2)
if length > L:
length = L
lidar_data[direction] = length
lidar_data = np.concatenate(
(lidar_data[-90:], lidar_data[:270]), axis=None
)
lidar_data = np.concatenate(
(lidar_data, lidar_data), axis=None
)
lidar_data =\
lidar_data[self.car.direction % 360:
self.car.direction % 360 + 180]
self.database.lidar.data = lidar_data
if __name__ == "__main__":
walls = [
WallSprite((512, 2.5), 1024, 5),
WallSprite((512, 765.5), 1024, 5),
WallSprite((2.5, 384), 5, 768),
WallSprite((1021.5, 384), 5, 768)
]
trophies = [
TrophySprite((300, 50))
]
car = CarSprite('images/car.png', (50, 700))
g = Game(walls, trophies, car)
g.run()