Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (93 loc) · 4.06 KB
/
Copy pathmain.py
File metadata and controls
114 lines (93 loc) · 4.06 KB
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
"""
main.py — Main entry point for the evolutionary cars simulation.
Coordinates Pygame initialization, the circuit creation menu,
the renderer, and the simulation loop with restart support.
"""
import os
import sys
import pygame
import utils.constants as cte
from ui.menu_circuit import show_initial_menu, generate_circuit
from ui.renderer import Renderer
from core.simulation import Simulation
from utils.config import load_config
from ui.menu_config import show_config_menu
def initialize_display():
"""Initializes or recreates the display surface."""
if cte.FULLSCREEN_MODE:
display_info = pygame.display.Info()
sw, sh = display_info.current_w, display_info.current_h
flags = pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE
else:
sw, sh = 1600, 900
flags = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.RESIZABLE
# Try to use logical reference resolution if not fullscreen
surface = pygame.display.set_mode((sw, sh), flags)
pygame.display.set_caption("Evolutionary Cars — NumPy Edition")
return surface
def main():
# --- 1. Pygame Initialization ---
pygame.init()
display_surface = initialize_display()
config = load_config()
# Orchestration loop (allows returning to initial menu)
while True:
# --- 2. Circuit Selection / Creation ---
# The menu can return circuit data or a screen change SIGNAL
menu_result = show_initial_menu(display_surface)
if isinstance(menu_result, str):
if menu_result == "TOGGLE_FS":
cte.FULLSCREEN_MODE = not cte.FULLSCREEN_MODE
display_surface = initialize_display()
continue
elif menu_result == "SETTINGS":
config = show_config_menu(display_surface, config)
continue
elif menu_result == "DEFAULT":
circuit_data = generate_circuit(scale=1.0, track_width=85)
circuit_name = "default"
elif menu_result == "QUIT":
pygame.quit(); sys.exit()
else:
continue
elif isinstance(menu_result, tuple) and menu_result[0] == "NEW":
circuit_data = menu_result[1:]
import datetime
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
circuit_name = f"custom_{timestamp}"
else:
circuit_data = menu_result
import datetime
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
circuit_name = f"custom_{timestamp}"
# --- 3. Initialization of Simulation Components ---
inner, outer, center, cumul = circuit_data
start_finish_line = (inner[0], outer[0])
renderer = Renderer(display_surface, inner, outer, start_finish_line)
sim = Simulation(circuit_data, renderer, config, circuit_name=circuit_name)
# --- 4. Simulation Loop Execution ---
# run() will return a signal indicating if it wants to:
# - "QUIT": Exit the program
# - "RESTART": Return to circuit selection menu
# - "TOGGLE_FS": Toggle fullscreen
while True:
signal = sim.run()
if signal == "QUIT":
pygame.quit()
sys.exit()
elif signal == "RESTART":
break # Break inner loop to return to show_initial_menu
elif signal == "TOGGLE_FS":
# Change configuration and recreate window
cte.FULLSCREEN_MODE = not cte.FULLSCREEN_MODE
display_surface = initialize_display()
renderer.update_surface(display_surface)
continue
elif signal == "RESIZE":
# Simply detect that we have resized
# (Pygame already updated display_surface internally in some cases,
# but we make sure the renderer knows about it)
renderer.update_surface(display_surface)
continue
if __name__ == "__main__":
main()