From 02b42d2de7cd458904f6adea4afd05f7b2c478e8 Mon Sep 17 00:00:00 2001 From: Ferry Schoenmakers Date: Wed, 23 Oct 2024 09:13:54 +0200 Subject: [PATCH] Add zandvoort track and add utility for easier track creation --- racer/tracks/create_track.py | 107 +++++++++++++++++++++++++++++++++++ racer/tracks/zandvoort.py | 78 +++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 racer/tracks/create_track.py create mode 100644 racer/tracks/zandvoort.py diff --git a/racer/tracks/create_track.py b/racer/tracks/create_track.py new file mode 100644 index 0000000..57b0991 --- /dev/null +++ b/racer/tracks/create_track.py @@ -0,0 +1,107 @@ +from PIL import Image, ImageTk +import tkinter as tk +from tkinter import filedialog +from math import ceil + +def resize_image(image, window): + # Get screen width and height + screen_width = window.winfo_screenwidth() + screen_height = window.winfo_screenheight() + + # Calculate maximum dimensions while maintaining aspect ratio + max_width = int(screen_width * 0.95) # Use 95% of screen width + max_height = int(screen_height * 0.95) # Use 95% of screen height + + # Calculate scaling factor + width_ratio = max_width / image.width + height_ratio = max_height / image.height + scale_factor = min(width_ratio, height_ratio) + track_width = 25 + + print(f"scale = {scale_factor}") + print(f"track_width = {track_width}") + print("lines = [") + + # Calculate new dimensions + new_width = int(image.width * scale_factor) + new_height = int(image.height * scale_factor) + + # Resize image + return image.resize((new_width, new_height), Image.LANCZOS), scale_factor, track_width + +def on_click(event): + global last_point, temp_line + x, y = event.x, event.y + + # Adjust coordinates back to original scale + orig_x = int(x / scale_factor) + orig_y = int(y / scale_factor) + + print(f" ({orig_x}, {orig_y}),") + + if last_point: + line = canvas.create_line(last_point[0], last_point[1], x, y, fill="red", width=2) + lines.append(line) + + last_point = (x, y) + + if temp_line: + canvas.delete(temp_line) + temp_line = canvas.create_line(x, y, x, y, fill="blue", dash=(4, 4)) + +def update_cursor(event): + global temp_line, track_width + x, y = event.x, event.y + + canvas.delete("circle") + canvas.create_oval(x-track_width, y-track_width, x+track_width, y+track_width, + outline="red", width=2, tags="circle") + + if last_point and temp_line: + canvas.coords(temp_line, last_point[0], last_point[1], x, y) + +def on_closing(): + window.quit() + window.destroy() + +# Open an image file +root = tk.Tk() +root.withdraw() +file_path = filedialog.askopenfilename() +if not file_path: + print("No file selected. Exiting.") + root.quit() + exit() + +# Create main window first to get screen dimensions +window = tk.Toplevel() + +# Open and resize image +original_image = Image.open(file_path) +image, scale_factor, track_width = resize_image(original_image, window) +rgb_image = image.convert('RGB') + +# Create PhotoImage +photo = ImageTk.PhotoImage(rgb_image) + +# Create a canvas and display the image on it +canvas = tk.Canvas(window, width=image.width, height=image.height) +canvas.pack() +canvas.create_image(0, 0, anchor=tk.NW, image=photo) + +# Initialize variables +lines = [] +last_point = None +temp_line = None + +# Bind events to the canvas +canvas.bind("", on_click) +canvas.bind("", update_cursor) + +# Set up the window close event +window.protocol("WM_DELETE_WINDOW", on_closing) + +# Start the Tkinter event loop +window.mainloop() +print("]") + diff --git a/racer/tracks/zandvoort.py b/racer/tracks/zandvoort.py new file mode 100644 index 0000000..375c260 --- /dev/null +++ b/racer/tracks/zandvoort.py @@ -0,0 +1,78 @@ +import os.path + +import pygame + +name = "Zandvoort circuit" +background = pygame.image.load(os.path.splitext(__file__)[0] + '.webp') +scale = 0.8 +track_width = 25 +lines = [ + (639, 1565), + (299, 1567), + (213, 1550), + (160, 1499), + (149, 1433), + (190, 1358), + (288, 1330), + (485, 1332), + (536, 1328), + (644, 1285), + (692, 1264), + (734, 1265), + (775, 1299), + (833, 1353), + (878, 1381), + (927, 1373), + (961, 1330), + (949, 1259), + (831, 1121), + (780, 1047), + (745, 946), + (720, 820), + (699, 768), + (613, 682), + (545, 624), + (487, 516), + (420, 288), + (434, 170), + (493, 119), + (548, 107), + (657, 117), + (936, 147), + (1017, 154), + (1065, 175), + (1086, 233), + (1096, 328), + (1085, 447), + (1078, 510), + (1052, 553), + (1004, 565), + (946, 540), + (858, 422), + (825, 362), + (765, 326), + (690, 339), + (656, 387), + (664, 468), + (702, 555), + (803, 735), + (929, 879), + (1062, 1000), + (1088, 1035), + (1085, 1070), + (1052, 1111), + (1048, 1161), + (1088, 1196), + (1125, 1197), + (1378, 1052), + (1458, 1017), + (1529, 1012), + (1574, 1048), + (1622, 1128), + (1670, 1259), + (1666, 1386), + (1618, 1484), + (1537, 1542), + (1390, 1564), + (1255, 1565), +]