85b60a8cf394a7ba9eb774e87d2084bdc8007139
[e-sieve] / scale_sieve / ScaleSieve.py
1 from random import randint, randrange, choice
2
3 from pygame import Surface, PixelArray
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 for location in self.locations:
95 location.bottom = self.parent.acid.ry[1]
96
97 def add_frames(self):
98 bar_locations = []
99 x = 0
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 gaps.append(nodeset.get_y(x, natural=True))
106 x += gaps[-1]
107 surface = Surface((x, 30))
108 transparent_color = (255, 0, 255)
109 surface.fill(transparent_color)
110 surface.set_colorkey(transparent_color)
111 frames = surface, surface.copy()
112 colors = (0, 255, 0), (153, 0, 204)
113 for x in bar_locations:
114 for ii, frame in enumerate(frames):
115 frame.fill(colors[ii], (x, 0, bar_w, surface.get_height()))
116 frame.fill(colors[ii - 1], (x + 1, 1, 1,
117 surface.get_height() - 2))
118 for frame in frames:
119 self.add_frame(frame)
120
121 def respond(self, event):
122 Strip.respond(self, event)
123 compare = self.delegate.compare
124 if compare(event, "up") or compare(event, "up", True):
125 self.vshifts[self.UP].active = not event.cancel
126 elif compare(event, "down") or compare(event, "down", True):
127 self.vshifts[self.DOWN].active = not event.cancel
128
129 def update(self):
130 if self.location.right < 0:
131 self.move(self.location.w)
132 if self.locations[1].left > self.display_surface.get_width():
133 self.move(-self.location.w)
134 for shift in self.vshifts:
135 shift.update()
136 if shift.time:
137 self.move(dy=shift.get_change())
138 acid = self.parent.acid
139 if self.location.bottom < acid.ry[0]:
140 self.vshifts[self.UP].time = 0
141 for location in self.locations:
142 location.top = acid.ry[0]
143 elif self.location.bottom > acid.get_level():
144 self.vshifts[self.DOWN].time = 0
145 for location in self.locations:
146 location.bottom = acid.get_level()
147 self.electric.location.centery = self.location.centery + 13
148 self.electric.update()
149 Strip.update(self)
150
151
152 class Electric(Sprite):
153
154 def __init__(self, parent):
155 Sprite.__init__(self, parent)
156 self.display_surface = self.get_display_surface()
157 self.add_frames()
158
159 def add_frames(self):
160 surface = Surface((self.display_surface.get_width(),
161 self.parent.location.h - 10))
162 frames = surface, surface.copy()
163 colors = (255, 255, 0), (100, 89, 213)
164 pixel_arrays = PixelArray(frames[0]), PixelArray(frames[1])
165 for x in xrange(len(pixel_arrays[0])):
166 for y in xrange( len(pixel_arrays[0][0])):
167 pixel_arrays[0][x][y] = colors[(y + x) % 2]
168 pixel_arrays[1][x][y] = colors[(y + x + 1) % 2]
169 for pixels in pixel_arrays:
170 del pixels
171 for frame in frames:
172 self.add_frame(frame)
173
174
175 class Triangles(GameChild, list):
176
177 def __init__(self, parent):
178 GameChild.__init__(self, parent)
179 list.__init__(self, [])
180 self.display_surface = self.get_display_surface()
181 self.populate()
182
183 def populate(self):
184 if not self:
185 self.append(Triangle(self))
186 self[-1].location.bottom = 0
187 self.set_next_gap()
188 while self[-1].location.top > -self.display_surface.get_height():
189 self.append(Triangle(self))
190 self[-1].location.bottom = self[-2].location.top - self.next_gap
191 self.set_next_gap()
192
193 def set_next_gap(self):
194 self.next_gap = randint(128, 382)
195
196 def update(self):
197 self.populate()
198 if self[0].location.collidelist(self.parent.sieve.locations) != -1:
199 self.remove(self[0])
200 for triangle in self:
201 triangle.update()
202
203
204 class Triangle(Sprite):
205
206 def __init__(self, parent):
207 Sprite.__init__(self, parent)
208 mark = randint(72, 296)
209 sieve = self.parent.parent.sieve
210 gaps = sieve.gaps
211 start = randrange(0, len(gaps))
212 widths = [gaps[start]]
213 while sum(widths) < mark:
214 widths.append(gaps[(start + len(widths)) % len(gaps)])
215 surface = Surface((sum(widths), 20))
216 surface.set_colorkey((0, 0, 0))
217 x = 0
218 height = surface.get_height()
219 margin = 12
220 for width in widths:
221 x += sieve.bar_w
222 points = (x + margin / 2, height - 2), \
223 (x + width - margin / 2 - 1, height - 2), \
224 (x + width / 2.0, 1)
225 polygon(surface, (60, 255, 220), points)
226 x += width - sieve.bar_w
227 self.add_frame(surface)
228 self.location.centerx = self.get_display_surface().get_rect().centerx
229
230 def update(self):
231 acid = self.get_game().acid
232 ratio = 1 - (self.get_game().sieve.location.bottom - acid.ry[0]) / \
233 float(acid.ry[1] - acid.ry[0])
234 self.move(dy=3 * ratio + .5)
235 Sprite.update(self)
236
237
238 class Acid(GameChild):
239
240 def __init__(self, parent):
241 GameChild.__init__(self, parent)
242 self.ry = 160, 640
243 self.space = 1
244
245 def get_level(self):
246 return self.space * (self.ry[1] - self.ry[0]) + self.ry[0]