boost
[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.delegate = self.get_game().delegate
166 self.booster = Shift(self, 1, "boost")
167 self.populate()
168 self.subscribe(self.respond)
169
170 def populate(self):
171 if not self:
172 self.append(Triangle(self))
173 self[-1].location.bottom = 0
174 self.set_next_gap()
175 while self[-1].location.top > -self.display_surface.get_height():
176 self.append(Triangle(self))
177 self[-1].location.bottom = self[-2].location.top - self.next_gap
178 self.set_next_gap()
179
180 def set_next_gap(self):
181 self.next_gap = randint(210, 380)
182
183 def respond(self, event):
184 compare = self.delegate.compare
185 if compare(event, "down") or compare(event, "down", True):
186 self.booster.active = not event.cancel
187
188 def get_boost(self):
189 return self.booster.get_change()
190
191 def update(self):
192 self.populate()
193 self.booster.update()
194 if self[0].location.collidelist(self.parent.sieve.locations) != -1:
195 sieve = self.parent.sieve
196 if self[0].location.colliderect(sieve.electric.location):
197 self.parent.acid.increase()
198 self.remove(self[0])
199 else:
200 for br in sieve.bar_rects:
201 for tr in self[0].collision_rects:
202 if tr.move((self[0].location.left,
203 0)).colliderect(br.move((sieve.location.left,
204 0))):
205 self.remove(self[0])
206 break
207 for triangle in self:
208 triangle.update()
209
210
211 class Triangle(Sprite):
212
213 def __init__(self, parent):
214 Sprite.__init__(self, parent)
215 mark = randint(96, 328)
216 sieve = self.parent.parent.sieve
217 gaps = sieve.gaps
218 start = randrange(0, len(gaps))
219 widths = [gaps[start]]
220 while sum(widths) < mark:
221 widths.append(gaps[(start + len(widths)) % len(gaps)])
222 surface = Surface((sum(widths), 20))
223 surface.set_colorkey((0, 0, 0))
224 x = 0
225 height = surface.get_height()
226 margin = 26
227 self.collision_rects = collision_rects = []
228 for width in widths:
229 x += sieve.bar_w
230 points = (x + margin / 2, height - 2), \
231 (x + width - margin / 2 - 1, height - 2), \
232 (x + width / 2.0, 1)
233 polygon(surface, (60, 255, 220), points)
234 collision_rects.append(Rect(points[0], (width - margin - 1, 1)))
235 x += width - sieve.bar_w
236 self.add_frame(surface)
237 self.location.centerx = self.get_display_surface().get_rect().centerx
238
239 def update(self):
240 self.move(dy=2.45 * self.get_game().acid.get_volume() + 1.6 + \
241 self.parent.get_boost())
242 for rect in self.collision_rects:
243 rect.bottom = self.location.bottom
244 Sprite.update(self)
245
246
247 class Acid(GameChild):
248
249 def __init__(self, parent):
250 GameChild.__init__(self, parent)
251 self.display_surface = self.get_display_surface()
252 self.level_r = 80, 320
253 self.substance = 0
254 self.nodeset = self.get_game().interpolator.get_nodeset("volume")
255
256 def get_top(self):
257 return self.display_surface.get_height() - self.get_level()
258
259 def get_level(self):
260 return self.get_volume() * (self.level_r[1] - self.level_r[0]) + \
261 self.level_r[0]
262
263 def get_volume(self):
264 return self.nodeset.get_y(self.substance)
265
266 def increase(self):
267 self.substance += 1