preface
authorFrank DeMarco <frank.s.demarco@gmail.com>
Sun, 24 Aug 2014 18:50:35 +0000 (03:50 +0900)
committerFrank DeMarco <frank.s.demarco@gmail.com>
Sun, 24 Aug 2014 19:16:11 +0000 (04:16 +0900)
shaken_and_spit_out/SSO.py

index d917079..e5657d4 100644 (file)
@@ -1,6 +1,7 @@
 from os import listdir
 from glob import glob
 from random import choice
+from collections import deque
 
 from pygame import cursors, PixelArray, Surface, Color
 from pygame.mouse import set_cursor
@@ -10,6 +11,7 @@ from pygame.locals import *
 
 from shaken_and_spit_out.pgfw.Game import Game
 from shaken_and_spit_out.pgfw.GameChild import GameChild
+from shaken_and_spit_out.pgfw.Sprite import Sprite
 
 class SSO(Game):
 
@@ -84,7 +86,7 @@ class Preface(GameChild, Surface):
     def __init__(self, parent):
         GameChild.__init__(self, parent)
         ds = self.ds = self.get_display_surface()
-        w, h = ds.get_width(), int(round(ds.get_width() / 1.618))
+        w, h = ds.get_width(), int(round(ds.get_width() / 3))
         Surface.__init__(self, (w, h), SRCALPHA)
         self.background = Surface((w, h))
         self.background.fill((0, 0, 0))
@@ -95,11 +97,87 @@ class Preface(GameChild, Surface):
             for x in xrange(w):
                 pixels[x][y] = Color(255, 255, 255, alpha)
         del pixels
+        self.set_text()
+        self.reset()
+
+    def set_text(self):
+        lines = self.get_configuration("text", "preface").upper()
+        lines = str.replace(lines, "\n", " ")
+        lines = lines.upper().split("/")
+        cc = 0
+        for line in lines:
+            if len(line) > cc:
+                cc = len(line)
+        cw = int(round((self.get_width() - 80) / float(cc)))
+        font = Font(self.get_resource("display", "font"), 12)
+        font.set_bold(True)
+        ch = font.metrics(lines[0][0])[0][3]
+        margin = 24
+        surface = Surface((cw * cc,
+                           ch * len(lines) + margin * (len(lines) - 1)),
+                          SRCALPHA)
+        text = self.text = Sprite(self, 200)
+        text.display_surface = self
+        magic_word = "PSPPIT"
+        magic_colors = deque(((255, 128, 128), (255, 255, 128), (128, 255, 128),
+                              (128, 255, 255), (128, 128, 255),
+                              (255, 128, 255)))
+        box_rect = Rect(0, 0, cw, ch)
+        for _ in xrange(len(magic_word)):
+            frame = surface.copy()
+            box_rect.top = 0
+            for line in lines:
+                box_rect.left = 0
+                extra = cc - len(line)
+                space_count = 0
+                for char in line:
+                    if char == " ":
+                        space_count += 1
+                space_widths = [1 + extra / space_count] * space_count
+                for ii in xrange(len(space_widths)):
+                    if ii >= (extra % space_count):
+                        break
+                    space_widths[ii] += 1
+                magic_word_ii = 0
+                for ii in xrange(len(line)):
+                    if line != lines[-1] and line[ii] == " ":
+                        box_rect.left += cw * space_widths.pop(-1)
+                    else:
+                        if (magic_word_ii and \
+                            magic_word_ii < len(magic_word)) \
+                               or line[ii:ii + len(magic_word)] == magic_word:
+                            color = magic_colors[magic_word_ii]
+                            magic_word_ii += 1
+                        else:
+                            color = (255, 255, 255)
+                        left = font.render(line[ii], True, (255, 0, 0))
+                        frame.blit(left, self.get_glyph_rect(left, box_rect, 0))
+                        right = font.render(line[ii], True, (0, 0, 255))
+                        frame.blit(right, self.get_glyph_rect(right, box_rect,
+                                                              4))
+                        glyph = font.render(line[ii], True, color)
+                        frame.blit(glyph, self.get_glyph_rect(glyph, box_rect,
+                                                              2))
+                        box_rect.left += cw
+                box_rect.top += ch + margin
+            text.add_frame(frame)
+            magic_colors.rotate(-1)
+        text.location.centerx = self.get_width() / 2
+
+    def get_glyph_rect(self, glyph, box_rect, offset):
+        gr = glyph.get_rect()
+        gr.center = box_rect.center
+        gr.left += offset
+        return gr
 
     def reset(self):
-        pass
+        self.text.location.top = self.get_height()
 
     def update(self):
+        self.text.move(dy=-.5)
+        if self.text.location.bottom < 0:
+            self.reset()
         self.blit(self.background, (0, 0))
+        self.text.update()
         self.blit(self.mask, (0, 0), None, BLEND_RGBA_MIN)
         self.ds.blit(self, (0, 0))