Skip to content

Commit 985cf76

Browse files
committed
improve stop iteration
1 parent 07a4c09 commit 985cf76

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

lineflow/simulation/visualization.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ def draw_cursor(self):
267267
def check_user_input(self):
268268
for event in pygame.event.get():
269269
if event.type == pygame.QUIT:
270-
raise StopIteration()
271-
270+
return False
271+
272272
keys = pygame.key.get_pressed()
273273
if keys[pygame.K_q]:
274274
self.viewpoint.z -= 3*self.dt
@@ -286,34 +286,38 @@ def check_user_input(self):
286286
self.halt_event.set()
287287
self.viewpoint.z = max(0.5,min(10,self.viewpoint.z))
288288
self.view = pygame.Vector2(self.viewpoint.x, self.viewpoint.y)
289+
return True
289290

290291
def run(self):
291292

292293
pygame.init()
293294
self.screen = pygame.display.set_mode(self.size)
294295
self.clock = pygame.time.Clock()
295296

296-
while True:
297-
if self.stop_event.is_set():
298-
break
297+
try:
298+
while True:
299+
if self.stop_event.is_set():
300+
break
299301

300-
self.check_user_input()
301-
self.check_connection()
302-
self.clear()
303-
self.draw_connectors()
304-
self.draw_stations()
305-
self.draw_carriers()
306-
self.draw_user_input()
307-
self.draw_info()
308-
self.draw_actions()
309-
self.draw_cursor()
302+
if not self.check_user_input():
303+
break
310304

311-
pygame.display.flip()
312-
313-
self.dt = self.clock.tick(60)/1000
314-
315-
pygame.quit()
316-
self.stop_event.set()
305+
self.check_connection()
306+
self.clear()
307+
self.draw_connectors()
308+
self.draw_stations()
309+
self.draw_carriers()
310+
self.draw_user_input()
311+
self.draw_info()
312+
self.draw_actions()
313+
self.draw_cursor()
314+
315+
pygame.display.flip()
316+
317+
self.dt = self.clock.tick(60)/1000
318+
finally:
319+
pygame.quit()
320+
self.stop_event.set()
317321

318322
def start_visualization(connection, stop_event, halt_event):
319323
visualization = Visualization(connection=connection, stop_event=stop_event, halt_event=halt_event)

0 commit comments

Comments
 (0)