fall
[e-sieve] / scale_sieve / ScaleSieve.py
1 from random import randint
2
3 from pygame import Surface, PixelArray
4
5 from scale_sieve.pgfw.Game import Game
6 from scale_sieve.pgfw.GameChild import GameChild
7 from scale_sieve.pgfw.Sprite import Sprite
8
9 class ScaleSieve(Game):
10
11 def __init__(self):
12 Game.__init__(self)
13 self.background = Surface(self.display.screen.get_size())
14 self.background.fill((255, 80, 190))
15
16 def set_children(self):
17 Game.set_children(self)
18 self.sieve = Sieve(self)
19 self.triangles = Triangles(self)
20
21 def update(self):
22 self.display.screen.blit(self.background, (0, 0))
23 self.triangles.update()
24 self.sieve.update()
25
26
27 class Strip(Sprite):
28
29 LEFT, RIGHT = range(2)
30
31 def __init__(self, parent):
32 Sprite.__init__(self, parent)
33 self.display_surface = self.get_display_surface()
34 self.delegate = self.get_game().delegate
35 self.shifts = Shift(self, 1), Shift(self, -1)
36 self.shift_nodeset = self.get_game().interpolator.get_nodeset("shift")
37 self.add_frames()
38 self.subscribe(self.respond)
39
40 def add_frames(self):
41 pass
42
43 def respond(self, event):
44 compare = self.delegate.compare
45 if compare(event, "left") or compare(event, "left", True):
46 self.shifts[self.LEFT].active = not event.cancel
47 elif compare(event, "right") or compare(event, "right", True):
48 self.shifts[self.RIGHT].active = not event.cancel
49
50 def update(self):
51 for shift in self.shifts:
52 shift.update()
53 if shift.time:
54 self.move(shift.get_dx())
55 # if self.location.left > 0:
56 # self.location.left = 0
57 # self.shifts[self.LEFT].time = 0
58 # elif self.location.right < self.display_surface.get_width():
59 # self.location.right = self.display_surface.get_width()
60 # self.shifts[self.RIGHT].time = 0
61 Sprite.update(self)
62
63
64 class Shift(GameChild):
65
66 def __init__(self, parent, direction):
67 GameChild.__init__(self, parent)
68 self.direction = direction
69 self.active = False
70 self.time = 0
71 self.timer = self.get_game().time_filter
72 self.nodeset = self.get_game().interpolator.get_nodeset("shift-2")
73
74 def update(self):
75 least, greatest = self.nodeset[0].x, self.nodeset[-1].x
76 if self.active and self.time < greatest:
77 self.time = min(self.time + self.timer.get_last_frame_duration(),
78 greatest)
79 elif not self.active and self.time > least:
80 self.time = max(self.time - self.timer.get_last_frame_duration(),
81 least)
82
83 def get_dx(self):
84 return self.nodeset.get_y(self.time) * self.direction
85
86
87 class Sieve(Strip):
88
89 def __init__(self, parent):
90 Strip.__init__(self, parent)
91 self.location.center = self.display_surface.get_rect().center
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 bar_w = 3
100 self.gaps = gaps = []
101 while x < nodeset[-1].x:
102 bar_locations.append(x)
103 step = nodeset.get_y(x, natural=True)
104 gaps.append(step - bar_w)
105 x += step
106 surface = Surface((x, 30))
107 transparent_color = (255, 0, 255)
108 surface.fill(transparent_color)
109 surface.set_colorkey(transparent_color)
110 frames = surface, surface.copy()
111 colors = (0, 255, 0), (153, 0, 204)
112 for x in bar_locations:
113 for ii, frame in enumerate(frames):
114 frame.fill(colors[ii], (x, 0, bar_w, surface.get_height()))
115 frame.fill(colors[ii - 1], (x + 1, 1, 1,
116 surface.get_height() - 2))
117 for frame in frames:
118 self.add_frame(frame)
119
120 def update(self):
121 if self.location.left > 0:
122 self.move(-self.location.w)
123 if self.locations[1].right < self.display_surface.get_width():
124 self.move(self.location.w)
125 self.electric.location.centery = self.location.centery
126 self.electric.update()
127 Strip.update(self)
128
129
130 class Electric(Sprite):
131
132 def __init__(self, parent):
133 Sprite.__init__(self, parent)
134 self.display_surface = self.get_display_surface()
135 self.add_frames()
136
137 def add_frames(self):
138 surface = Surface((self.display_surface.get_width(),
139 self.parent.location.h - 10))
140 frames = surface, surface.copy()
141 colors = (255, 255, 0), (100, 89, 213)
142 pixel_arrays = PixelArray(frames[0]), PixelArray(frames[1])
143 for x in xrange(len(pixel_arrays[0])):
144 for y in xrange( len(pixel_arrays[0][0])):
145 pixel_arrays[0][x][y] = colors[(y + x) % 2]
146 pixel_arrays[1][x][y] = colors[(y + x + 1) % 2]
147 for pixels in pixel_arrays:
148 del pixels
149 for frame in frames:
150 self.add_frame(frame)
151
152
153 class Triangles(GameChild, list):
154
155 def __init__(self, parent):
156 GameChild.__init__(self, parent)
157 list.__init__(self, [])
158 self.set_next_gap()
159
160 def set_next_gap(self):
161 self.next_gap = randint(30, 56)
162
163 def update(self):
164 if not self or self[-1].location.top > self.next_gap:
165 self.append(Triangle(self))
166 self.set_next_gap()
167 if self[0].location.collidelist(self.parent.sieve.locations) != -1:
168 self.remove(self[0])
169 for triangle in self:
170 triangle.update()
171
172
173 class Triangle(Sprite):
174
175 def __init__(self, parent):
176 Sprite.__init__(self, parent)
177 surface = Surface((100, 20))
178 surface.fill((69, 178, 220))
179 self.add_frame(surface)
180
181 def update(self):
182 self.move(dy=8)
183 Sprite.update(self)