-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_manual.py
248 lines (192 loc) · 8.46 KB
/
game_manual.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
"""
This script allows you to test drive the car yourself, with arrow keys.
Author: Mattias Ulmestrand
"""
from racing_agent import RacingAgent
from collision_handling import get_lidar_lines, line_intersect_distance
import numpy as np
import pygame
from pygame import gfxdraw
import argparse
import math
from pygame_recorder import ScreenRecorder
from game_utils import Car, draw_track
def text(s: str, font: pygame.font.Font) -> pygame.Surface:
return font.render(s, True, (0, 0, 0))
def parse_args():
parser = argparse.ArgumentParser(
description="Drive the car yourself."
)
parser.add_argument(
"--track-name",
required=False,
type=str,
default="racetrack15",
help="Name of the track."
)
parser.add_argument(
"--save-name",
default="default_name",
type=str
)
return parser.parse_args()
def main(track_name: str, save_name: str):
def get_wheel_lines():
total_angle = agent.angle + agent.turning_angle
# scale decides how far we are willing to look for an intersection
scale = box_size * 100
top_vector = scale * (np.array([-math.sin(total_angle), math.cos(total_angle)]))
bottom_line = car_bounds[0:2]
if agent.turning_angle < 0:
bottom_line = bottom_line[::-1]
top_vector *= -1
top_line = car_bounds[6:8]
top_line[0] = top_line.mean(axis=0)
top_line[1] = top_line[0] + top_vector
bottom_vector = scale * (bottom_line[0] - bottom_line[1])
bottom_line[1] = bottom_line[0] + bottom_vector
top_distance = line_intersect_distance(top_line[0], top_line[1], bottom_line[0], bottom_line[1])
bottom_distance = line_intersect_distance(bottom_line[0], bottom_line[1], top_line[0], top_line[1])
top_line[1] = top_line[0] + top_distance * top_vector
bottom_line[1] = bottom_line[0] + bottom_distance * bottom_vector
return bottom_distance, top_distance, bottom_line, top_line, bottom_vector
def plot_turning_circle():
bottom_distance, top_distance, bottom_line, top_line, bottom_vector = get_wheel_lines()
if top_distance != 0:
pygame.draw.aalines(screen, "blue", False, bottom_line * screen_scale)
pygame.draw.aalines(screen, "blue", False, top_line * screen_scale)
focal_point = (top_line[1] * screen_scale).astype(int)
turning_radius = int(np.linalg.norm(screen_scale * bottom_distance * bottom_vector))
gfxdraw.filled_circle(screen, *focal_point, 4, (0, 0, 255))
gfxdraw.aacircle(screen, *focal_point, 4, (0, 0, 255))
# gfxdraw.aacircle(screen, *focal_point, turning_radius, (0, 0, 255, 120))
def plot_arc():
bottom_distance, top_distance, bottom_line, top_line, bottom_vector = get_wheel_lines()
if top_distance != 0:
turning_sign = np.sign(agent.turning_angle)
angle_deg = 1
angle = np.radians(angle_deg) * turning_sign
c, s = np.cos(angle), np.sin(angle)
rotation_matrix = np.array([[c, -s], [s, c]])
n_segments = 145 // angle_deg
bottom_line *= screen_scale
x = bottom_line[0] - bottom_line[1]
x = x[:, None]
for i in range(n_segments):
x_new = rotation_matrix @ x
pygame.draw.aaline(screen, (0, 0, 255), bottom_line[1] + x[:, 0], bottom_line[1] + x_new[:, 0])
x = x_new
box_size = 100
screen_scale = 8
turning_speed = 0.02
drift = 0.
acc = 0.01
agent = RacingAgent(
box_size=box_size,
buffer_size=1,
device="cpu",
)
agent.store_track(track_name)
agent.turning_speed=turning_speed
agent.drift=drift
agent.acc=acc
agent.speed_lower *= 0.1
agent.velocity *= 0.01
agent.dec = 0.97
pygame.init()
screen_x1 = box_size * screen_scale
screen_x2 = int(1.3 * screen_x1)
screen_y = box_size * screen_scale
slider_width = (screen_x2 - screen_x1) * 0.9
screen = pygame.display.set_mode((screen_x2, screen_y))
recorder = ScreenRecorder(screen_x2, screen_y, 60, "./pygame_recordings/" + save_name + ".avi")
inner_line = np.load(f"tracks/{track_name}_inner_bound.npy")
outer_line = np.load(f"tracks/{track_name}_outer_bound.npy")
car = Car(agent.position[0], agent.position[1], downscale=(100 // screen_scale), car_path="sprites/car_grey.png")
sprites = pygame.sprite.Group(car)
clock = pygame.time.Clock()
previous_key = None
running = True
show_turning_circle = False
show_turning_arc = False
screen.fill("white")
draw_track(inner_line, outer_line, screen, screen_scale)
pygame.image.save(screen, "background/track_background.jpg")
track_background = pygame.image.load("background/track_background.jpg")
track_rect = track_background.get_rect()
height = 30
font = pygame.font.Font(r"C:\Windows\Fonts\timesi.ttf", height)
texts = [text(s, font) for s in ['v', 'θ', "d₋₂", "d₋₁", "d₀", "d₁", "d₂"]]
lidar_order = [0, 1, 2, 3, 8, 9, 4, 5, 6, 7]
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
agent.accelerate()
if keys[pygame.K_DOWN]:
agent.decelerate()
if keys[pygame.K_LEFT]:
agent.turn_right()
if keys[pygame.K_RIGHT]:
agent.turn_left()
if keys[pygame.K_t]:
if previous_key != pygame.K_t:
show_turning_circle = not show_turning_circle
previous_key = pygame.K_t
elif keys[pygame.K_a]:
if previous_key != pygame.K_a:
show_turning_arc = not show_turning_arc
previous_key = pygame.K_a
else:
previous_key = None
agent.limit_speeds()
agent.move()
xs, ys, car_bounds, car_collides = get_lidar_lines(agent)
agent.current_step += 1
lidar_lines = np.vstack((xs, ys)).T
car_center = (car_bounds[0, :] + car_bounds[-1, :]) / 2
scaled_center = screen_scale * car_center
screen.fill("white")
screen.blit(track_background, track_rect)
scaled_lidar_lines = screen_scale * lidar_lines
pygame.draw.aalines(screen, "black", False, car_bounds * screen_scale)
pygame.draw.aalines(screen, "red", False, scaled_lidar_lines)
for i in range(1, scaled_lidar_lines.shape[0], 2):
circle_xy = scaled_lidar_lines[i].astype(int)
gfxdraw.filled_circle(screen, *circle_xy, 2, (255, 0, 0))
gfxdraw.aacircle(screen, *circle_xy, 2, (255, 0, 0))
if show_turning_circle:
plot_turning_circle()
if show_turning_arc:
plot_arc()
rect_x1 = screen_x1
rect_width = screen_x2 - screen_x1
rect_y1 = 10
angle_width = rect_width * (agent.turning_angle / agent.max_angle + 1) / 2
speed_width = rect_width * np.linalg.norm(agent.velocity) / agent.max_speed
color = (0, 200, 100)
border_radius = 10
pygame.draw.rect(screen, color, pygame.Rect(rect_x1, rect_y1, speed_width, height), border_radius=border_radius)
pygame.draw.rect(screen, color, pygame.Rect(rect_x1, rect_y1 + height, angle_width, height), border_radius=border_radius)
ordered_lidar_lines = lidar_lines[lidar_order]
for count, i in enumerate(range(1, lidar_lines.shape[0], 2), 2):
line_width = np.linalg.norm((ordered_lidar_lines[i] - ordered_lidar_lines[i - 1]) / box_size * rect_width)
pygame.draw.rect(screen, color, pygame.Rect(rect_x1, rect_y1 + count * height, line_width, height), border_radius=border_radius)
for i, txt in enumerate(texts):
screen.blit(txt, (rect_x1, rect_y1 + height * i))
sprites.update(scaled_center, agent.angle)
sprites.draw(screen)
# pygame.draw.line(screen, "green", scaled_center, scaled_center + agent.velocity * 100)
# pygame.draw.line(screen, "red", scaled_center, scaled_center + agent.drift_velocity * 100)
pygame.display.update()
recorder.capture_frame(screen)
clock.tick(60)
recorder.end_recording()
# pygame.quit()
if __name__ == "__main__":
args = parse_args()
main(args.track_name, args.save_name)