Skip to content

Commit

Permalink
table standings
Browse files Browse the repository at this point in the history
  • Loading branch information
Fer14 committed Sep 10, 2024
1 parent afdf4ba commit 72cab41
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
8 changes: 8 additions & 0 deletions car.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math
import numpy as np
from collections import deque
import time

WIDTH = 1920
HEIGHT = 1080
Expand Down Expand Up @@ -47,6 +48,13 @@ def __init__(self, position, angle=0, len_positions=20):

drift = pygame.image.load("./images/drift.png").convert_alpha()
self.drift = pygame.transform.scale(drift, (20, 40))
self.lap_times = []

def __str__(self):
return f"{self.name}"

def init_time(self, time):
self.init_time_ = time

def draw(self, screen, draw_radar=False):
if self.n_drifts_left >= 3 or self.n_drifts_right >= 3:
Expand Down
1 change: 1 addition & 0 deletions neat_/neat_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, net=None, position=None):
self.sprite = pygame.image.load("./neat_/car.png").convert_alpha()
self.sprite = pygame.transform.scale(self.sprite, (CAR_SIZE_X, CAR_SIZE_Y))
self.rotated_sprite = self.sprite
self.name = "NEAT"

def load_net(self):
local_dir = os.path.dirname(__file__)
Expand Down
1 change: 1 addition & 0 deletions policy_gradient/pg_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(

self.onpolicy_reset()
self.crashed = False
self.name = "PG"

def create_model(self, input_size, hidden_size, output_size):
model = nn.Sequential(
Expand Down
1 change: 1 addition & 0 deletions qlearning/q_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(

self.crashed = False
self.last_position = self.position
self.name = "DQN"

def create_model(self, input_size, hidden_size, output_size, trainable=True):
model = nn.Sequential(
Expand Down
104 changes: 100 additions & 4 deletions race.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import os
from car import Car
import time

WIDTH = 1920
HEIGHT = 1080
Expand All @@ -12,7 +13,7 @@

class Race:

def __init__(self, start: list[int, int], finnish_line) -> None:
def __init__(self, start: list[int, int], finnish_line, laps: int = 2) -> None:
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) # , pygame.FULLSCREEN)
pygame.display.set_caption("RAICE")
Expand All @@ -37,6 +38,9 @@ def __init__(self, start: list[int, int], finnish_line) -> None:
self.crash = pygame.image.load("./images/smoke.png").convert_alpha()
self.crash = pygame.transform.scale(self.crash, (50, 50))

self.laps = laps
self.finished_cars = []

def load_random_map(self):
map_dir = "../maps" # Directory containing map images
map_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
Expand All @@ -57,12 +61,17 @@ def draw(self, cars: list[Car], draw_radar=False):
self.screen.blit(self.crash, car.crashed_position)

# pygame.draw.rect(self.screen, (255, 0, 0), self.finnish_line, 2)

self.draw_standings_table(cars + self.finished_cars)
pygame.display.flip()

def race(self, cars: list[Car]):
clock = pygame.time.Clock()

time_init = time.time()

for car in cars:
car.init_time(time_init)

running = True
while running:
clock.tick(120) # Cap the frame rate
Expand All @@ -74,11 +83,16 @@ def race(self, cars: list[Car]):
raise KeyboardInterrupt("Race interrupted")

# For Each Car Get The Acton It Takes
for car in cars:
for car in cars[:]:
car.action()
car.update(self.game_map, training=False)
self.check_lap(car)

if car.laps == self.laps:
print(f"Car has completed all laps: {car.laps}")
self.finished_cars.append(car)
cars.remove(car)

self.draw(cars)

print("Race finished")
Expand All @@ -100,10 +114,92 @@ def check_lap(self, car: Car):
if not hasattr(car, "on_finish_line") or not car.on_finish_line:
if correct_direction:
car.laps += 1
print(f"Lap completed! Total laps: {car.laps}")
car.lap_times.append(time.time() - car.init_time_)

# Mark that the car is on the finish line
car.on_finish_line = True
else:
# Car is not on the finish line
car.on_finish_line = False

def draw_standings_table(self, cars: list[Car]):
# Sort cars by laps (descending) and then by distance (descending)
sorted_cars = sorted(
cars,
key=lambda car: (
-car.laps,
car.lap_times[-1] if car.lap_times else float("inf"),
),
)
# Table settings
table_width = 300
header_height = 60
row_height = 30
table_height = min(
HEIGHT - 40, header_height + len(cars) * row_height
) # Adjust based on number of cars
table_x = WIDTH - table_width - 20
table_y = 20

# Draw table background
pygame.draw.rect(
self.screen, (17, 75, 95), (table_x, table_y, table_width, table_height)
)
pygame.draw.rect(
self.screen, (0, 0, 0), (table_x, table_y, table_width, table_height), 2
)

# Draw header
header_font = pygame.font.SysFont("Arial", 20, bold=True)
header = header_font.render("Race Standings", True, (255, 255, 255))
header_2 = header_font.render(f"Total laps: {self.laps}", True, (255, 255, 255))
self.screen.blit(header, (table_x + 10, table_y + 5))
self.screen.blit(header_2, (table_x + 180, table_y + 5))

# Draw column titles
column_font = pygame.font.SysFont("Arial", 16, bold=True)
pos_title = column_font.render("Pos", True, (255, 255, 255))
name_title = column_font.render("Name", True, (255, 255, 255))
laps_title = column_font.render("Laps", True, (255, 255, 255))
dist_title = column_font.render("time", True, (255, 255, 255))
self.screen.blit(pos_title, (table_x + 10, table_y + 35))
self.screen.blit(name_title, (table_x + 60, table_y + 35))
self.screen.blit(laps_title, (table_x + 140, table_y + 35))
self.screen.blit(dist_title, (table_x + 200, table_y + 35))

# Draw standings
font = pygame.font.SysFont("Arial", 16)
for i, car in enumerate(sorted_cars):
y = table_y + (i + 2) * row_height
if y + row_height > table_y + table_height:
break # Stop if we run out of space in the table

# Draw alternating row backgrounds
if i % 2 == 0:
pygame.draw.rect(
self.screen, (17, 75, 95), (table_x, y, table_width, row_height)
)
else:
pygame.draw.rect(
self.screen, (26, 147, 111), (table_x, y, table_width, row_height)
)

if car.lap_times:
last_lap_time = car.lap_times[-1]
else:
last_lap_time = 0

pos_text = font.render(f"{i + 1}", True, (255, 255, 255))
name_text = font.render(f"{car.name}", True, (255, 255, 255))
laps_text = font.render(f"{car.laps}", True, (255, 255, 255))
dist_text = font.render(f"{last_lap_time:.2f}", True, (255, 255, 255))

self.screen.blit(pos_text, (table_x + 10, y + 5))
self.screen.blit(name_text, (table_x + 60, y + 5))
self.screen.blit(laps_text, (table_x + 140, y + 5))
self.screen.blit(dist_text, (table_x + 200, y + 5))

# Redraw the border to clean up any overflow
pygame.draw.rect(
self.screen, (0, 0, 0), (table_x, table_y, table_width, table_height), 2
)
1 change: 1 addition & 0 deletions sarsa/sarsa_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
self.rotated_sprite = self.sprite

self.crashed = False
self.name = "SARSA"

def create_model(self, input_size, hidden_size, output_size, trainable=True):
model = nn.Sequential(
Expand Down

0 comments on commit 72cab41

Please sign in to comment.