X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Fscenes%2Fday.py;h=017675432ce71828a012c7e015cd1f10c6125978;hb=b4d0997da0f17f5277c473a2d2ca708573181295;hp=4ab14e713fc8b51f6f2ed2cbb4fb8ee09dece8e6;hpb=6a370ed299395d469efcc9561ea56bc7dcd9b4ae;p=tabakrolletjie.git diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 4ab14e7..0176754 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -3,20 +3,22 @@ import math import pygame.display +import pygame.surface import pygame.locals as pgl import pymunk import pymunk.pygame_util from .base import BaseScene +from ..battery import BatteryManager from ..lights import LightManager, light_fitting_by_type 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, Alpha, ColourWedges -from ..constants import SCREEN_SIZE, FONTS +from ..constants import SCREEN_SIZE, FONTS, DEBUG from ..widgets import ImageButton from ..turnip import Turnip, TurnipInvalidPosition @@ -30,6 +32,7 @@ class DayScene(BaseScene): self._toolbar_font = loader.load_font(FONTS['sans'], size=20) self._obstacles = ObstacleManager(self._space, gamestate) self._lights = LightManager(self._space, gamestate) + self._battery = BatteryManager(gamestate) self._turnips = [] self._seeds = gamestate.seeds self._harvested = gamestate.harvested @@ -46,6 +49,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: @@ -60,7 +81,9 @@ class DayScene(BaseScene): def create_tools(self, gamestate): tools = [] + x, y, step = 50, SCREEN_SIZE[1] - 40, 50 + tools.append(ImageButton('32', 'seed.png', name='seed', pos=(x, y))) x += step @@ -74,6 +97,12 @@ class DayScene(BaseScene): tools.append(ImageButton( '32', 'default_cursor.png', name='reset tool', pos=(x, y))) + + tools.append(ImageButton( + '32', 'night.png', name='start night', + pos=(SCREEN_SIZE[0] - 100, y))) + tools.append(ImageButton( + '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, y))) return tools def exit(self, gamestate): @@ -83,6 +112,11 @@ class DayScene(BaseScene): turnip_data = [turnip.serialize() for turnip in self._turnips] gamestate.turnips = turnip_data + def end_day(self, gamestate): + self._battery.apply_recharge() + from .night import NightScene + SceneChangeEvent.post(scene=NightScene()) + @debug_timer("day.render") def render(self, surface, gamestate): surface.blit(self._soil, (0, 0)) @@ -98,6 +132,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): height = SCREEN_SIZE[1] - 80 @@ -165,14 +202,17 @@ 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 SceneChangeEvent.post(scene=MenuScene()) elif ev.key == pgl.K_e: - from .night import NightScene - SceneChangeEvent.post(scene=NightScene()) - elif ev.key == pgl.K_SPACE: + self.end_day(gamestate) + elif ev.key == pgl.K_SPACE and DEBUG: self._paused = not self._paused elif ev.type == pgl.MOUSEBUTTONDOWN: if ev.button == 1: @@ -184,6 +224,11 @@ class DayScene(BaseScene): self._unset_cursor() self._tool = None self._clear_light_toolbar() + elif tool.name == 'start night': + self.end_day(gamestate) + elif tool.name == 'exit': + from .menu import MenuScene + SceneChangeEvent.post(scene=MenuScene()) else: self._tool = tool if self._tool.name == 'seed': @@ -225,6 +270,7 @@ class DayScene(BaseScene): elif self._tool: # Unset tool self._tool = None + self._clear_light_toolbar() self._unset_cursor() elif ev.type == pgl.MOUSEMOTION: if self._dragging: