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
14 from .loader import loader
15 from .sound import sound
17 MOULD_FILTER = pymunk.ShapeFilter(
18 mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
19 categories=MOULD_CATEGORY)
22 class Mould(pymunk.Body):
23 """A segment of Boyd"""
25 def __init__(self, gamestate, space, pos):
26 super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
28 self._shape = pymunk.Circle(self, 16)
29 space.add(self, self._shape)
30 self._shape.filter = MOULD_FILTER
31 self._resistances = {}
35 self.has_eyeball = False
38 def pygame_pos(self, surface):
39 """Convert to pygame coordinates and offset position so
40 our position is the centre of the image."""
41 # The odd sign combination is because of the pymunk / pygame
42 # transform, but we do it this way to exploit Vec2d math magic
43 return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
48 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
49 size = "16" if self._age < 10 else "32" if self._age < 20 else "64"
50 self._img = loader.load_image(size, name)
53 def get_eyeball(self):
55 self._eyeball = loader.load_image("32", "eyeball.png")
58 def tick(self, gamestate, space, moulds):
59 """Grow and / or Die"""
63 # we regain a health every tick, so we heal in the dark
64 if self._health < 100:
69 if (self._age % 15) == 0 and len(moulds) < 1000:
70 # Spawn a new child, if we can
72 choice = random.randint(0, 4)
74 pos = self.position + (0, 24)
76 pos = self.position + (24, 0)
78 pos = self.position + (-24, 0)
80 pos = self.position + (0, -24)
82 if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
84 if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
86 # Check for free space
87 # We allow some overlap, hence not checking full radius
88 query = space.point_query(pos, 8, MOULD_FILTER)
91 # if not isinstance(x.shape.body, Mould):
92 # print x.shape, x.shape.body
95 child = Mould(gamestate, space, pos)
96 child._health = self._health
99 if random.randint(0, 10) < 2:
100 sound.play_sound("mouth_pop_2a.ogg")
102 if self._age in (10, 20):
105 self._img = None # invalidate cached image
107 if self._age > 20 and random.randint(0, 100) < 1:
108 # Maybe we grow an eyeball
109 self.has_eyeball = True
113 space.remove(self, self._shape)
118 def damage(self, light_color, intensity, space, moulds):
119 """Take damage for light, adjusted for resistances."""
121 if self._health <= 0 and self._age <= 120:
123 space.remove(self, self._shape)
131 def __init__(self, gamestate, space):
132 seed = Mould(gamestate, space, (350, 370))
133 self._moulds = [seed]
134 self._image = pygame.surface.Surface(SCREEN_SIZE)
135 self._image.convert_alpha(pygame.display.get_surface())
138 def _draw_moulds(self):
139 self._image.fill((0, 0, 0, 0))
140 for m in self._moulds:
141 self._image.blit(m.get_image(),
142 m.pygame_pos(self._image), None,
144 for m in self._moulds:
146 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
149 def tick(self, gamestate, space, lights):
151 # Handle spawn events
152 for mould in self._moulds[:]:
154 if mould.tick(gamestate, space, self._moulds):
157 lit_by = lights.light_query(mould._shape)
159 # Todo: extract colour and intensity from light
160 if mould.damage(None, None, space, self._moulds):
162 break # we only die once
166 def render(self, surface):
168 surface.blit(self._image, (0, 0), None, pgl.BLEND_RGBA_ADD)