1313from CSVParser import CSVParser
1414
1515class Results :
16- def __init__ (self , x , y , w , h , bg_color , color , font ):
16+ def __init__ (self , x , y , w , h , bg_color , color , font , button_font ):
1717 self .x = x
1818 self .y = y
1919 self .w = w
2020 self .h = h
2121 self .bg_color = bg_color
2222 self .color = color
2323 self .font = font
24+ self .button_font = button_font
2425 self .bg_rect = pg .Rect (x ,y ,w ,h )
2526 self .finished_rect = pg .Rect (x + 10 , y + 10 , w - 20 , 40 )
26- def draw (self , surf , times ):
27+ self .return_to_menu_rect = pg .Rect (x + 10 , y + h - 30 , w // 2 - 20 , 25 )
28+ self .restart_rect = pg .Rect (x + w // 2 + 10 , y + h - 30 , w // 2 - 20 , 25 )
29+ self .return_text_rect = pg .Rect (x + 10 , y + h - 30 , w // 2 - 20 , 25 )
30+ self .restart_text_rect = pg .Rect (x + w // 2 + 10 , y + h - 30 , w // 2 - 20 , 25 )
31+
32+ def draw (self , surf , times , pressed ):
2733 pg .draw .rect (surf , self .bg_color , self .bg_rect )
2834 pg .draw .rect (surf , self .color , self .finished_rect )
2935 finished = self .font .render ("FINISHED!" , 1 , (0 ,0 ,0 ))
@@ -32,6 +38,30 @@ def draw(self, surf, times):
3238 for i in range (6 ):
3339 t = self .font .render (times [i ], 1 , (0 ,0 ,0 ))
3440 surf .blit (t , t .get_rect (center = results [i ].center ))
41+
42+ return_button = pg .draw .rect (surf , self .color , self .return_to_menu_rect )
43+ restart_button = pg .draw .rect (surf , self .color , self .restart_rect )
44+ return_text = self .button_font .render ("Return" ,1 ,(0 ,0 ,0 ))
45+ restart_text = self .button_font .render ("Restart" ,1 ,(0 ,0 ,0 ))
46+ surf .blit (return_text , return_text .get_rect (center = self .return_text_rect .center ))
47+ surf .blit (restart_text , restart_text .get_rect (center = self .restart_text_rect .center ))
48+ if return_button .collidepoint (pg .mouse .get_pos ()):
49+ return_color = (240 ,230 ,140 )
50+ if pressed :
51+ return 0
52+ else :
53+ return_color = (100 ,200 ,255 )
54+
55+ if restart_button .collidepoint (pg .mouse .get_pos ()):
56+ restart_color = (240 ,230 ,140 )
57+ if pressed :
58+ return 1
59+ else :
60+ restart_color = (100 ,200 ,255 )
61+
62+ pg .draw .rect (return_text , return_color , [self .x + 10 , self .y + self .h - 30 , self .w // 2 - 20 , 25 ])
63+ pg .draw .rect (restart_text , restart_color , [self .x + self .w // 2 + 10 , self .y + self .h - 30 , self .w // 2 - 20 , 25 ])
64+ return 2
3565
3666class NitroBar ():
3767 def __init__ (self , x , y , w , h , bg_color , color , font ):
@@ -63,7 +93,7 @@ def __init__(self, refresh_rate, name, car, map):
6393 self .car = car
6494 self .map = map
6595 self .nitro_bar = NitroBar (5 , 5 , 250 , 40 , (240 , 230 , 140 ), (100 , 100 , 255 ), pg .font .SysFont ('Calibri' , 35 ))
66- self .results_popup = Results (1480 // 2 - 300 // 2 , 780 // 2 - 400 // 2 , 300 , 400 , (240 , 230 , 140 ), (100 , 100 , 255 ), pg .font .SysFont ('Calibri' , 35 ))
96+ self .results_popup = Results (1480 // 2 - 300 // 2 , 780 // 2 - 400 // 2 , 300 , 400 , (240 , 230 , 140 ), (100 , 100 , 255 ), pg .font .SysFont ('Calibri' , 35 ), pg . font . SysFont ( 'Calibri' , 25 ))
6797
6898 def spawn_booster (self , map : Map , dt ):
6999 if random .randrange (0 , 256 ) != 8 :
@@ -126,7 +156,6 @@ def run(self):
126156
127157 id , name , engine = CSVParser (None , None , "./data/Cars.csv" ).read_car_statistics (self .car )
128158 car = Car (id , Vector2D (50 , 100 ), 0 , 0 , 10 , engine , name , curr_map )
129-
130159 while run :
131160 dt = self .clock .tick (self .refresh )
132161 self .screen .blit (map_img , (0 , 0 ))
@@ -151,14 +180,21 @@ def run(self):
151180
152181 self .display_laps (curr_map )
153182
183+ pressed = False
154184 for event in pg .event .get ():
155185 if event .type == pg .QUIT :
156186 run = False
187+ elif event .type == pg .MOUSEBUTTONDOWN :
188+ pressed = True
157189
158190 ticks = pg .time .get_ticks ()
159191 stopwatch .display_timer (ticks )
160192
161193 if curr_map .won == 1 :
162- self .results_popup .draw (self .screen , curr_map .times )
163-
194+ r = self .results_popup .draw (self .screen , curr_map .times , pressed )
195+ if r < 2 :
196+ run = 0
197+ self .run ()
198+
164199 pg .display .flip ()
200+ pg .display .set_mode ((1080 , 720 ))
0 commit comments