score
authorFrank DeMarco <frank.s.demarco@gmail.com>
Sun, 27 Apr 2014 18:21:42 +0000 (03:21 +0900)
committerFrank DeMarco <frank.s.demarco@gmail.com>
Sun, 27 Apr 2014 18:21:42 +0000 (03:21 +0900)
config
scale_sieve/ScaleSieve.py
scale_sieve/pgfw/Sprite.py

diff --git a/config b/config
index b1036a5..0c78f1f 100644 (file)
--- a/config
+++ b/config
@@ -1,8 +1,13 @@
 [display]
 dimensions = 420, 700
+font-path = Titan-One.ttf
+
+[mouse]
+visible = no
 
 [keys]
 quit = K_ESCAPE
+advance = K_RETURN
 
 [interpolate]
 volume = C 0.0 0.0, 16.0 0.61, 80.0 1.0
index 56fa368..fc86b75 100644 (file)
@@ -1,7 +1,10 @@
+# -*- coding: utf-8 -*-
+
 from random import randint, randrange, choice
 
 from pygame import Surface, PixelArray, Rect
 from pygame.draw import aalines, polygon
+from pygame.font import Font
 from pygame.locals import *
 
 from scale_sieve.pgfw.Game import Game
@@ -14,49 +17,95 @@ class ScaleSieve(Game):
         Game.__init__(self)
         self.background = Surface(self.display.screen.get_size())
         self.background.fill((255, 80, 190))
+        self.title.activate()
 
     def set_children(self):
         Game.set_children(self)
-        self.acid = Acid(self)
+        self.title = Title(self)
         self.sieve = Sieve(self)
         self.triangles = Triangles(self)
+        self.acid = Acid(self)
         self.static = Static(self)
 
     def update(self):
-        self.display.screen.blit(self.background, (0, 0))
+        self.title.update()
+        if self.triangles.active:
+            self.display.screen.blit(self.background, (0, 0))
         self.triangles.update()
         self.sieve.update()
         self.static.update()
 
 
+class Title(GameChild):
+
+    def __init__(self, parent):
+        GameChild.__init__(self, parent)
+        self.display_surface = self.get_display_surface()
+        self.delegate = self.parent.delegate
+        self.background = surface = Surface(self.display_surface.get_size())
+        surface.fill((0, 255, 255))
+        self.subscribe(self.respond)
+
+    def respond(self, event):
+        if self.active:
+            if self.delegate.compare(event, "advance"):
+                self.deactivate()
+                self.parent.triangles.activate()
+                self.parent.sieve.activate()
+                self.parent.static.activate()
+
+    def activate(self):
+        self.active = True
+
+    def deactivate(self):
+        self.active = False
+
+    def update(self):
+        if self.active:
+            self.display_surface.blit(self.background, (0, 0))
+
+
 class Strip(Sprite):
 
     LEFT, RIGHT = range(2)
 
     def __init__(self, parent):
         Sprite.__init__(self, parent)
+        self.deactivate()
         self.display_surface = self.get_display_surface()
         self.delegate = self.get_game().delegate
         self.hshifts = Shift(self, 1, "shift-2"), Shift(self, -1, "shift-2")
         self.add_frames()
         self.subscribe(self.respond)
 
+    def deactivate(self):
+        self.active = False
+
+    def reset(self):
+        for shift in self.hshifts:
+            shift.reset()
+
     def add_frames(self):
         pass
 
     def respond(self, event):
-        compare = self.delegate.compare
-        if compare(event, "left") or compare(event, "left", True):
-            self.hshifts[self.LEFT].active = not event.cancel
-        elif compare(event, "right") or compare(event, "right", True):
-            self.hshifts[self.RIGHT].active = not event.cancel
+        if self.active:
+            compare = self.delegate.compare
+            if compare(event, "left") or compare(event, "left", True):
+                self.hshifts[self.LEFT].active = not event.cancel
+            elif compare(event, "right") or compare(event, "right", True):
+                self.hshifts[self.RIGHT].active = not event.cancel
+
+    def activate(self):
+        self.active = True
 
     def update(self):
-        for shift in self.hshifts:
-            shift.update()
-            if shift.time:
-                self.move(shift.get_change())
-        Sprite.update(self)
+        if self.active:
+            for shift in self.hshifts:
+                shift.update()
+                if shift.time:
+                    self.move(shift.get_change())
+            Sprite.update(self)
 
 
 class Shift(GameChild):
@@ -64,11 +113,14 @@ class Shift(GameChild):
     def __init__(self, parent, direction, nodeset):
         GameChild.__init__(self, parent)
         self.direction = direction
-        self.active = False
-        self.time = 0
+        self.reset()
         self.timer = self.get_game().time_filter
         self.nodeset = self.get_game().interpolator.get_nodeset(nodeset)
 
+    def reset(self):
+        self.active = False
+        self.time = 0
+
     def update(self):
         least, greatest = self.nodeset[0].x, self.nodeset[-1].x
         if self.active and self.time < greatest:
@@ -89,7 +141,6 @@ class Sieve(Strip):
     def __init__(self, parent):
         Strip.__init__(self, parent)
         self.delegate = self.get_game().delegate
-        self.location.center = self.display_surface.get_rect().center
         self.electric = Electric(self)
         self.add_location(offset=(self.location.w, 0))
 
@@ -120,18 +171,24 @@ class Sieve(Strip):
         for frame in frames:
             self.add_frame(frame)
 
+    def reset(self):
+        Strip.reset(self)
+        self.location.centerx = self.display_surface.get_rect().centerx
+        self.locations[1].centerx = self.location.centerx + self.location.w
+
     def update(self):
-        if self.location.right < 0:
-            self.move(self.location.w)
-        if self.locations[1].left > self.display_surface.get_width():
-            self.move(-self.location.w)
-        for location in self.locations:
-            location.bottom = self.parent.acid.get_top()
-        self.electric.location.centery = self.location.centery + 13
-        self.electric.update()
-        for rect in self.bar_rects:
-            rect.centery = self.location.centery
-        Strip.update(self)
+        if self.active:
+            if self.location.right < 0:
+                self.move(self.location.w)
+            if self.locations[1].left > self.display_surface.get_width():
+                self.move(-self.location.w)
+            for location in self.locations:
+                location.bottom = self.parent.acid.get_top()
+            self.electric.location.centery = self.location.centery + 13
+            self.electric.update()
+            for rect in self.bar_rects:
+                rect.centery = self.location.centery
+            Strip.update(self)
 
 
 class Electric(Sprite):
@@ -161,13 +218,22 @@ class Triangles(GameChild, list):
 
     def __init__(self, parent):
         GameChild.__init__(self, parent)
-        list.__init__(self, [])
+        self.deactivate()
         self.display_surface = self.get_display_surface()
         self.delegate = self.get_game().delegate
         self.booster = Shift(self, 1, "boost")
-        self.populate()
+        self.reset()
         self.subscribe(self.respond)
 
+    def deactivate(self):
+        self.active = False
+
+    def reset(self):
+        list.__init__(self, [])
+        self.streak = 0
+        self.score = 0
+        self.booster.reset()
+
     def populate(self):
         if not self:
             self.append(Triangle(self))
@@ -182,32 +248,42 @@ class Triangles(GameChild, list):
         self.next_gap = randint(500, 800)
 
     def respond(self, event):
-        compare = self.delegate.compare
-        if compare(event, "down") or compare(event, "down", True):
-            self.booster.active = not event.cancel
+        if self.active:
+            compare = self.delegate.compare
+            if compare(event, "down") or compare(event, "down", True):
+                self.booster.active = not event.cancel
 
     def get_boost(self):
         return self.booster.get_change()
 
+    def activate(self):
+        self.active = True
+
     def update(self):
-        self.populate()
-        self.booster.update()
-        if self[0].location.collidelist(self.parent.sieve.locations) != -1:
-            sieve = self.parent.sieve
-            if self[0].location.colliderect(sieve.electric.location):
-                self.parent.acid.increase()
-                self.remove(self[0])
-            else:
-                for br in sieve.bar_rects:
-                    for tr in self[0].collision_rects:
-                        if tr.move((self[0].location.left,
-                                    0)).colliderect(br.move((sieve.location.left,
-                                                             0))):
-                            self.remove(self[0])
-                            self.parent.static.increase()
-                            break
-        for triangle in self:
-            triangle.update()
+        if self.active:
+            self.populate()
+            self.booster.update()
+            if self[0].location.collidelist(self.parent.sieve.locations) != -1:
+                sieve = self.parent.sieve
+                if self[0].location.colliderect(sieve.electric.location):
+                    self.parent.acid.increase()
+                    self.streak += 1
+                    self.score += self.streak ** .8 + \
+                                  self.parent.acid.get_volume() * 5 + \
+                                  self[0].count
+                    self.remove(self[0])
+                else:
+                    for br in sieve.bar_rects:
+                        for tr in self[0].collision_rects:
+                            if tr.move((self[0].location.left,
+                                        0)).colliderect(br.move((sieve.location.left,
+                                                                 0))):
+                                self.remove(self[0])
+                                self.parent.static.increase()
+                                self.streak = 0
+                                break
+            for triangle in self:
+                triangle.update()
 
 
 class Triangle(Sprite):
@@ -237,6 +313,7 @@ class Triangle(Sprite):
             x += width - sieve.bar_w
         self.add_frame(surface)
         self.location.centerx = self.get_display_surface().get_rect().centerx
+        self.count = len(widths)
 
     def update(self):
         self.move(dy=5.5 * self.get_game().acid.get_volume() + 3.8 + \
@@ -252,8 +329,11 @@ class Acid(GameChild):
         GameChild.__init__(self, parent)
         self.display_surface = self.get_display_surface()
         self.level_r = 80, 320
-        self.substance = 0
         self.nodeset = self.get_game().interpolator.get_nodeset("volume")
+        self.reset()
+
+    def reset(self):
+        self.substance = 0
 
     def get_top(self):
         return self.display_surface.get_height() - self.get_level()
@@ -272,10 +352,22 @@ class Acid(GameChild):
 class Static(Sprite):
 
     def __init__(self, parent):
-        Sprite.__init__(self, parent)
+        Sprite.__init__(self, parent, 120)
+        self.deactivate()
+        self.delegate = self.get_game().delegate
+        self.increaser = Shift(self, 1, "intensity")
+        self.total = Total(self)
+        self.reset()
         self.add_frames()
+        self.subscribe(self.respond)
+
+    def deactivate(self):
+        self.active = False
+
+    def reset(self):
+        self.complete = False
         self.intensity = 0
-        self.increaser = Shift(self, 1, "intensity")
+        self.increaser.reset()
 
     def add_frames(self):
         surface = Surface(self.get_display_surface().get_size())
@@ -286,7 +378,6 @@ class Static(Sprite):
         pixel_arrays = []
         for tile in tiles:
             pixel_arrays.append(PixelArray(tile))
-        # colors = (0, 0, 0), (128, 128, 128), (198, 128, 255), (255, 255, 255)
         colors = (0, 0, 0), (64, 64, 64), (128, 128, 128), (196, 196, 196), \
                  (255, 255, 255)
         for x in xrange(len(pixel_arrays[0])):
@@ -302,6 +393,17 @@ class Static(Sprite):
                     frame.blit(choice(tiles), (x, y))
             self.add_frame(frame)
 
+    def respond(self, event):
+        if self.active and self.complete:
+            if self.delegate.compare(event, "advance"):
+                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()
         print self.increaser.get_change()
@@ -311,11 +413,72 @@ class Static(Sprite):
         if self.increaser.time >= self.increaser.nodeset[-1].x + 5000:
             self.increaser.time = self.increaser.nodeset[-1].x + 5000
 
+    def activate(self):
+        self.active = True
+
     def update(self):
-        self.set_alpha(int(self.intensity * 255))
-        if self.intensity >= .65:
-            print "game over"
-        if self.intensity > 0:
-            self.intensity *= .998
-        self.increaser.update()
-        Sprite.update(self)
+        if self.active:
+            if not self.complete and self.intensity >= .65:
+                self.complete = True
+                self.parent.sieve.deactivate()
+                self.parent.triangles.deactivate()
+                self.set_alpha(255)
+                self.total.load()
+            elif not self.complete:
+                self.set_alpha(int(self.intensity * 255))
+                if self.intensity > 0:
+                    self.intensity *= .998
+                self.increaser.update()
+            Sprite.update(self)
+            self.total.update()
+
+
+class Total(Sprite):
+
+    def __init__(self, parent):
+        Sprite.__init__(self, parent, 68)
+        self.deactivate()
+        self.font = Font(self.get_resource("display", "font-path"), 48)
+        self.font.set_italic(True)
+
+    def deactivate(self):
+        self.active = False
+
+    def load(self):
+        self.clear_frames()
+        score = ""
+        for ch in str(int(round(self.get_game().triangles.score))):
+            score += ch + " "
+        score += "¥"
+        colors = (255, 255, 80), (80, 255, 255), (255, 80, 255), \
+                 (255, 120, 60), (60, 255, 120), (120, 60, 255)
+        template = Surface((self.display_surface.get_width(), 100))
+        transparent_color = (255, 0, 255)
+        template.fill(transparent_color)
+        template.set_colorkey(transparent_color)
+        tr = template.get_rect()
+        template.fill((255, 0, 0), (0, 20, tr.w, 1))
+        template.fill((255, 128, 128), (0, 21, tr.w, 1))
+        for y in xrange(22, 78, 2):
+            template.fill((255, 255, 255), (0, y, tr.w, 1))
+        template.fill((255, 128, 128), (0, 78, tr.w, 1))
+        template.fill((255, 0, 0), (0, 79, tr.w, 1))
+        for _ in xrange(20):
+            surface = template.copy()
+            polygon(surface, choice(colors), ((tr.centerx - 7, 19),
+                                              (tr.centerx, 0),
+                                              (tr.centerx + 7, 19)))
+            text = self.font.render(score, True, choice(colors))
+            rect = text.get_rect()
+            rect.center = tr.centerx, tr.centery + 2
+            surface.blit(text, rect)
+            polygon(surface, choice(colors), ((tr.centerx - 7, 80),
+                                              (tr.centerx, tr.h - 1),
+                                              (tr.centerx + 7, 80)))
+            self.add_frame(surface)
+        self.location.center = self.display_surface.get_rect().center
+        self.active = True
+
+    def update(self):
+        if self.active:
+            Sprite.update(self)
index 5ceb7c5..1df6bd1 100644 (file)
@@ -189,13 +189,13 @@ class Sprite(Animation):
             for frameset in self.framesets:
                 frameset.reverse()
 
-    def update(self):
+    def update(self, flags=0):
         Animation.update(self)
-        self.draw()
+        self.draw(flags)
 
-    def draw(self):
+    def draw(self, flags=0):
         for location in self.locations:
-            location.fader.draw()
+            location.fader.draw(flags)
 
 
 class Location(Rect):
@@ -263,7 +263,7 @@ class Fader(Surface):
                 self.start_time = self.time_filter.get_ticks()
                 self.fading_out = out
 
-    def draw(self):
+    def draw(self, flags):
         sprite = self.location.sprite
         if self.fade_remaining >= 0:
             self.update_alpha()
@@ -273,15 +273,16 @@ class Fader(Surface):
             self.blit(frame, (0, 0))
             frame.set_alpha(sprite.alpha)
             if not sprite.hidden:
-                self.blit_to_display(self)
+                self.blit_to_display(self, flags)
         elif self.fade_remaining is None or self.get_alpha() >= sprite.alpha:
             if self.fade_remaining >= 0:
                 self.update_alpha()
             if not sprite.hidden:
-                self.blit_to_display(sprite.get_current_frame())
+                self.blit_to_display(sprite.get_current_frame(), flags)
 
-    def blit_to_display(self, frame):
-        self.location.sprite.display_surface.blit(frame, self.location)
+    def blit_to_display(self, frame, flags):
+        self.location.sprite.display_surface.blit(frame, self.location, None,
+                                                  flags)
 
     def update_alpha(self):
         remaining = self.fade_remaining = self.fade_length - \