Don't repeatedly reload soil.
authorSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 19:12:08 +0000 (21:12 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 19:12:08 +0000 (21:12 +0200)
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/night.py

index c28070203c20d6dd7b872af17807988844e1d716..9e55fba1ce3c9763cf93fbdc53ec04a9e496475c 100644 (file)
@@ -1,7 +1,6 @@
 """ Be prepared. """
 
 import pygame.locals as pgl
-import pygame.surface
 
 import pymunk
 import pymunk.pygame_util
@@ -12,6 +11,7 @@ from ..obstacles import ObstacleManager
 from ..events import SceneChangeEvent
 from ..utils import debug_timer
 from ..loader import loader
+from ..transforms import Overlay
 
 from ..constants import SCREEN_SIZE, FONTS
 from ..widgets import ImageButton
@@ -19,6 +19,9 @@ from ..turnip import Turnip, TurnipInvalidPosition
 
 
 class DayScene(BaseScene):
+
+    BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
+
     def enter(self, gamestate):
         self._space = pymunk.Space()
         self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
@@ -46,6 +49,9 @@ class DayScene(BaseScene):
                         pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
         ]
         self._update_toolbar(gamestate)
+        # Background
+        self._soil = loader.load_image(
+            "textures", "soil.png", transform=self.BRIGHTNESS)
 
     def exit(self, gamestate):
         self._unset_cursor()
@@ -56,12 +62,8 @@ class DayScene(BaseScene):
 
     @debug_timer("day.render")
     def render(self, surface, gamestate):
-        surface.blit(loader.load_image("textures", "soil.png"), (0, 0))
-        brightness = pygame.surface.Surface(surface.get_size())
-        brightness = brightness.convert_alpha()
-        brightness.fill((255, 255, 255, 50))
-        surface.blit(brightness, (0, 0))
-        
+        surface.blit(self._soil, (0, 0))
+
         for turnip in self._turnips:
             turnip.render(surface)
         self._lights.render_light(surface)
@@ -122,7 +124,7 @@ class DayScene(BaseScene):
 
     def _update_toolbar(self, gamestate):
         text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
-                "Harvested: %d. Destroyed: %d" % 
+                "Harvested: %d. Destroyed: %d" %
                 (self._seeds, len(self._turnips),
                  self._harvested, gamestate.eaten))
         self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))
index fe1fae3860d1d87bcb2b8a77d2ff895afff904ed..d8f0e1a53144d9993734644cb3d5bc65d37abce7 100644 (file)
@@ -1,7 +1,6 @@
 """ In the night, the mould attacks. """
 
 import pygame.locals as pgl
-import pygame.surface
 
 import pymunk
 
@@ -12,10 +11,14 @@ from ..enemies import Boyd
 from ..events import SceneChangeEvent
 from ..utils import debug_timer
 from ..loader import loader
+from ..transforms import Overlay
 from ..turnip import Turnip
 
 
 class NightScene(BaseScene):
+
+    DARKNESS = Overlay(colour=(0, 0, 0, 150))
+
     def enter(self, gamestate):
         self._space = pymunk.Space()
         self._obstacles = ObstacleManager(self._space, gamestate)
@@ -25,15 +28,13 @@ class NightScene(BaseScene):
         for turnip_data in gamestate.turnips:
             turnip = Turnip(space=self._space, **turnip_data)
             self._turnips.append(turnip)
+        self._soil = loader.load_image(
+            "textures", "soil.png", transform=self.DARKNESS)
 
     @debug_timer("night.render")
     def render(self, surface, gamestate):
-        surface.blit(loader.load_image("textures", "soil.png"), (0, 0))
-        darkness = pygame.surface.Surface(surface.get_size())
-        darkness = darkness.convert_alpha()
-        darkness.fill((0, 0, 0, 150))
-        surface.blit(darkness, (0, 0))
-        
+        surface.blit(self._soil, (0, 0))
+
         self._mould.render(surface)
 
         for turnip in self._turnips[:]:
@@ -43,6 +44,7 @@ class NightScene(BaseScene):
                 gamestate.eaten += 1
             else:
                 turnip.render(surface)
+
         self._lights.render_light(surface)
         self._obstacles.render(surface)
         self._lights.render_fittings(surface)