Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable scenario termination from closing display #238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/scenic/core/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
continueAfterDivergence=False,
verbosity=0,
):
self.screen = None
self.result = None
self.scene = scene
self.objects = []
Expand Down Expand Up @@ -430,6 +431,13 @@
terminationReason = newReason
terminationType = TerminationType.terminatedByMonitor

# Check if users manually closed out display for simulator
if "Dead" in str(self.screen):
return (

Check warning on line 436 in src/scenic/core/simulators.py

View check run for this annotation

Codecov / codecov/patch

src/scenic/core/simulators.py#L436

Added line #L436 was not covered by tests
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 @@ -866,6 +874,9 @@
#: 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 @@ -70,6 +70,7 @@
def __init__(self, scene, network, render, timestep, **kwargs):
self.render = render
self.network = network
self.screen = None

if timestep is None:
timestep = 0.1
Expand Down Expand Up @@ -186,6 +187,11 @@
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

Check warning on line 194 in src/scenic/simulators/newtonian/simulator.py

View check run for this annotation

Codecov / codecov/patch

src/scenic/simulators/newtonian/simulator.py#L193-L194

Added lines #L193 - L194 were not covered by tests
self.draw_objects()
pygame.event.pump()

Expand Down
Loading