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
16 MOULD_FILTER = pymunk.ShapeFilter(
17 mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
18 categories=MOULD_CATEGORY)
21 class Mould(pymunk.Body):
22 """A segment of Boyd"""
24 def __init__(self, gamestate, space, pos):
25 super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
27 self._shape = pymunk.Circle(self, 16)
28 space.add(self, self._shape)
29 self._shape.filter = MOULD_FILTER
30 self._resistances = {}
35 def pygame_pos(self, surface):
36 """Convert to pygame coordinates and offset position so
37 our position is the centre of the image."""
38 # The odd sign combination is because of the pymunk / pygame
39 # transform, but we do it this way to exploit Vec2d math magic
40 return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
45 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
46 self._img = loader.load_image("32", name)
49 def tick(self, gamestate, space, moulds):
50 """Grow and / or Die"""
53 # we regain a health every tick, so we heal in the dark
54 if self._health < 100:
59 if (self._age % 15) == 0 and len(moulds) < 1000:
60 # Spawn a new child, if we can
62 choice = random.randint(0, 4)
64 pos = self.position + (0, 24)
66 pos = self.position + (24, 0)
68 pos = self.position + (-24, 0)
70 pos = self.position + (0, -24)
72 if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
74 if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
76 # Check for free space
77 # We allow some overlap, hence not checking full radius
78 query = space.point_query(pos, 8, MOULD_FILTER)
81 # if not isinstance(x.shape.body, Mould):
82 # print x.shape, x.shape.body
85 child = Mould(gamestate, space, pos)
86 child._health = self._health
92 space.remove(self, self._shape)
97 def damage(self, light_color, intensity, space, moulds):
98 """Take damage for light, adjusted for resistances."""
100 if self._health <= 0 and self._age <= 120:
102 space.remove(self, self._shape)
110 def __init__(self, gamestate, space):
111 seed = Mould(gamestate, space, (350, 370))
112 self._moulds = [seed]
113 self._image = pygame.surface.Surface(SCREEN_SIZE)
114 self._image.convert_alpha(pygame.display.get_surface())
115 self._image.fill((0, 0, 0, 0))
116 self._image.blit(seed.get_image(),
117 seed.pygame_pos(self._image), None,
120 def tick(self, gamestate, space, lights):
122 # Handle spawn events
123 for mould in self._moulds[:]:
125 if mould.tick(gamestate, space, self._moulds):
128 if lights.lit_by(mould.position):
129 if mould.damage(None, None, space, self._moulds):
132 self._image.fill((0, 0, 0, 0))
133 for mould in self._moulds:
134 self._image.blit(mould.get_image(),
135 mould.pygame_pos(self._image), None,
138 def render(self, surface):
140 surface.blit(self._image, (0, 0), None, pgl.BLEND_RGBA_ADD)