From cff2127fd334bfe5794b15857508699d2bcbd2d8 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 10 Sep 2016 22:50:47 +0200 Subject: [PATCH] Add ending flag to avoid many scene change events --- tabakrolletjie/scenes/day.py | 9 +++++++++ tabakrolletjie/scenes/night.py | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index e2d1e19..7f73ad2 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -53,6 +53,7 @@ class DayScene(BaseScene): self._draw_you_lose(gamestate) elif gamestate.harvested >= gamestate.turnip_target: self._draw_you_win(gamestate) + self._ending = False def _draw_you_lose(self, gamestate): overlay = pygame.surface.Surface( @@ -128,8 +129,11 @@ class DayScene(BaseScene): gamestate.turnips = turnip_data def end_day(self, gamestate): + if self._ending: + return self._battery.apply_recharge() gamestate.update_lights(self._lights) + self._ending = True from .night import NightScene SceneChangeEvent.post(scene=NightScene()) @@ -226,12 +230,16 @@ class DayScene(BaseScene): self._lights.add_light(cfg) def event(self, ev, gamestate): + if self._ending: + return if self._game_over_text: if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN): + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) if ev.type == pgl.KEYDOWN: if ev.key in (pgl.K_q, pgl.K_ESCAPE): + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) elif ev.key == pgl.K_e: @@ -251,6 +259,7 @@ class DayScene(BaseScene): elif tool.name == 'start night': self.end_day(gamestate) elif tool.name == 'exit': + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) else: diff --git a/tabakrolletjie/scenes/night.py b/tabakrolletjie/scenes/night.py index 9459c0e..7813e32 100644 --- a/tabakrolletjie/scenes/night.py +++ b/tabakrolletjie/scenes/night.py @@ -47,6 +47,7 @@ class NightScene(BaseScene): self._paused = False self._eaten_tonight = 0 self._night_over_text = [] + self._ending = False def create_tools(self, gamestate): tools = [] @@ -105,11 +106,14 @@ class NightScene(BaseScene): surface.blit(text, text_pos, None) def event(self, ev, gamestate): + if self._ending: + return if ev.type == pgl.KEYDOWN: if not self._do_ticks: # Any keypress exits self._to_day(gamestate) if ev.key in (pgl.K_q, pgl.K_ESCAPE): + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) elif ev.key == pgl.K_e and DEBUG: @@ -129,6 +133,7 @@ class NightScene(BaseScene): if tool.name == 'pause play': self.toggle_pause() elif tool.name == 'exit': + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) elif tool.name == 'day': @@ -143,7 +148,10 @@ class NightScene(BaseScene): def _to_day(self, gamestate): # End the night + if self._ending: + return gamestate.update_lights(self._lights) + self._ending = True from .day import DayScene SceneChangeEvent.post(scene=DayScene()) -- 2.34.1