1 # Brassica rapa extraterrestrialus, the common space turnip
4 import pymunk.pygame_util
6 import pygame.transform
8 from .loader import loader
9 from .constants import TURNIP_CATEGORY, LIGHT_CATEGORY
12 TURNIP_FILTER = pymunk.ShapeFilter(
13 mask=pymunk.ShapeFilter.ALL_MASKS ^ LIGHT_CATEGORY,
14 categories=TURNIP_CATEGORY)
17 class TurnipInvalidPosition(Exception):
23 def __init__(self, **kwargs):
24 self._age = kwargs.get('age', 0)
25 self._pos = kwargs.get('pos', (0, 0))
26 space = kwargs.get('space', None)
27 self._rotation = 90 * (hash(self._pos) % 4)
29 self.eaten = False # Flag for boyd
30 self._body = pymunk.Body(0, 0, pymunk.Body.STATIC)
31 self._shape = pymunk.Circle(self._body, 24)
32 self._shape.filter = TURNIP_FILTER
33 self._body.position = pymunk.pygame_util.from_pygame(
34 self._pos, pygame.display.get_surface())
35 # Add a reference so space query can get the object
36 self._body.turnip = self
37 if space.shape_query(self._shape):
38 raise TurnipInvalidPosition()
39 space.add(self._body, self._shape)
41 def _update_image(self):
42 self._image = loader.load_image('48', 'turnip%d.png' % (self._age + 1))
43 self._image = pygame.transform.rotate(self._image, self._rotation)
45 def render(self, surface):
46 surface.blit(self._image, self._pos, None)
49 return {'age': self._age, 'pos': self._pos}
52 self._body.turnip = None
53 space = self._body.space
54 space.remove(self._body, self._shape)
59 # Mature, so harvest it for seeds
61 return random.randint(1, 3)