7f7f5d4b245d51b29648e4a899d59a0bd50843bb
[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 # if self.location.left > 0:
52 # self.location.left = 0
53 # self.shifts[self.LEFT].time = 0
54 # elif self.location.right < self.display_surface.get_width():
55 # self.location.right = self.display_surface.get_width()
56 # self.shifts[self.RIGHT].time = 0
57 Sprite.update(self)
58
59
60 class Shift(GameChild):
61
62 def __init__(self, parent, direction):
63 GameChild.__init__(self, parent)
64 self.direction = direction
65 self.active = False
66 self.time = 0
67 self.timer = self.get_game().time_filter
68 self.nodeset = self.get_game().interpolator.get_nodeset("shift-2")
69
70 def update(self):
71 least, greatest = self.nodeset[0].x, self.nodeset[-1].x
72 if self.active and self.time < greatest:
73 self.time = min(self.time + self.timer.get_last_frame_duration(),
74 greatest)
75 elif not self.active and self.time > least:
76 self.time = max(self.time - self.timer.get_last_frame_duration(),
77 least)
78
79 def get_dx(self):
80 return self.nodeset.get_y(self.time) * self.direction
81
82
83 class Sieve(Strip):
84
85 def __init__(self, parent):
86 Strip.__init__(self, parent)
87 self.location.center = self.display_surface.get_rect().center
88 self.electric = Electric(self)
89 self.add_location(offset=(self.location.w, 0))
90
91 def add_frames(self):
92 bar_locations = []
93 x = 0
94 nodeset = self.get_game().interpolator.get_nodeset("scale")
95 while x < nodeset[-1].x:
96 bar_locations.append(x)
97 x += nodeset.get_y(x, natural=True)
98 surface = Surface((x, 30))
99 transparent_color = (255, 0, 255)
100 surface.fill(transparent_color)
101 surface.set_colorkey(transparent_color)
102 frames = surface, surface.copy()
103 colors = (0, 255, 0), (153, 0, 204)
104 for x in bar_locations:
105 for ii, frame in enumerate(frames):
106 frame.fill(colors[ii], (x, 0, 3, surface.get_height()))
107 frame.fill(colors[ii - 1], (x + 1, 1, 1,
108 surface.get_height() - 2))
109 for frame in frames:
110 self.add_frame(frame)
111
112 def update(self):
113 if self.location.left > 0:
114 self.move(-self.location.w)
115 if self.locations[1].right < self.display_surface.get_width():
116 self.move(self.location.w)
117 self.electric.location.centery = self.location.centery
118 self.electric.update()
119 Strip.update(self)
120
121
122 class Electric(Sprite):
123
124 def __init__(self, parent):
125 Sprite.__init__(self, parent)
126 self.display_surface = self.get_display_surface()
127 self.add_frames()
128
129 def add_frames(self):
130 surface = Surface((self.display_surface.get_width(),
131 self.parent.location.h - 10))
132 frames = surface, surface.copy()
133 colors = (255, 255, 0), (100, 89, 213)
134 pixel_arrays = PixelArray(frames[0]), PixelArray(frames[1])
135 for x in xrange(len(pixel_arrays[0])):
136 for y in xrange( len(pixel_arrays[0][0])):
137 pixel_arrays[0][x][y] = colors[(y + x) % 2]
138 pixel_arrays[1][x][y] = colors[(y + x + 1) % 2]
139 for pixels in pixel_arrays:
140 del pixels
141 for frame in frames:
142 self.add_frame(frame)
143
144
145 class Triangles(GameChild, list):
146
147 def __init__(self, parent):
148 GameChild.__init__(self, parent)