import pygame.mixer
from .constants import DEBUG
+from .transforms import NullTransform
class Loader(object):
""" Load data files from beneath a prefix. """
+ NULL_TRANSFORM = NullTransform()
+
def __init__(self, prefix):
self._prefix = prefix
self._cache = {}
with self.open_file("stations", *parts) as f:
return json.load(f)
- def load_image(self, *parts):
+ def load_image(self, *parts, **kwargs):
"""Return a pygame surface of the requested image."""
fn = self.full_path("images", *parts)
- img = self._cache.get(fn, None)
+ transform = kwargs.pop("transform", self.NULL_TRANSFORM)
+ img = self._cache.get((fn, transform.hash), None)
if img is None:
img = pygame.image.load(fn)
# We assume pygame.display has been initialised
# Fix this if that changes
img.convert_alpha(pygame.display.get_surface())
- self._cache[fn] = img
+ img = transform.apply(img)
+ self._cache[(fn, transform.hash)] = img
return img
def load_font(self, *parts, **kwargs):
""" Be prepared. """
import pygame.locals as pgl
-import pygame.surface
import pymunk
import pymunk.pygame_util
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
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)
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()
@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)
""" In the night, the mould attacks. """
import pygame.locals as pgl
-import pygame.surface
import pymunk
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)
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)
gamestate.eaten += 1
else:
turnip.render(surface)
+
self._lights.render_light(surface)
self._obstacles.render(surface)
self._lights.render_fittings(surface)
--- /dev/null
+""" Image transformations for use with the image loader. """
+
+import pygame.surface
+
+
+class Transform(object):
+
+ ARGS = []
+
+ def __init__(self, **kw):
+ for k in self.ARGS:
+ setattr(self, k, kw.pop(k))
+ assert not kw
+ self.hash = "%s: <%s>" % (
+ self.__class__.__name__,
+ ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS))
+ return
+
+ def apply(self, surface):
+ raise NotImplementedError("Transforms should implement .apply")
+
+
+class NullTransform(Transform):
+ """ Do nothing. """
+
+ def apply(self, surface):
+ return surface
+
+
+class Overlay(Transform):
+ """ Apply a colour overlay. """
+
+ ARGS = ["colour"]
+
+ def apply(self, surface):
+ over = pygame.surface.Surface(surface.get_size())
+ over = over.convert_alpha()
+ over.fill(self.colour)
+ surface.blit(over, (0, 0), None)
+ return surface