9efc7a9d6523588623f3f75755c976778b139260
[e-sieve] / scale_sieve / ScaleSieve.py
1 from random import randint, randrange, choice
2
3 from pygame import Surface, PixelArray, Rect
4 from pygame.draw import aalines, polygon
5 from pygame.locals import *
6
7 from scale_sieve.pgfw.Game import Game
8 from scale_sieve.pgfw.GameChild import GameChild
9 from scale_sieve.pgfw.Sprite import Sprite
10
11 class ScaleSieve(Game):
12
13 def __init__(self):
14 Game.__init__(self)
15 self.background = Surface(self.display.screen.get_size())
16 self.background.fill((255, 80, 190))
17
18 def set_children(self):
19 Game.set_children(self)
20 self.acid = Acid(self)
21 self.sieve = Sieve(self)
22 self.triangles = Triangles(self)
23
24 def update(self):
25 self.display.screen.blit(self.background, (0, 0))
26 self.triangles.update()
27 self.sieve.update()
28
29
30 class Strip(Sprite):
31
32 LEFT, RIGHT = range(2)
33
34 def __init__(self, parent):
35 Sprite.__init__(self, parent)
36 self.display_surface = self.get_display_surface()
37 self.delegate = self.get_game().delegate
38 self.hshifts = Shift(self, -1, "shift-2"), Shift(self, 1, "shift-2")
39 self.add_frames()
40 self.subscribe(self.respond)
41
42 def add_frames(self):
43 pass
44
45 def respond(self, event):
46 compare = self.delegate.compare
47 if compare(event, "left") or compare(event, "left", True):
48 self.hshifts[self.LEFT].active = not event.cancel
49 elif compare(event, "right") or compare(event, "right", True):
50 self.hshifts[self.RIGHT].active = not event.cancel
51
52 def update(self):
53 for shift in self.hshifts:
54 shift.update()
55 if shift.time:
56 self.move(shift.get_change())
57 Sprite.update(self)
58
59
60 class Shift(GameChild):
61
62 def __init__(self, parent, direction, nodeset):
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(nodeset)
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_change(self):
80 return self.nodeset.get_y(self.time) * self.direction
81
82
83 class Sieve(Strip):
84
85 UP, DOWN = range(2)
86
87 def __init__(self, parent):
88 Strip.__init__(self, parent)
89 self.delegate = self.get_game().delegate
90 self.location.center = self.display_surface.get_rect().center
91 self.vshifts = Shift(self, -1, "shift"), Shift(self, 1, "shift")
92 self.electric = Electric(self)
93 self.add_location(offset=(self.location.w, 0))
94
95 def add_frames(self):
96 bar_locations = []
97 self.bar_rects = bar_rects = []
98 x = 0
99 sh = 30
100 nodeset = self.get_game().interpolator.get_nodeset("scale")
101 self.bar_w = bar_w = 3
102 self.gaps = gaps = []
103 while x < nodeset[-1].x:
104 bar_locations.append(x)
105 bar_rects.append(Rect(x, 0, bar_w, sh))
106 gaps.append(nodeset.get_y(x, natural=True))
107 x += gaps[-1]
108 surface = Surface((x, sh))
109 transparent_color = (255, 0, 255)
110 surface.fill(transparent_color)
111 surface.set_colorkey(transparent_color)
112 frames = surface, surface.copy()
113 colors = (0, 255, 0), (153, 0, 204)
114 for x in bar_locations:
115 bar_rects.append(Rect(x + surface.get_width(), 0, bar_w, sh))
116 for ii, frame in enumerate(frames):
117 frame.fill(colors[ii], (x, 0, bar_w, sh))
118 frame.fill(colors[ii - 1], (x + 1, 1, 1, sh - 2))
119 for frame in frames:
120 self.add_frame(frame)
121
122 def update(self):
123 if self.location.right < 0:
124 self.move(self.location.w)
125 if self.locations[1].left > self.display_surface.get_width():
126 self.move(-self.location.w)
127 for location in self.locations:
128 location.bottom = self.parent.acid.get_top()
129 self.electric.location.centery = self.location.centery + 13
130 self.electric.update()
131 for rect in self.bar_rects:
132 rect.centery = self.location.centery
133 Strip.update(self)
134
135
136 class Electric(Sprite):
137
138 def __init__(self, parent):
139 Sprite.__init__(self, parent)
140 self.display_surface = self.get_display_surface()
141 self.add_frames()
142
143 def add_frames(self):
144 surface = Surface((self.display_surface.get_width(),
145 self.parent.location.h - 10))
146 frames = surface, surface.copy()
147 colors = (255, 255, 0), (100, 89, 213)
148 pixel_arrays = PixelArray(frames[0]), PixelArray(frames[1])
149 for x in xrange(len(pixel_arrays[0])):
150 for y in xrange( len(pixel_arrays[0][0])):
151 pixel_arrays[0][x][y] = colors[(y + x) % 2]
152 pixel_arrays[1][x][y] = colors[(y + x + 1) % 2]
153 for pixels in pixel_arrays:
154 del pixels
155 for frame in frames:
156 self.add_frame(frame)
157
158
159 class Triangles(GameChild, list):
160
161 def __init__(self, parent):
162 GameChild.__init__(self, parent)
163 list.__init__(self, [])
164 self.display_surface = self.get_display_surface()
165 self.populate()
166
167 def populate(self):
168 if not self:
169 self.append(Triangle(self))
170 self[-1].location.bottom = 0
171 self.set_next_gap()
172 while self[-1].location.top > -self.display_surface.get_height():
173 self.append(Triangle(self))
174 self[-1].location.bottom = self[-2].location.top - self.next_gap
175 self.set_next_gap()
176
177 def set_next_gap(self):
178 self.next_gap = randint(210, 380)
179
180 def update(self):
181 self.populate()
182 if self[0].location.collidelist(self.parent.sieve.locations) != -1:
183 sieve = self.parent.sieve
184 if self[0].location.colliderect(sieve.electric.location):
185 self.parent.acid.increase()
186 self.remove(self[0])
187 else:
188 for br in sieve.bar_rects:
189 for tr in self[0].collision_rects:
190 if tr.move((self[0].location.left,
191 0)).colliderect(br.move((sieve.location.left,
192 0))):
193 self.remove(self[0])
194 break
195 for triangle in self:
196 triangle.update()
197
198
199 class Triangle(Sprite):
200
201 def __init__(self, parent):
202 Sprite.__init__(self, parent)
203 mark = randint(96, 328)
204 sieve = self.parent.parent.sieve
205 gaps = sieve.gaps
206 start = randrange(0, len(gaps))
207 widths = [gaps[start]]
208 while sum(widths) < mark:
209 widths.append(gaps[(start + len(widths)) % len(gaps)])
210 surface = Surface((sum(widths), 20))
211 surface.set_colorkey((0, 0, 0))
212 x = 0
213 height = surface.get_height()
214 margin = 26
215 self.collision_rects = collision_rects = []
216 for width in widths:
217 x += sieve.bar_w
218 points = (x + margin / 2, height - 2), \
219 (x + width - margin / 2 - 1, height - 2), \
220 (x + width / 2.0, 1)
221 polygon(surface, (60, 255, 220), points)
222 collision_rects.append(Rect(points[0], (width - margin - 1, 1)))
223 x += width - sieve.bar_w
224 self.add_frame(surface)
225 self.location.centerx = self.get_display_surface().get_rect().centerx
226
227 def update(self):
228 self.move(dy=2.45 * self.get_game().acid.get_volume() + 1.6)
229 for rect in self.collision_rects:
230 rect.bottom = self.location.bottom
231 Sprite.update(self)
232
233
234 class Acid(GameChild):
235
236 def __init__(self, parent):
237 GameChild.__init__(self, parent)
238 self.display_surface = self.get_display_surface()
239 self.level_r = 80, 320
240 self.substance = 0
241 self.nodeset = self.get_game().interpolator.get_nodeset("volume")
242
243 def get_top(self):
244 return self.display_surface.get_height() - self.get_level()
245
246 def get_level(self):
247 return self.get_volume() * (self.level_r[1] - self.level_r[0]) + \
248 self.level_r[0]
249
250 def get_volume(self):
251 return self.nodeset.get_y(self.substance)
252
253 def increase(self):
254 self.substance += 1