From e273ee03bc5d751bb9d22974a699ec4ed63cdb25 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 9 Sep 2016 12:25:17 +0200 Subject: [PATCH] Nom --- tabakrolletjie/enemies.py | 11 ++++++++++- tabakrolletjie/gamestate.py | 1 + tabakrolletjie/scenes/day.py | 12 +++++++----- tabakrolletjie/scenes/night.py | 9 +++++++-- tabakrolletjie/turnip.py | 9 +++++++-- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/tabakrolletjie/enemies.py b/tabakrolletjie/enemies.py index efd5447..65bae00 100644 --- a/tabakrolletjie/enemies.py +++ b/tabakrolletjie/enemies.py @@ -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): diff --git a/tabakrolletjie/gamestate.py b/tabakrolletjie/gamestate.py index ca7f961..091b709 100644 --- a/tabakrolletjie/gamestate.py +++ b/tabakrolletjie/gamestate.py @@ -10,6 +10,7 @@ class GameState(object): "turnips": [], } self.harvested = 0 + self.eaten = 0 @property def station(self): diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index a3e4347..c2dee94 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -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)) diff --git a/tabakrolletjie/scenes/night.py b/tabakrolletjie/scenes/night.py index 72e6c84..c532c77 100644 --- a/tabakrolletjie/scenes/night.py +++ b/tabakrolletjie/scenes/night.py @@ -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) diff --git a/tabakrolletjie/turnip.py b/tabakrolletjie/turnip.py index 27cb4de..5f4e8c5 100644 --- a/tabakrolletjie/turnip.py +++ b/tabakrolletjie/turnip.py @@ -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() -- 2.34.1