Nom
authorNeil <neil@dip.sun.ac.za>
Fri, 9 Sep 2016 10:25:17 +0000 (12:25 +0200)
committerNeil <neil@dip.sun.ac.za>
Fri, 9 Sep 2016 10:25:17 +0000 (12:25 +0200)
tabakrolletjie/enemies.py
tabakrolletjie/gamestate.py
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/night.py
tabakrolletjie/turnip.py

index efd544761ff5c9d704b4840d699fa44ef58c994f..65bae00ea4af6b79e5eeba8f76984f8435b8eb58 100644 (file)
@@ -10,7 +10,8 @@ import pygame.display
 
 import pygame.locals as pgl
 
-from .constants import SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY
+from .constants import (SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY,
+                        TURNIP_CATEGORY)
 from .loader import loader
 from .sound import sound
 
@@ -18,6 +19,8 @@ MOULD_FILTER = pymunk.ShapeFilter(
     mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
     categories=MOULD_CATEGORY)
 
+EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
+
 
 class Mould(pymunk.Body):
     """A segment of Boyd"""
@@ -115,6 +118,12 @@ class Mould(pymunk.Body):
             space.remove(self, self._shape)
             moulds.remove(self)
             refresh = True
+        else:
+            # Check for turnips we can eat
+            # Note that we can only eat a tick after we spawn
+            query = space.point_query(self.position, 16, EAT_TURNIP_FILTER)
+            if query:
+                query[0].shape.body.turnip.eaten = True
         return refresh
 
     def damage(self, light_color, intensity, space, moulds):
index ca7f961466a1b841bb78fce2a11047e65d8f6c32..091b7093d419ac3024c4c52c35c6e984444fe47f 100644 (file)
@@ -10,6 +10,7 @@ class GameState(object):
             "turnips": [],
         }
         self.harvested = 0
+        self.eaten = 0
 
     @property
     def station(self):
index a3e43475d72e63d36edde6cf4997a109cd0b1bcf..c2dee9488f6379188abb3e045edccaad36de5c9a 100644 (file)
@@ -44,7 +44,7 @@ class DayScene(BaseScene):
             ImageButton('32', 'default_cursor.png', name='reset tool',
                         pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
         ]
-        self._update_toolbar()
+        self._update_toolbar(gamestate)
 
     def exit(self, gamestate):
         self._unset_cursor()
@@ -100,7 +100,7 @@ class DayScene(BaseScene):
                             turnip = Turnip(age=0, pos=pos, space=self._space)
                             self._turnips.append(turnip)
                             self._seeds -= 1
-                            self._update_toolbar()
+                            self._update_toolbar(gamestate)
                         except TurnipInvalidPosition as e:
                             # TODO: Add error sound or something
                             pass
@@ -114,7 +114,9 @@ class DayScene(BaseScene):
         if not self._paused:
             self._lights.tick()
 
-    def _update_toolbar(self):
-        text = "Turnip Stocks: Seeds: %d. Planted: %d. Harvested: %d" % (
-            self._seeds, len(self._turnips), self._harvested)
+    def _update_toolbar(self, gamestate):
+        text = ("Turnip Stocks: Seeds: %d. Planted: %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 72e6c84167f1f4c52d0be41b6b1134a178196ab6..c532c77159c623f9cedb0a8000f69ef72da1cd16 100644 (file)
@@ -28,8 +28,13 @@ class NightScene(BaseScene):
     def render(self, surface, gamestate):
         surface.fill((0, 0, 155))
         self._mould.render(surface)
-        for turnip in self._turnips:
-            turnip.render(surface)
+        for turnip in self._turnips[:]:
+            if turnip.eaten:
+                self._turnips.remove(turnip)
+                turnip.remove()
+                gamestate.eaten += 1
+            else:
+                turnip.render(surface)
         self._lights.render_light(surface)
         self._obstacles.render(surface)
         self._lights.render_fittings(surface)
index 27cb4de9b20c3f3af8da42fe24b347c051fcb8ec..5f4e8c52f2273651f349281acbae1b42f6e8284e 100644 (file)
@@ -24,11 +24,14 @@ class Turnip(object):
         self._pos = kwargs.get('pos', (0, 0))
         space = kwargs.get('space', None)
         self._update_image()
+        self.eaten = False  # Flag for boyd
         self._body = pymunk.Body(0, 0, pymunk.Body.STATIC)
         self._shape = pymunk.Circle(self._body, 16)
         self._shape.filter = TURNIP_FILTER
         self._body.position = pymunk.pygame_util.from_pygame(
             self._pos, pygame.display.get_surface())
+        # Add a reference so space query can get the object
+        self._body.turnip = self
         if space.shape_query(self._shape):
             raise TurnipInvalidPosition()
         space.add(self._body, self._shape)
@@ -43,13 +46,15 @@ class Turnip(object):
         return {'age': self._age, 'pos': self._pos}
 
     def remove(self):
-        # FIXME: Remove body from the space
-        pass
+        self._body.turnip = None
+        space = self._body.space
+        space.remove(self._body, self._shape)
 
     def grow(self):
         self._age += 1
         if self._age >= 3:
             # Mature, so harvest it for seeds
+            self.remove()
             return random.randint(1, 3)
         else:
             self._update_image()