Merge branch 'master' of ctpug.org.za:koperkapel
[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     "brown": (170, 68, 0, 255),
14 }
15
16
17 class RoachActor(SurfActor):
18     def __init__(self, frames):
19         self._frames = frames
20         self._frame = random.randint(0, len(frames) - 1)
21         self._dt = 0
22         self._cycle_dt = 0.2
23         each_tick(self.update)
24         super().__init__(surf=frames[self._frame])
25
26     def update(self, dt):
27         self._dt += dt
28         while self._dt > self._cycle_dt:
29             self._dt -= self._cycle_dt
30             self._frame += 1
31             self._frame %= len(self._frames)
32         self.surf = self._frames[self._frame]
33
34
35 class RoachFactory:
36
37     def __init__(self, suffix, frames=4):
38         self.suffix = suffix
39         self.frames = 4
40
41     def roach_color(self, roach):
42         if roach.smart:
43             return ROACH_COLORS["blue"]
44         elif roach.fast:
45             return ROACH_COLORS["green"]
46         elif roach.strong:
47             return ROACH_COLORS["purple"]
48         return ROACH_COLORS["brown"]
49
50     def assemble_frame(self, i, color, roach_data):
51         roach = images.load("roach%s/roach_%d" % (self.suffix, i + 1))
52         eyes = images.load("roach%s/eyes_%d" % (self.suffix, i + 1))
53         roach = roach.copy()
54         roach.fill(color, None, BLEND_RGBA_MULT)
55         roach.blit(eyes, (0, 0))
56         return roach
57
58     def assemble(self, roach_data):
59         color = self.roach_color(roach_data)
60         frames = [
61             self.assemble_frame(i, color, roach_data)
62             for i in range(self.frames)]
63         return RoachActor(frames)
64
65
66 default_roaches = RoachFactory("")
67 t32_roaches = RoachFactory("_32")
68 t21_roaches = RoachFactory("_21")
69 big_roaches = RoachFactory("_big")