1 # Boyd, the friendly, misunderstood turnip loving, light hating space mould
6 import pymunk.pygame_util
11 import pygame.locals as pgl
13 from .constants import (SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY,
15 from .loader import loader
16 from .sound import sound
18 MOULD_FILTER = pymunk.ShapeFilter(
19 mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
20 categories=MOULD_CATEGORY)
22 EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
25 class Mould(pymunk.Body):
26 """A segment of Boyd"""
28 def __init__(self, gamestate, space, pos):
29 super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
31 self._shape = pymunk.Circle(self, 16)
32 space.add(self, self._shape)
33 self._shape.filter = MOULD_FILTER
34 self._resistances = {}
38 self.has_eyeball = False
41 def pygame_pos(self, surface):
42 """Convert to pygame coordinates and offset position so
43 our position is the centre of the image."""
44 # The odd sign combination is because of the pymunk / pygame
45 # transform, but we do it this way to exploit Vec2d math magic
46 return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
51 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
52 size = "16" if self._age < 10 else "32" if self._age < 20 else "64"
53 self._img = loader.load_image(size, name)
56 def get_eyeball(self):
59 ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
60 self._eyeball = loader.load_image("32", name)
63 def tick(self, gamestate, space, moulds):
64 """Grow and / or Die"""
68 # we regain a health every tick, so we heal in the dark
69 if self._health < 100:
74 if (self._age % 15) == 0 and len(moulds) < 1000:
75 # Spawn a new child, if we can
77 choice = random.randint(0, 4)
79 pos = self.position + (0, 24)
81 pos = self.position + (24, 0)
83 pos = self.position + (-24, 0)
85 pos = self.position + (0, -24)
87 if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
89 if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
91 # Check for free space
92 # We allow some overlap, hence not checking full radius
93 query = space.point_query(pos, 8, MOULD_FILTER)
96 # if not isinstance(x.shape.body, Mould):
97 # print x.shape, x.shape.body
100 child = Mould(gamestate, space, pos)
101 child._health = self._health
104 if random.randint(0, 10) < 2:
105 sound.play_sound("mouth_pop_2a.ogg")
107 if self._age in (10, 20):
110 self._img = None # invalidate cached image
112 if self._age > 20 and random.randint(0, 500) < 1:
113 # Maybe we grow an eyeball
114 self.has_eyeball = True
118 space.remove(self, self._shape)
122 # Check for turnips we can eat
123 # Note that we can only eat a tick after we spawn
124 query = space.point_query(self.position, 16, EAT_TURNIP_FILTER)
126 query[0].shape.body.turnip.eaten = True
129 def damage(self, light_color, intensity, space, moulds):
130 """Take damage for light, adjusted for resistances."""
132 if self._health <= 0 and self._age <= 120:
134 space.remove(self, self._shape)
142 def __init__(self, gamestate, space):
143 seed = Mould(gamestate, space, (350, 370))
144 self._moulds = [seed]
145 self._image = pygame.surface.Surface(SCREEN_SIZE)
146 self._image = self._image.convert_alpha(pygame.display.get_surface())
149 def _draw_moulds(self):
150 self._image.fill((0, 0, 0, 0))
151 for m in self._moulds:
152 self._image.blit(m.get_image(),
153 m.pygame_pos(self._image), None,
155 for m in self._moulds:
157 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
160 def tick(self, gamestate, space, lights):
162 # Handle spawn events
163 for mould in self._moulds[:]:
165 if mould.tick(gamestate, space, self._moulds):
168 lit_by = lights.light_query(mould._shape)
170 # Todo: extract colour and intensity from light
171 if mould.damage(None, None, space, self._moulds):
173 break # we only die once
177 def render(self, surface):
179 surface.blit(self._image, (0, 0), None, 0)