Add end of night text overlay
authorNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 13:39:23 +0000 (15:39 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 13:39:30 +0000 (15:39 +0200)
tabakrolletjie/scenes/night.py

index aadff681400cd67a38794eba040dbe71a800d977..848bb54fa8362def38a6b01d16e7b13d86523a94 100644 (file)
@@ -1,5 +1,6 @@
 """ In the night, the mould attacks. """
 
+import pygame.surface
 import pygame.locals as pgl
 
 import pymunk
@@ -9,11 +10,11 @@ from ..lights import LightManager
 from ..obstacles import ObstacleManager
 from ..enemies import Boyd
 from ..events import SceneChangeEvent
-from ..utils import debug_timer
+from ..utils import debug_timer, shadowed_text
 from ..loader import loader
 from ..transforms import Overlay
 from ..turnip import Turnip
-from ..constants import NIGHT_LENGTH, DEBUG
+from ..constants import NIGHT_LENGTH, DEBUG, FONTS, SCREEN_SIZE
 
 
 class NightScene(BaseScene):
@@ -32,6 +33,9 @@ class NightScene(BaseScene):
         self._soil = loader.load_image(
             "textures", "soil.png", transform=self.DARKNESS)
         self._total_ticks = 0
+        self._do_ticks = True
+        self._eaten_tonight = 0
+        self._night_over_text = []
 
     @debug_timer("night.render")
     def render(self, surface, gamestate):
@@ -44,6 +48,7 @@ class NightScene(BaseScene):
                 self._turnips.remove(turnip)
                 turnip.remove()
                 gamestate.eaten += 1
+                self._eaten_tonight += 1
             else:
                 turnip.render(surface)
 
@@ -51,14 +56,23 @@ class NightScene(BaseScene):
         self._obstacles.render(surface)
         self._lights.render_fittings(surface)
 
+        for text, text_pos in self._night_over_text:
+            surface.blit(text, text_pos, None)
+
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN:
+            if not self._do_ticks:
+                # Any keypress exits
+                self._to_day()
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
                 from .menu import MenuScene
                 SceneChangeEvent.post(scene=MenuScene())
             if ev.key == pgl.K_e and DEBUG:
-                self._to_day()
+                self._end_night()
         elif ev.type == pgl.MOUSEBUTTONDOWN:
+            if not self._do_ticks:
+                # Any mouse press exits
+                self._to_day()
             if ev.button == 1:
                 self._lights.toggle_nearest(ev.pos, surfpos=True)
                 print self._lights.lit_by(ev.pos, surfpos=True)
@@ -68,16 +82,36 @@ class NightScene(BaseScene):
         from .day import DayScene
         SceneChangeEvent.post(scene=DayScene())
 
+    def _end_night(self):
+        self._do_ticks = False
+        self._night_over_text = []
+        overlay = pygame.surface.Surface(
+            (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
+        overlay.fill((0, 0, 0, 172))
+        self._night_over_text.append((overlay, (0, 40)))
+        self._night_over_text.append(
+            (shadowed_text("The Night is Over", FONTS["bold"], 48), (300, 50)))
+        self._night_over_text.append(
+            (shadowed_text("Turnips eaten tonight: %d" % self._eaten_tonight,
+                           FONTS["sans"], 32), (300, 130)))
+        self._night_over_text.append(
+            (shadowed_text("Surviving turnips: %d" % len(self._turnips),
+                           FONTS["sans"], 32), (300, 170)))
+        self._night_over_text.append(
+            (shadowed_text("Press any key to continue", FONTS["sans"], 24),
+             (350, 240)))
+
     @debug_timer("night.tick")
     def tick(self, gamestate):
-        if self._total_ticks < NIGHT_LENGTH:
-            self._mould.tick(gamestate, self._space, self._lights)
-            self._lights.tick()
-            print "Power usage: ", self._lights.total_power_usage()
-        else:
-            self._to_day()
-        if not self._mould.alive():
-            self._to_day()
+        if self._do_ticks:
+            if self._total_ticks < NIGHT_LENGTH:
+                self._mould.tick(gamestate, self._space, self._lights)
+                self._lights.tick()
+                print "Power usage: ", self._lights.total_power_usage()
+            else:
+                self._end_night()
+            if not self._mould.alive():
+                self._end_night()
 
     def exit(self, gamestate):
         turnip_data = [turnip.serialize() for turnip in self._turnips]