Skip to content

Commit

Permalink
Put the finish line on index -1
Browse files Browse the repository at this point in the history
This makes it easier to track when a bot has finished a lap.
  • Loading branch information
Rayman committed Dec 2, 2024
1 parent 14a78de commit 35b2da9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions racer/car_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def update(self, dt: float, throttle: float, steering_command: float):
class CarInfo:
def __init__(self, car_type: Car, track: Track):
# angle of the first section
starting_vector = track.lines[1] - track.lines[0]
starting_vector = track.lines[0] - track.lines[-1]
starting_vector.normalize_ip()
starting_rotation = Rotation(starting_vector[0], -starting_vector[1], starting_vector[1], starting_vector[0])

self.car_type = car_type
self.track = track
self.car_physics = CarPhysics(Transform(starting_rotation, deepcopy(track.lines[0])),
self.car_physics = CarPhysics(Transform(starting_rotation, deepcopy(track.lines[-1])),
Vector2())
self.round = 0
self.next_waypoint = 0
Expand Down
9 changes: 6 additions & 3 deletions racer/game_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def get_bot_commands(self, bot, car_info):
def ranked(self) -> List[Bot]:
# Search for the car_info with the longest waypoint_timing
bots = list(self.bots.keys())
bots.sort(
key=lambda bot: (self.bots[bot].round, self.bots[bot].next_waypoint, -self.bots[bot].waypoint_timing[-1]),
reverse=True)

def bot_distance(bot):
info = self.bots[bot]
return info.round, info.next_waypoint, -info.waypoint_timing[-1] if info.waypoint_timing else 0

bots.sort(key=bot_distance, reverse=True)
return bots
2 changes: 1 addition & 1 deletion racer/tracks/track1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
scale = 2
track_width = 15
lines = [
(694.59796, 220.4221),
(733.31953, 226.15862),
(759.13391, 248.62669),
(762.00217, 278.26542),
Expand Down Expand Up @@ -55,4 +54,5 @@
(325.0699300000001, 197.95403),
(358.05497, 214.68557),
(414.46417, 222.33427),
(694.59796, 220.4221),
]
2 changes: 1 addition & 1 deletion racer/tracks/track2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
scale = 1
track_width = 30
lines = [
(1226.2844, 739.13218),
(985.75603, 907.33965),
(964.77402, 953.11646),
(985.03829, 996.35491),
Expand Down Expand Up @@ -58,4 +57,5 @@
(1714.8148, 316.77504),
(1706.158, 403.61958),
(1658.7513, 449.78183),
(1226.2844, 739.13218),
]
9 changes: 6 additions & 3 deletions racer/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ def draw(self, clock):
fastest_bot = self.game_state.ranked()[0]
for i, bot in enumerate(self.game_state.ranked()):
car_info = self.game_state.bots[bot]
fastest_bot_time = self.game_state.bots[fastest_bot].waypoint_timing[len(car_info.waypoint_timing) - 1]
own_time = car_info.waypoint_timing[len(car_info.waypoint_timing) - 1]
behind = own_time - fastest_bot_time
if car_info.waypoint_timing:
fastest_bot_time = self.game_state.bots[fastest_bot].waypoint_timing[len(car_info.waypoint_timing) - 1]
own_time = car_info.waypoint_timing[len(car_info.waypoint_timing) - 1]
behind = own_time - fastest_bot_time
else:
behind = 0

text = self.font.render(f'{bot.name} ({bot.contributor})', True, bot.color)
self.window.blit(text, (self.window.get_width() - 300, offset_from_top + i * 20))
Expand Down

0 comments on commit 35b2da9

Please sign in to comment.