Skip to content

Commit

Permalink
feat: enable dynamic scenario termination from closing display (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Armando Banuelos <[email protected]>
Co-authored-by: Lola Marrero <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent a6e48a2 commit 26d9da7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/scenic/core/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def __init__(
continueAfterDivergence=False,
verbosity=0,
):
self.screen = None
self.result = None
self.scene = scene
self.objects = []
Expand Down Expand Up @@ -432,6 +433,13 @@ def _run(self, dynamicScenario, maxSteps):
terminationReason = newReason
terminationType = TerminationType.terminatedByMonitor

# Check if users manually closed out display for simulator
if "Dead" in str(self.screen):
return (
TerminationType.terminatedByUser,
"user manually terminated simulation",
)

# "Always" and scenario-level requirements have been checked;
# now safe to terminate if the top-level scenario has finished,
# a monitor requested termination, or we've hit the timeout
Expand Down Expand Up @@ -886,6 +894,9 @@ class TerminationType(enum.Enum):
#: A :term:`dynamic behavior` used :keyword:`terminate simulation` to end the simulation.
terminatedByBehavior = "a behavior terminated the simulation"

#: A user manually intervenes and closes display window
terminatedByUser = "manually terminated by user"


class SimulationResult:
"""Result of running a simulation.
Expand Down
6 changes: 6 additions & 0 deletions src/scenic/simulators/newtonian/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(
self.export_gif = export_gif
self.render = render
self.network = network
self.screen = None
self.frames = []
self.debug_render = debug_render

Expand Down Expand Up @@ -226,6 +227,11 @@ def step(self):
obj.heading += obj.angularSpeed * self.timestep

if self.render:
# Handle closing out pygame screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.destroy()
return
self.draw_objects()
pygame.event.pump()

Expand Down

0 comments on commit 26d9da7

Please sign in to comment.