slide
[e-sieve] / scale_sieve / ScaleSieve.py
1 from pygame import Surface, PixelArray
2
3 from scale_sieve.pgfw.Game import Game
4 from scale_sieve.pgfw.GameChild import GameChild
5 from scale_sieve.pgfw.Sprite import Sprite
6
7 class ScaleSieve(Game):
8
9 def __init__(self):
10 Game.__init__(self)
11 self.background = Surface(self.display.screen.get_size())
12 self.background.fill((255, 80, 190))
13
14 def set_children(self):
15 Game.set_children(self)
16 self.sieve = Sieve(self)
17
18 def update(self):
19 self.display.screen.blit(self.background, (0, 0))
20 self.sieve.update()
21
22
23 class Strip(Sprite):
24
25 LEFT, RIGHT = range(2)
26
27 def __init__(self, parent):
28 Sprite.__init__(self, parent)
29 self.display_surface = self.get_display_surface()
30 self.delegate = self.get_game().delegate
31 self.shifts = Shift(self, 1), Shift(self, -1)
32 self.shift_nodeset = self.get_game().interpolator.get_nodeset("shift")
33 self.add_frames()
34 self.subscribe(self.respond)
35
36 def add_frames(self):
37 pass
38
39 def respond(self, event):
40 compare = self.delegate.compare
41 if compare(event, "left") or compare(event, "left", True):
42 self.shifts[self.LEFT].active = not event.cancel
43 elif compare(event, "right") or compare(event, "right", True):
44 self.shifts[self.RIGHT].active = not event.cancel
45
46 def update(self):
47 for shift in self.shifts:
48 shift.update()
49 if shift.time:
50 self.move(shift.get_dx())
51 Sprite.update(self)
52
53
54 class Shift(GameChild):
55
56 def __init__(self, parent, direction):
57 GameChild.__init__(self, parent)
58 self.direction = direction
59 self.active = False
60 self.time = 0
61 self.timer = self.get_game().time_filter
62 self.nodeset = self.get_game().interpolator.get_nodeset("shift")
63
64 def update(self):
65 least, greatest = self.nodeset[0].x, self.nodeset[-1].x
66 if self.active and self.time < greatest:
67 self.time = min(self.time + self.timer.get_last_frame_duration(),
68 greatest)
69 elif not self.active and self.time > least:
70 self.time = max(self.time - self.timer.get_last_frame_duration(),
71 least)
72
73 def get_dx(self):
74 return self.nodeset.get_y(self.time) * self.direction
75
76
77 class Sieve(Strip):
78
79 def __init__(self, parent):
80 Strip.__init__(self, parent)
81 self.location.center = self.display_surface.get_rect().center
82
83 def add_frames(self):
84 surface = Surface((1000, 30))
85 transparent_color = (255, 0, 255)
86 surface.fill(transparent_color)
87 surface.set_colorkey(transparent_color)
88 frames = surface, surface.copy()
89 background_colors = (255, 255, 0), (0, 0, 213)
90 divider_colors = (0, 255, 0), (153, 0, 204)
91 pixel_arrays = PixelArray(frames[0]), PixelArray(frames[1])
92 bw = 5
93 for x in xrange(len(pixel_arrays[0])):
94 for y in xrange(bw, len(pixel_arrays[0][0]) - bw):
95 pixel_arrays[0][x][y] = background_colors[(y + x) % 2]
96 pixel_arrays[1][x][y] = background_colors[(y + x + 1) % 2]
97 for pixels in pixel_arrays:
98 del pixels
99 x = 0
100 nodeset = self.get_game().interpolator.get_nodeset("scale")
101 while x < surface.get_width():
102 for ii, frame in enumerate(frames):
103 frame.fill(divider_colors[ii], (x, 0, 3, surface.get_height()))
104 frame.fill(divider_colors[ii - 1], (x + 1, 1, 1,
105 surface.get_height() - 2))
106 x += nodeset.get_y(x)
107 for frame in frames:
108 self.add_frame(frame)