Give roaches more colours and fewer legs.
[koperkapel.git] / koperkapel / roaches.py
1 """ Tools for creating roach actors. """
2
3 import random
4 from pgzero.clock import each_tick
5 from pgzero.loaders import images
6 from pygame.constants import BLEND_RGBA_MULT
7 from .actors.surf import SurfActor
8
9 ROACH_COLORS = {
10     "blue": (0, 0, 255, 255),
11     "green": (0, 255, 0, 255),
12     "purple": (255, 0, 255, 255),
13 }
14
15
16 class RoachActor(SurfActor):
17     def __init__(self, frames):
18         self._frames = frames
19         self._frame = random.randint(0, len(frames) - 1)
20         self._dt = 0
21         self._cycle_dt = 0.2
22         each_tick(self.update)
23         super().__init__(surf=frames[self._frame])
24
25     def update(self, dt):
26         self._dt += dt
27         while self._dt > self._cycle_dt:
28             self._dt -= self._cycle_dt
29             self._frame += 1
30             self._frame %= len(self._frames)
31         self.surf = self._frames[self._frame]
32
33
34 class RoachFactory:
35
36     def __init__(self, suffix, frames=4):
37         self.suffix = suffix
38         self.frames = 4
39
40     def roach_color(self, roach_data):
41         return random.choice(list(ROACH_COLORS.values()))
42
43     def assemble_frame(self, i, color, roach_data):
44         roach = images.load("roach%s/roach_%d" % (self.suffix, i + 1))
45         eyes = images.load("roach%s/eyes_%d" % (self.suffix, i + 1))
46         roach = roach.copy()
47         roach.fill(color, None, BLEND_RGBA_MULT)
48         roach.blit(eyes, (0, 0))
49         return roach
50
51     def assemble(self, roach_data):
52         color = self.roach_color(roach_data)
53         frames = [
54             self.assemble_frame(i, color, roach_data)
55             for i in range(self.frames)]
56         return RoachActor(frames)
57
58
59 default_roaches = RoachFactory("")
60 t32_roaches = RoachFactory("_32")
61 t21_roaches = RoachFactory("_21")
62 big_roaches = RoachFactory("_big")