initials
authorFrank DeMarco <frank.s.demarco@gmail.com>
Thu, 23 Jul 2015 16:42:07 +0000 (12:42 -0400)
committerFrank DeMarco <frank.s.demarco@gmail.com>
Thu, 23 Jul 2015 16:42:07 +0000 (12:42 -0400)
config
electric_sieve/ElectricSieve.py
resource/high-scores

diff --git a/config b/config
index 838701c..6932032 100644 (file)
--- a/config
+++ b/config
@@ -16,6 +16,7 @@ dimensions = 420, 700
 score-font-path = font/Titan-One.ttf
 title-font-path = font/Oxygen.ttf
 scoreboard-font-path = font/terminus/Terminus.ttf
+initials-font = font/terminus/Terminus-Bold.ttf
 
 [mouse]
 visible = no
index 617bb4a..f390918 100644 (file)
@@ -2,6 +2,7 @@
 
 from random import randint, randrange, choice
 from time import time
+from operator import itemgetter
 
 from pygame import Surface, PixelArray, Rect
 from pygame.draw import aalines, polygon
@@ -185,6 +186,7 @@ class Scoreboard(GameChild):
     BLINK_INTERVAL = 400
     PADDING = 6
     BORDER = 1
+    SCORE_COUNT = 9
 
     def __init__(self, parent):
         GameChild.__init__(self, parent)
@@ -194,21 +196,17 @@ class Scoreboard(GameChild):
         self.load()
 
     def load(self):
-        scores = []
-        for line in file(self.scores_path, "r"):
-            fields = line.split()
-            scores.append((float(fields[0]), int(fields[1]), fields[2]))
         self.sprites = sprites = []
         font_path = self.get_resource("display", "scoreboard-font-path")
-        sizes = [24] * 5
-        for ii, score in enumerate(sorted(scores, key=lambda score: score[1],
-                                          reverse=True)[:len(sizes)]):
+        sizes = [24] * self.SCORE_COUNT
+        blink = False
+        for ii, score in enumerate(self.get_scores()[:len(sizes)]):
             font = Font(font_path, sizes[ii])
             sprites.append((Sprite(self, self.BLINK_INTERVAL),
                             Sprite(self, self.BLINK_INTERVAL)))
             score_text = str(score[1])
-            color = self.FOREGROUND if not (score[1] == self.most_recent_score) else \
-                    self.BACKGROUND
+            color = self.BACKGROUND if (self.most_recent_score and not blink and score[1:] == \
+                                        self.most_recent_score) else self.FOREGROUND
             score_plate = font.render(score_text, False, color, self.BACKGROUND)
             rect = score_plate.get_rect()
             surface = Surface(rect.inflate((2, 2)).size)
@@ -219,21 +217,31 @@ class Scoreboard(GameChild):
                                                 self.BACKGROUND, self.FOREGROUND, self.BORDER, self.PADDING))
             sprites[ii][0].add_frame(render_box(font, score[2], False, color,
                                                  self.BACKGROUND, self.FOREGROUND, self.BORDER, self.PADDING))
-            if score[1] == self.most_recent_score:
+            if self.most_recent_score and not blink and score[1:] == self.most_recent_score:
                 sprites[ii][1].add_frame(render_box(font, score_text, False, self.NEW,
                                                     self.BACKGROUND, self.FOREGROUND, self.BORDER, self.PADDING))
                 sprites[ii][0].add_frame(render_box(font, score[2], False, self.NEW,
                                                     self.BACKGROUND, self.FOREGROUND, self.BORDER, self.PADDING))
+                blink = True
             sprites[ii][0].location.left = self.MARGIN
             sprites[ii][1].location.right = self.get_display_surface().get_rect().right - self.MARGIN
             y = self.get_display_surface().get_rect().centery + self.SPACING * (ii - len(sizes) / 2)
             for sprite in sprites[ii]:
                 sprite.location.centery = y
 
-    def write(self):
+    def get_scores(self):
+        scores = []
+        for line in file(self.scores_path, "r"):
+            fields = line.split()
+            scores.append((float(fields[0]), int(fields[1]), fields[2]))
+        scores = sorted(scores, key=itemgetter(0))
+        return sorted(scores, key=itemgetter(1), reverse=True)
+
+    def write(self, initials):
         score = int(round(self.get_game().triangles.score))
-        file(self.scores_path, "a").write(str(time()) + " " + str(score) + " " + "SIV" + "\n")
-        self.most_recent_score = score
+        fields = str(time()), str(score), initials
+        file(self.scores_path, "a").write(fields[0] + " " + fields[1] + " " + fields[2] + "\n")
+        self.most_recent_score = score, initials
         self.load()
 
     def update(self):
@@ -474,6 +482,7 @@ class Static(Sprite):
         self.delegate = self.get_game().delegate
         self.increaser = Shift(self, 1, "intensity")
         self.total = Total(self)
+        self.initials = Initials(self)
         self.reset()
         self.add_frames()
         self.subscribe(self.respond)
@@ -513,16 +522,26 @@ class Static(Sprite):
             self.add_frame(frame)
 
     def respond(self, event):
-        if self.active and self.complete:
+        if self.active and self.complete and not self.initials.active:
             if self.delegate.compare(event, "advance"):
-                self.parent.title.scoreboard.write()
-                self.total.deactivate()
-                self.deactivate()
-                self.reset()
-                self.parent.acid.reset()
-                self.parent.triangles.reset()
-                self.parent.sieve.reset()
-                self.parent.title.activate()
+                if self.get_game().triangles.score > \
+                  self.get_game().title.scoreboard.get_scores()[Scoreboard.SCORE_COUNT - 1][1]:
+                    self.total.deactivate()
+                    self.initials.activate()
+                else:
+                    self.finish(wipe=True)
+
+    def finish(self, text="---", wipe=False):
+        if wipe:
+            self.parent.title.scoreboard.most_recent_score = None
+        self.parent.title.scoreboard.write(text)
+        self.total.deactivate()
+        self.deactivate()
+        self.reset()
+        self.parent.acid.reset()
+        self.parent.triangles.reset()
+        self.parent.sieve.reset()
+        self.parent.title.activate()
 
     def increase(self):
         self.intensity += self.increaser.get_change()
@@ -554,6 +573,90 @@ class Static(Sprite):
                 self.noise.set_volume(self.intensity)
             Sprite.update(self)
             self.total.update()
+            self.initials.update()
+
+
+class Initials(GameChild):
+
+    LETTER_SIZE = 24
+    FOREGROUND = 27, 27, 27
+    BACKGROUND = 255, 255, 255
+    PADDING = 10
+    ARROW_MARGIN = 40
+    ARROW_HEIGHT = 10
+    
+
+    def __init__(self, parent):
+        GameChild.__init__(self, parent)
+        self.reset()
+        self.deactivate()
+        self.font = Font(self.get_resource("display", "initials-font"), self.LETTER_SIZE)
+        self.subscribe(self.respond)
+
+    def reset(self):
+        self.text = "---"
+        self.index = 0
+
+    def deactivate(self):
+        self.active = False
+
+    def respond(self, event):
+        if self.active:
+            compare = self.get_game().delegate.compare
+            if compare(event, "right") or compare(event, "advance"):
+                self.index += 1
+                if self.index == len(self.text):
+                    self.index = 0
+                    if compare(event, "advance"):
+                        self.deactivate()
+                        self.parent.finish(self.text)
+            elif compare(event, "left"):
+                self.index -= 1
+                if self.index == -1:
+                    self.index = len(self.text) - 1
+            elif compare(event, "up") or compare(event, "down"):
+                if compare(event, "up"):
+                    increment = 1
+                elif compare(event, "down"):
+                    increment = -1
+                letter = self.text[self.index]
+                if letter == '-':
+                    letter = 'A' if increment == 1 else 'Z'
+                else:
+                    letter = chr(ord(letter) + increment)
+                if ord(letter) == 91 or ord(letter) == 64:
+                    letter = '-'
+                replacement = ""
+                for ii in xrange(len(self.text)):
+                    if ii == self.index:
+                        replacement += letter
+                    else:
+                        replacement += self.text[ii]
+                self.text = replacement
+
+    def activate(self):
+        self.active = True
+
+    def update(self):
+        if self.active:
+            ds = self.get_display_surface()
+            for ii, letter in enumerate(self.text):
+                box = render_box(self.font, letter, False, self.FOREGROUND, self.BACKGROUND,
+                                 self.FOREGROUND, padding=self.PADDING)
+                rect = box.get_rect()
+                rect.centery = ds.get_rect().centery
+                rect.centerx = ii * ds.get_width() / 3 + ds.get_width() / 6
+                ds.blit(box, rect)
+                if ii == self.index:
+                    margin = self.ARROW_MARGIN
+                    polygon(ds, (0, 255, 0), ((rect.left, rect.top - margin),
+                                              (rect.right, rect.top - margin),
+                                              (rect.centerx,
+                                               rect.top - margin - self.ARROW_HEIGHT)))
+                    polygon(ds, (0, 255, 0), ((rect.left, rect.bottom + margin),
+                                              (rect.right, rect.bottom + margin),
+                                              (rect.centerx,
+                                               rect.bottom + margin + self.ARROW_HEIGHT)))
 
 
 class Total(Sprite):
index 555d37c..fb29342 100644 (file)
@@ -1,9 +1,9 @@
-1398631170.296798 100 SIV
-1398631170.296798 70 SIV
-1398631170.296798 45 SIV
-1398635621.7 25 SIV
-1398635792.99 10 SIV
-1437647034.17 214 SIV
-1437647409.07 32 SIV
-1437648294.15 168 SIV
-1437648394.84 105 SIV
+1398631170.296798 80 ELE
+1398631170.296798 60 ELE
+1398631170.296798 50 ELE
+1398635621.7 40 ELE
+1398635792.99 30 ELE
+1398635792.99 25 ELE
+1398635792.99 20 ELE
+1398635792.99 15 ELE
+1398635792.99 10 ELE