Merge branch 'master' of ctpug:tabakrolletjie
authorNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 18:03:36 +0000 (20:03 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 18:03:36 +0000 (20:03 +0200)
tabakrolletjie/lights.py
tabakrolletjie/scenes/help.py [new file with mode: 0644]
tabakrolletjie/scenes/menu.py
tabakrolletjie/scenes/night.py

index 723da6a9f427ee0304130269bc6f76716dfd8f19..8ef12655ed5da1958a6187a474c8e9c69ea77cf7 100644 (file)
@@ -36,6 +36,7 @@ class LightManager(object):
 
     def __init__(self, space, gamestate):
         self._space = space
+        self._battery_dead = False
         self._lights = [
             BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
         for light in self._lights:
@@ -46,7 +47,14 @@ class LightManager(object):
         self._lights.append(light)
         light.add(self._space)
 
+    def battery_dead(self):
+        self._battery_dead = True
+        for light in self._lights:
+            light.off()
+
     def toggle_nearest(self, *args, **kw):
+        if self._battery_dead:
+            return
         light = self.nearest(*args, **kw)
         if light:
             light.toggle()
@@ -239,6 +247,9 @@ class BaseLight(object):
     def base_damage(self):
         return 10 * self.intensity
 
+    def off(self):
+        self.on = False
+
     def toggle(self):
         self.colour_pos += 1
         if self.colour_pos >= len(self.colour_cycle):
diff --git a/tabakrolletjie/scenes/help.py b/tabakrolletjie/scenes/help.py
new file mode 100644 (file)
index 0000000..f9b35e5
--- /dev/null
@@ -0,0 +1,101 @@
+""" What's going on?! """
+
+from textwrap import wrap
+
+import pymunk
+import pygame.locals as pgl
+
+from .base import BaseScene
+from ..widgets import ImageButton
+from ..constants import SCREEN_SIZE, FONTS, COLOURS
+from ..loader import loader
+from ..events import SceneChangeEvent
+from ..transforms import Multiply, NullTransform, ColourWedges
+
+
+class HelpItem(object):
+    FONT = loader.load_font(FONTS['sans'], size=14)
+    
+    def __init__(self, imgparts, label, transform=NullTransform()):
+        self._img_size = int(imgparts[0])
+        self._img = loader.load_image(*imgparts, transform=transform).convert_alpha()
+
+        maxwidth = SCREEN_SIZE[0] / 2 - self._img_size
+        
+        wrap_width = int(2.25 * maxwidth / self.FONT.get_height())
+
+        self._text = [self.FONT.render(text, True, COLOURS["white"]) for text in wrap(label, wrap_width)]
+
+    def render(self, surface, height, x_offset=0):
+        surface.blit(self._img, (x_offset, height), None)
+        for t in self._text:
+            surface.blit(t, (x_offset + self._img_size + 5, height), None)
+            height += self.FONT.get_height()
+
+
+class HelpScene(BaseScene):
+
+    def enter(self, gamestate):
+        self._space = pymunk.Space()
+        
+        font_title = loader.load_font(FONTS['bold'], size=32)
+        self._title = font_title.render("Help! What's going on?!", True, COLOURS["white"])
+        
+        self._tools = self.create_tools(gamestate)
+        self._items = self.create_items(gamestate)
+
+    def create_tools(self, gamestate):
+        tools = []
+        tools.append(ImageButton(
+            '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)))
+        return tools
+
+    def create_items(self, gamestate):
+        items = [
+            HelpItem(("48", "turnip3.png"), "This is a space turnip, the most valuable vegetable in the universe. A crucial ingredient of longevity serum, space navigator tonic and a pink sweet that everyone loves."),
+            HelpItem(("64", "mouldA.png"), "This is Boyd the space mould. He loves turnips. You've disinfected your ship a thousand times, but somehow he shows up on every planet at night to devour your crop. He is vulnerable to light, but beware -- he builds up a resistance if you overuse the same colour."),
+            HelpItem(("32", "seed.png"), "This is a turnip seed. You get a limited number of these at the start of a level. You can plant them, and you can use them to buy lights to protect your crop. Turnips which survive to harvest yield more seeds."),
+            HelpItem(("48", "lamp.png"), "This is a lamp. It comes in many colours, and has some other varying properties. It is powered by your farm's battery during the night. The battery recharges during the day.", transform=Multiply(colour=COLOURS["yellow"])),
+            HelpItem(("48", "spotlight.png"), "This is a spotlight. Unlike a lamp, it has a rotating beam. Lights can be multicoloured, like this one. You can toggle lights on and off at night to conserve power, and toggle the colour of multicoloured lights.", transform=ColourWedges(colours=("red", "green"))),
+            HelpItem(("32", "night.png"), "When you have finished planting seeds and placing lights, you can prepare for Boyd's night-time onslaught by clicking this button."),
+            HelpItem(("32", "pause.png"), "You can pause the game during the night if you need to make a more detailed analysis of why you're losing horribly."),
+            #HelpItem(("", ""), ""),
+            #HelpItem(("", ""), ""),
+        ]
+
+        # Special mould assembly
+        items[1]._img.blit(loader.load_image("32", "mouldB.png").convert_alpha(), (30, 30), None)
+        items[1]._img.blit(loader.load_image("32", "eyeballA.png").convert_alpha(), (10, 10), None)
+
+        return items
+
+    def render(self, surface, gamestate):
+        surface.fill(COLOURS["blue"])
+        
+        pos = ((surface.get_width() - self._title.get_width()) / 2, 5)
+        surface.blit(self._title, pos, None)
+
+        height = 50
+        x_offset = 0
+
+        for item in self._items:
+            item.render(surface, height, x_offset)
+            height += max(item._img_size, item.FONT.get_height() * len(item._text))
+            height += 5
+            if height > SCREEN_SIZE[1]:
+                height = 50
+                x_offset = SCREEN_SIZE[0] / 2
+
+    def event(self, ev, gamestate):
+        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.type == pgl.MOUSEBUTTONDOWN:
+            if ev.button == 1:
+                # Check tools
+                for tool in self._tools:
+                    if tool.pressed(ev):
+                        if tool.name == 'exit':
+                            from .menu import MenuScene
+                            SceneChangeEvent.post(scene=MenuScene())
index 9dc54c1ceb93a4d14dd81f11f0e81cb535c35b1b..dcad16b3d27057898a2f80e6d448b6b06ce24d38 100644 (file)
@@ -21,6 +21,7 @@ class MenuScene(BaseScene):
             TextButton("Load Level", (255, 255, 255), name='load level'),
             TextButton("Start Game (Day)", (255, 255, 255), name='start game'),
             TextButton("Load Saved Game", (255, 255, 255), name='load game'),
+            TextButton("Help", (255, 255, 255), name='help'),
         ]
 
     def render(self, surface, gamestate):
@@ -51,6 +52,10 @@ class MenuScene(BaseScene):
         from .load_level import LoadLevelScene
         SceneChangeEvent.post(scene=LoadLevelScene())
 
+    def _do_help(self):
+        from .help import HelpScene
+        SceneChangeEvent.post(scene=HelpScene())
+
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
@@ -62,11 +67,15 @@ class MenuScene(BaseScene):
                 self._do_day()
             elif ev.key == pgl.K_l:
                 self._do_load_level()
+            elif ev.key == pgl.K_h:
+                self._do_help()
         elif ev.type == pgl.MOUSEBUTTONDOWN:
             pressed = self._get_pressed(ev)
             if pressed == 'load level':
                 self._do_load_level()
             elif pressed == 'start game':
                 self._do_day()
+            elif pressed == 'help':
+                self._do_help()
             elif pressed:
                 print 'Pressed', pressed
index 692ecf1924b828ebcc094b1ba5afb289a992bdb5..115630ab65ef5e143e0266665b12d7e0af8570b9 100644 (file)
@@ -29,6 +29,7 @@ class NightScene(BaseScene):
         self._obstacles = ObstacleManager(self._space, gamestate)
         self._lights = LightManager(self._space, gamestate)
         self._battery = BatteryManager(gamestate)
+        self.check_battery()
         self._infobar = InfoBar("day", battery=self._battery, scene=self)
         self._mould = Boyd(gamestate, self._space)
         self._turnips = []
@@ -147,6 +148,10 @@ class NightScene(BaseScene):
             (shadowed_text("Press any key to continue", FONTS["sans"], 24),
              (350, 240)))
 
+    def check_battery(self):
+        if self._battery.current == 0:
+            self._lights.battery_dead()
+
     @debug_timer("night.tick")
     def tick(self, gamestate):
         if self._do_ticks and not self._paused:
@@ -156,6 +161,7 @@ class NightScene(BaseScene):
                 if self._total_ticks % 60 == 0:
                     self._battery.current -= int(
                         self._lights.total_power_usage())
+                    self.check_battery()
                 self._total_ticks += 1
             else:
                 self._end_night()