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