Add 'game over' check and display
authorNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 14:04:22 +0000 (16:04 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 14:05:27 +0000 (16:05 +0200)
tabakrolletjie/scenes/day.py

index 4c81130c9e7e77104ad24ce84a92dc35547dfb0e..334e59f971b8189d0f20e644aaf046e0ad9feeb3 100644 (file)
@@ -3,6 +3,7 @@
 import math
 
 import pygame.display
+import pygame.surface
 import pygame.locals as pgl
 
 import pymunk
@@ -12,7 +13,7 @@ from .base import BaseScene
 from ..lights import LightManager
 from ..obstacles import ObstacleManager
 from ..events import SceneChangeEvent
-from ..utils import debug_timer
+from ..utils import debug_timer, shadowed_text
 from ..loader import loader
 from ..transforms import Overlay, Multiply, Alpha
 
@@ -46,6 +47,24 @@ class DayScene(BaseScene):
         # Background
         self._soil = loader.load_image(
             "textures", "soil.png", transform=self.BRIGHTNESS)
+        # Check if we've lost
+        self._game_over_text = []
+        if self._seeds == 0 and len(self._turnips) == 0:
+            self._draw_game_over_text()
+
+    def _draw_game_over_text(self):
+        overlay = pygame.surface.Surface(
+            (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
+        overlay.fill((0, 0, 0, 128))
+        self._game_over_text.append((overlay, (0, 250)))
+        self._game_over_text.append(
+            (shadowed_text("You Lost", FONTS["bold"], 48), (400, 280)))
+        self._game_over_text.append(
+            (shadowed_text("You have no seeds and no turnips growing",
+                           FONTS["sans"], 24), (250, 350)))
+        self._game_over_text.append(
+            (shadowed_text("Press a key to return to the menu",
+                           FONTS["sans"], 24), (250, 400)))
 
     def grow_turnips(self, gamestate):
         for turnip_data in gamestate.turnips:
@@ -61,7 +80,7 @@ class DayScene(BaseScene):
     def create_tools(self, gamestate):
         tools = []
         x, y, step = 0, SCREEN_SIZE[1] - 40, 50
-        
+
         tools.append(ImageButton(
             '32', 'default_cursor.png', name='reset tool', pos=(x, y)))
         x += step
@@ -104,6 +123,9 @@ class DayScene(BaseScene):
         for light_tool in self._light_toolbar:
             light_tool.render(surface)
         self._draw_cursor(surface)
+        if self._game_over_text:
+            for surf, pos in self._game_over_text:
+                surface.blit(surf, pos)
 
     def _draw_light_toolbar(self, light_config, x):
         self._light_toolbar = []
@@ -166,6 +188,10 @@ class DayScene(BaseScene):
             self._lights.add_light(cfg)
 
     def event(self, ev, gamestate):
+        if self._game_over_text:
+            if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN):
+                from .menu import MenuScene
+                SceneChangeEvent.post(scene=MenuScene())
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
                 from .menu import MenuScene