write
authorFrank DeMarco <frank.s.demarco@gmail.com>
Sun, 27 Apr 2014 22:12:18 +0000 (07:12 +0900)
committerFrank DeMarco <frank.s.demarco@gmail.com>
Sun, 27 Apr 2014 22:12:18 +0000 (07:12 +0900)
scale_sieve/ScaleSieve.py

index 9b5ea69..d7c2420 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from random import randint, randrange, choice
+from time import time
 
 from pygame import Surface, PixelArray, Rect
 from pygame.draw import aalines, polygon
@@ -171,7 +172,9 @@ class Scoreboard(GameChild, Surface):
     def __init__(self, parent):
         GameChild.__init__(self, parent)
         Surface.__init__(self, (380, 80))
+        self.scores_path = self.get_resource("score", "path")
         self.display_surface = self.get_display_surface()
+        self.most_recent_score = None
         self.fill((255, 255, 255))
         self.rect = self.get_rect()
         self.rect.center = self.display_surface.get_rect().centerx, 350
@@ -179,7 +182,7 @@ class Scoreboard(GameChild, Surface):
 
     def load(self):
         scores = []
-        for line in file(self.get_resource("score", "path"), "r"):
+        for line in file(self.scores_path, "r"):
             fields = line.split()
             scores.append((float(fields[0]), int(fields[1])))
         self.sprites = sprites = []
@@ -188,17 +191,30 @@ class Scoreboard(GameChild, Surface):
         for ii, score in enumerate(sorted(scores, key=lambda score: score[1],
                                           reverse=True)[:3]):
             font = Font(font_path, [40, 24, 16][ii])
-            sprites.append(Sprite(self))
-            sprites[ii].add_frame(font.render(str(score[1]) + "¥", True, (0, 0, 0),
+            sprites.append(Sprite(self, 250))
+            text = str(score[1]) + "¥"
+            color = (0, 0, 0) if not (score[1] == self.most_recent_score) else \
+                    (255, 255, 255)
+            sprites[ii].add_frame(font.render(text, True, color,
                                               (255, 255, 255)))
+            if score[1] == self.most_recent_score:
+                sprites[ii].add_frame(font.render(text, True, (0, 255, 0),
+                                                  (255, 255, 255)))
             sprites[ii].location.midtop = self.rect.w / 2, y
             y += sprites[ii].location.h
             sprites[ii].display_surface = self
 
+    def write(self):
+        score = int(round(self.get_game().triangles.score))
+        file(self.scores_path, "a").write(str(time()) + " " + str(score) + "\n")
+        self.most_recent_score = score
+        self.load()
+
     def update(self):
-        self.display_surface.blit(self, self.rect)
+        self.fill((255, 255, 255))
         for sprite in self.sprites:
             sprite.update()
+        self.display_surface.blit(self, self.rect)
 
 
 class Sieve(Strip):
@@ -463,6 +479,7 @@ class Static(Sprite):
     def respond(self, event):
         if self.active and self.complete:
             if self.delegate.compare(event, "advance"):
+                self.parent.title.scoreboard.write()
                 self.total.deactivate()
                 self.deactivate()
                 self.reset()