43d8d27a64f9a91028091be0d6116ad8ae0a2bbf
[sso] / shaken_and_spit_out / SSO.py
1 from os import listdir
2 from glob import glob
3 from random import choice
4 from collections import deque
5
6 from pygame import cursors, PixelArray, Surface, Color
7 from pygame.mouse import set_cursor
8 from pygame.font import Font
9 from pygame.image import load
10 from pygame.locals import *
11
12 from shaken_and_spit_out.pgfw.Game import Game
13 from shaken_and_spit_out.pgfw.GameChild import GameChild
14 from shaken_and_spit_out.pgfw.Sprite import Sprite
15
16 class SSO(Game):
17
18 def __init__(self):
19 Game.__init__(self)
20 set_cursor(*cursors.tri_left)
21 # string = u"\u2250\u2254\u2258"
22 # font = Font(self.get_resource("display", "font"), 24)
23 # self.message = font.render(string, True, (0, 255, 0))
24 self.backgrounds = [load(path).convert() for path in \
25 glob(self.get_resource("image", "background") + \
26 "*.png")]
27 self.used_backgrounds = []
28 self.set_random_background()
29 self.subscribe(self.respond)
30 self.title.activate()
31
32 def set_children(self):
33 Game.set_children(self)
34 self.title = Title(self)
35 self.floor = Floor(self)
36
37 def set_random_background(self):
38 index = choice(list(set(range(len(self.backgrounds))).\
39 difference(self.used_backgrounds)))
40 self.used_backgrounds.append(index)
41 if len(self.used_backgrounds) > 3:
42 self.used_backgrounds.pop(0)
43 self.background = self.backgrounds[index]
44
45 def respond(self, event):
46 if self.delegate.compare(event, "reset-game"):
47 self.set_random_background()
48
49 def update(self):
50 self.get_screen().blit(self.background, (0, 0))
51 self.title.update()
52 self.floor.update()
53 # self.get_screen().fill((0, 0, 0))
54 # self.get_screen().blit(self.message, (100, 100))
55
56
57 class Title(GameChild):
58
59 def __init__(self, parent):
60 GameChild.__init__(self, parent)
61 self.delegate = self.get_delegate()
62 self.preface = Preface(self)
63 self.subscribe(self.respond)
64 self.subscribe(self.respond, MOUSEBUTTONDOWN)
65 self.deactivate()
66
67 def respond(self, event):
68 if self.delegate.is_command(event) and \
69 self.delegate.compare(event, "reset-game"):
70 self.activate()
71 elif self.active:
72 if event.type == MOUSEBUTTONDOWN and event.button == 1:
73 print "left-click"
74
75 def deactivate(self):
76 self.active = True
77
78 def activate(self):
79 self.active = True
80 self.preface.reset()
81
82 def update(self):
83 if self.active:
84 self.preface.update()
85
86
87 class Preface(GameChild, Surface):
88
89 def __init__(self, parent):
90 GameChild.__init__(self, parent)
91 ds = self.ds = self.get_display_surface()
92 w, h = ds.get_width(), int(round(ds.get_width() / 3))
93 Surface.__init__(self, (w, h), SRCALPHA)
94 self.background = Surface((w, h))
95 self.background.fill((0, 0, 0))
96 mask = self.mask = Surface((w, h), SRCALPHA)
97 pixels = PixelArray(mask)
98 for y in xrange(h):
99 alpha = int(round(255 - (float(y) / h) * 255))
100 for x in xrange(w):
101 pixels[x][y] = Color(255, 255, 255, alpha)
102 del pixels
103 self.set_text()
104 self.reset()
105
106 def set_text(self):
107 lines = self.get_configuration("text", "preface").upper()
108 lines = str.replace(lines, "\n", " ")
109 lines = lines.upper().split("/")
110 cc = 0
111 for line in lines:
112 if len(line) > cc:
113 cc = len(line)
114 cw = int(round((self.get_width() - 80) / float(cc)))
115 font = Font(self.get_resource("display", "font"), 12)
116 font.set_bold(True)
117 ch = font.metrics(lines[0][0])[0][3]
118 margin = 24
119 surface = Surface((cw * cc,
120 ch * len(lines) + margin * (len(lines) - 1)),
121 SRCALPHA)
122 text = self.text = Sprite(self, 200)
123 text.display_surface = self
124 magic_word = "PSPPIT"
125 magic_colors = deque(((255, 128, 128), (255, 255, 128), (128, 255, 128),
126 (128, 255, 255), (128, 128, 255),
127 (255, 128, 255)))
128 box_rect = Rect(0, 0, cw, ch)
129 for _ in xrange(len(magic_word)):
130 frame = surface.copy()
131 box_rect.top = 0
132 for line in lines:
133 box_rect.left = 0
134 extra = cc - len(line)
135 space_count = 0
136 for char in line:
137 if char == " ":
138 space_count += 1
139 space_widths = [1 + extra / space_count] * space_count
140 for ii in xrange(len(space_widths)):
141 if ii >= (extra % space_count):
142 break
143 space_widths[ii] += 1
144 magic_word_ii = 0
145 for ii in xrange(len(line)):
146 if line != lines[-1] and line[ii] == " ":
147 box_rect.left += cw * space_widths.pop(-1)
148 else:
149 if (magic_word_ii and \
150 magic_word_ii < len(magic_word)) \
151 or line[ii:ii + len(magic_word)] == magic_word:
152 color = magic_colors[magic_word_ii]
153 magic_word_ii += 1
154 else:
155 color = (255, 255, 255)
156 left = font.render(line[ii], True, (255, 0, 0))
157 frame.blit(left, self.get_glyph_rect(left, box_rect, 0))
158 right = font.render(line[ii], True, (0, 0, 255))
159 frame.blit(right, self.get_glyph_rect(right, box_rect,
160 4))
161 glyph = font.render(line[ii], True, color)
162 frame.blit(glyph, self.get_glyph_rect(glyph, box_rect,
163 2))
164 box_rect.left += cw
165 box_rect.top += ch + margin
166 text.add_frame(frame)
167 magic_colors.rotate(-1)
168 text.location.centerx = self.get_width() / 2
169
170 def get_glyph_rect(self, glyph, box_rect, offset):
171 gr = glyph.get_rect()
172 gr.center = box_rect.center
173 gr.left += offset
174 return gr
175
176 def reset(self):
177 self.text.location.top = self.get_height()
178
179 def update(self):
180 self.text.move(dy=-.5)
181 if self.text.location.bottom < 0:
182 self.reset()
183 self.blit(self.background, (0, 0))
184 self.text.update()
185 self.blit(self.mask, (0, 0), None, BLEND_RGBA_MIN)
186 self.ds.blit(self, (0, 0))
187
188
189 class Floor(Sprite):
190
191 def __init__(self, parent):
192 Sprite.__init__(self, parent, 340)
193 base = load(self.get_resource("image", "brick")).convert()
194 ds = self.ds = self.get_display_surface()
195 surface = Surface((ds.get_width(), 17))
196 for swap in False, True:
197 frame = surface.copy()
198 tile = base.copy()
199 if swap:
200 pixels = PixelArray(tile)
201 foreground, background = pixels[1][1], pixels[0][0]
202 for x in xrange(len(pixels)):
203 for y in xrange(len(pixels[0])):
204 if pixels[x][y] == foreground:
205 pixels[x][y] = background
206 else:
207 pixels[x][y] = foreground
208 del pixels
209 for x in xrange(0, frame.get_width(), tile.get_width()):
210 for y in xrange(0, frame.get_height(), tile.get_height()):
211 frame.blit(tile, (x, y))
212 self.add_frame(frame)
213 self.location.bottom = self.ds.get_height() - 34