def start_race(self, cars, window, countdown=True): world.delete('all') self.podium = [] self.display() start_time = time.time() pos = 0 un_sur_deux = 1 for car in cars: self.put_car_on_track(car) x = self.start.x - np.sin(self.start.angle) * car.length + np.cos(self.start.angle)*(pos - car.width/2) y = self.start.y - np.cos(self.start.angle) * car.length + np.sin(self.start.angle)*(pos - car.width/2) pos = pos + car.width * un_sur_deux un_sur_deux = - un_sur_deux car.teleport(x, y, 360*self.start.angle/(2*np.pi) - 90) car.speed = 0 self.current_checkpoint[car.name] = None self.next_checkpoint(car) self.next_ai_checkpoint(car) self.total_time[car.name] = start_time - 3 car.display() if countdown: for i in range(3, 0, -1): self.display_text({'countdown': str(i)}) world.pack() window.update() time.sleep(1) world.delete('countdown')
def highlight(self): gx = self.x + np.cos(self.angle) * self.width / 2 gy = self.y - np.sin(self.angle) * self.width / 2 dx = self.x - np.cos(self.angle) * self.width / 2 dy = self.y + np.sin(self.angle) * self.width / 2 world.delete(str(self.highlight_time)) self.highlight_time = time.time() world.create_line(gx, gy, dx, dy, width=5, fill='yellow', tag=str(self.highlight_time))
def update(self): output = {} tmp = time.time() for c in self.pieces: if tmp - c.highlight_time > 0.2: world.delete(str(c.highlight_time)) for name in self.cars: car = self.cars[name] output['reward' + car.name] = 0 ai_checkpoint = self.ai_checkpoint[car.name] if ai_checkpoint is not None and ai_checkpoint.reached(car): self.next_ai_checkpoint(car) output['reward' + car.name] = 1 checkpoint = self.current_checkpoint[name] if checkpoint is not None and checkpoint.reached(car): output['reward' + car.name] = 1 self.next_checkpoint(car) checkpoint.highlight() if checkpoint == self.start: self.current_lap[name] += 1 output[name + 'nb_laps'] = "Lap : {}/{}".format(min(self.current_lap[name], self.nb_laps), self.nb_laps) if self.times[car.name][0] != -1: lap_time = "lap time : {0}h {1}'{2}\"{3}\"'".format(*sec_to_hmsc(tmp - self.times[name][0])) output[name + 'lap_time'] = lap_time print(lap_time) if self.best_times[car.name][0] > tmp - self.times[name][0]: self.best_times[car.name][0] = tmp - self.times[name][0] best_lap = "best lap : {0}h {1}'{2}\"{3}\"'".format(*sec_to_hmsc(self.best_times[name][0])) output[name + 'best_lap'] = best_lap print(best_lap) else: print("starting race !") output[name + 'starting_race'] = 'starting race !' if self.current_lap[name] - 1 == self.nb_laps: self.total_time[car.name] = tmp - self.total_time[car.name] self.end_race(car) output[name + 'end_race'] = "End of race !" self.times[name][0] = tmp else: self.times[name][self.pieces.index(checkpoint)] = tmp checkpoint_time = "checkpoint time : {0}h {1}'{2}\"{3}".format(*sec_to_hmsc(tmp - self.times[name][0])) print(checkpoint_time) output[name + 'checkpoint_time'] = checkpoint_time return output
def distances(car, track, nb_angles=8, debugging=False): if debugging and world is not None: world.delete('debug') dists = [] for i in range(nb_angles): theta = -180 + i*360/(nb_angles) x, y = closest_point(car, track, theta) dists.append(distance(car.x, car.y, x, y)) if debugging and world is not None: world.create_line(car.x, car.y, x, y, fill='green', tag='debug') return dists
def erase(self, *args): """erase every part of the car except the ones specified in argsi wfl = wheel front left wbr = wheel bottom right etc""" if not self.leave_trail == 'shadow': if self.name + "_wfl" not in args: world.delete(self.name + "_wfl") if self.name + "_wbl" not in args: world.delete(self.name + "_wbl") if self.name + "_wfr" not in args: world.delete(self.name + "_wfr") if self.name + "_wbr" not in args: world.delete(self.name + "_wbr") if self.name not in args: world.delete(self.name) if self.leave_trail == 'point': Point(self.x, self.y).display()
def display_text(self, text_dic): x = 150 # position of checkpoint text y = 30 if text_dic is None: return for key, string in text_dic.items(): if key == 'countdown': world.delete('countdown') world.create_text(1800/2, 900/2, fill='green', text=string, font='Sans 100', tag='countdown') for name in self.cars: if key == name + 'checkpoint_time': world.delete('car_checkpoint') world.create_text(x, y, fill='green', text=string, font="Times 15", tag='car_checkpoint') if key == name + 'nb_laps': world.delete('lap_nb') world.create_text(1800/2, 900-40, fill='green', text=string, font="Sans 30", tag='lap_nb') if key == name + 'lap_time': world.delete('lap_time') world.create_text(x, y + 30, fill='green', text=string, font="Times 15", tag='lap_time') if key == name + 'best_lap': world.delete('best_lap') world.delete('start') world.create_text(x, y + 50, fill='green', text=string, font="Times 15", tag='best_lap') if key == name + 'starting_race': world.create_text(1800/2, 900/2+20, fill='red', text=string, font="Times 30", tag='start') if key == name + 'end_race': if len(self.podium) == len(self.cars): world.create_text(1800 / 2 - 50, 800 / 2 - 30, fill='green', text=string, font="Sans 15", tag='end_race') for i in range(len(self.podium)): car = self.podium[i] s = "{0} : {1} {2}h{3}'{4}\"{5}".format(i+1, car.name, *sec_to_hmsc(self.total_time[car.name])) world.create_text(1800 / 2 - 50, 800 / 2 + i*25, fill=self.podium[i].color, text=s, font="Sans 15", tag='end_race')