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 def check_turnips(space, pos, max_distance):
18 point_info = space.point_query_nearest(
19 pos, max_distance, pymunk.ShapeFilter(mask=TURNIP_CATEGORY))
20 if point_info is not None:
25 class TurnipInvalidPosition(Exception):
31 def __init__(self, **kwargs):
32 self._age = kwargs.get('age', 0)
33 self._pos = kwargs.get('pos', (0, 0))
34 space = kwargs.get('space', None)
35 self._rotation = 90 * (hash(self._pos) % 4)
37 self.eaten = False # Flag for boyd
38 self._body = pymunk.Body(0, 0, pymunk.Body.STATIC)
39 self._shape = pymunk.Circle(self._body, 24)
40 self._shape.filter = TURNIP_FILTER
41 self._body.position = pymunk.pygame_util.from_pygame(
42 self._pos, pygame.display.get_surface())
43 # Add a reference so space query can get the object
44 self._body.turnip = self
45 if space.shape_query(self._shape):
46 raise TurnipInvalidPosition()
47 space.add(self._body, self._shape)
49 def _update_image(self):
50 self._image = loader.load_image('48', 'turnip%d.png' % (self._age + 1))
51 self._image = pygame.transform.rotate(self._image, self._rotation)
53 def render(self, surface):
54 surface.blit(self._image, self._pos, None)
57 return {'age': self._age, 'pos': self._pos}
60 self._body.turnip = None
61 space = self._body.space
62 space.remove(self._body, self._shape)
67 # Mature, so harvest it for seeds
69 return random.randint(1, 3)