from pygame import Surface, Color, Rect
from pygame.time import get_ticks

from ball_cup.pgfw.Animation import Animation

class Charge(Animation, Surface):

    def __init__(self, parent):
        Animation.__init__(self, parent, self.flash)
        self.display_surface = self.get_display_surface()
        self.input = self.get_input()
        self.load_configuration()
        self.register(self.flash, interval=self.interval)
        self.init_surface()
        self.reset()
        self.play()

    def load_configuration(self):
        config = self.get_configuration("charge")
        self.transparent_color = config["transparent-color"]
        self.peak_time = config["peak-time"]
        self.colors = self.build_color_list(config["colors"])
        self.width = config["width"]
        self.interval = config["interval"]
        self.magnitude_range = config["magnitude-range"]

    def build_color_list(self, colors):
        return [Color(color + "FF") for color in colors]

    def init_surface(self):
        Surface.__init__(self, (self.width, self.magnitude_range[1] * 2))
        self.set_colorkey(self.transparent_color)
        self.fill(self.transparent_color)
        rect = self.get_rect()
        rect.centery = self.display_surface.get_rect().centery
        self.rect = rect

    def reset(self):
        self.cancel()
        self.set_strength()
        self.color_index = 0

    def cancel(self):
        self.start = None
        self.fill(self.transparent_color)

    def set_strength(self):
        strength = 0
        if self.start:
            peak = self.peak_time
            elapsed = (get_ticks() - self.start) % peak
            strength = min(1, float(elapsed) / peak)
        self.strength = strength

    def is_charging(self):
        return self.start is not None

    def flash(self):
        if self.is_charging():
            self.increment_color_index()
            self.fill(self.transparent_color)
            self.fill(self.colors[self.color_index], self.get_fill_section())

    def get_fill_section(self):
        lower, upper = self.magnitude_range
        magnitude = self.strength * (upper - lower) + lower
        rect = Rect(0, 0, self.get_width(), magnitude * 2)
        rect.top = self.get_height() / 2 - magnitude
        return rect

    def increment_color_index(self):
        index = self.color_index + 1
        if index >= len(self.colors):
            index = 0
        self.color_index = index

    def update(self):
        self.check_input()
        self.set_strength()
        self.clear()
        Animation.update(self)
        self.draw()

    def check_input(self):
        pressed = self.input.is_command_active("left")
        charging = self.is_charging()
        if not pressed:
            if charging:
                self.release()
        elif pressed and not charging:
            self.hold()

    def release(self):
        self.parent.send()
        self.cancel()

    def hold(self):
        self.start = get_ticks()

    def clear(self):
        self.parent.parent.clear(self.rect)

    def draw(self):
        self.display_surface.blit(self, self.rect)
from ball_cup.pgfw.GameChild import GameChild
from ball_cup.field.scoreboard.Score import Score

class Scoreboard(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.load_configuration()
        self.set_scores()
        self.reset()

    def load_configuration(self):
        config = self.get_configuration("scoreboard")
        self.height = config["height"]
        self.segment_width = config["score-segment-width"]
        self.margin_top = config["margin-top"]
        self.border_width = config["score-border-width"]
        self.border_color = config["score-border-color"]

    def set_scores(self):
        display_surface = self.get_display_surface()
        height = self.height
        segment_width = self.segment_width
        colors = self.parent.result.success_colors
        border_width = self.border_width
        border_color = self.border_color
        centerx = display_surface.get_rect().centerx
        margin = self.margin_top
        self.scores = [Score(self, display_surface, height, segment_width,
                             colors, border_width, border_color, centerx,
                             margin),
                       Score(self, display_surface, height, segment_width,
                             colors, border_width, border_color, centerx,
                             display_surface.get_height() - height - margin)]

    def reset(self):
        for score in self.scores:
            score.reset()

    def refresh(self):
        for score in self.scores:
            score.insert(self.parent.result.color)

    def update(self):
        for score in self.scores:
            score.draw()

    def clear(self):
        for score in self.scores:
            self.parent.clear(score.rect)
from pygame import Surface

from ball_cup.pgfw.GameChild import GameChild

class Score(Surface, GameChild):

    def __init__(self, parent, display_surface, height, segment_width, colors,
                 border_width, border_color, centerx, top):
        GameChild.__init__(self, parent)
        self.display_surface = display_surface
        self.height = height
        self.segment_width = segment_width
        self.colors = colors
        self.border_width = border_width
        self.border_color = border_color
        self.centerx = centerx
        self.top = top
        self.reset()
        self.populate()

    def reset(self):
        counts = []
        for color in self.colors:
            counts.append([color, 0])
        self.draw_counts = counts
        self.populate()

    def populate(self):
        self.init_surface()
        self.fill_borders()
        self.fill_center()

    def init_surface(self):
        Surface.__init__(self, (self.measure_length(),
                                self.height + self.border_width * 2))
        rect = self.get_rect()
        rect.centerx = self.centerx
        rect.top = self.top
        self.rect = rect

    def measure_length(self):
        length = 0
        for color, count in self.draw_counts:
            length += count * self.segment_width
        return length + self.border_width * 2

    def fill_borders(self):
        color = self.border_color
        rect = self.rect
        thickness = self.border_width
        self.fill(color, (0, 0, rect.w, thickness))
        self.fill(color, (rect.w - thickness, 0, thickness, rect.h))
        self.fill(color, (0, rect.h - thickness, rect.w, thickness))
        self.fill(color, (0, 0, thickness, rect.h))

    def fill_center(self):
        offset = self.border_width
        size = self.segment_width / 2, self.get_height() - offset * 2
        right = self.rect.w
        indent = offset
        for color in self.colors:
            for _ in range(self.get_draw_count(color)):
                self.fill(color, ((indent, offset), size))
                self.fill(color, ((right - indent - size[0], offset), size))
                indent += size[0]

    def get_draw_count(self, key):
        for color, count in self.draw_counts:
            if key == color:
                return count

    def insert(self, color):
        counts = self.draw_counts
        for ii in range(len(counts)):
            if counts[ii][0] == color:
                counts[ii][1] += 1
        self.populate()

    def draw(self):
        self.display_surface.blit(self, self.rect)
216.73.216.184
216.73.216.184
216.73.216.184
 
August 12, 2013

I've been researching tartan/plaid recently for decoration in my updated version of Ball & Cup, now called Send. I want to create the atmosphere of a sports event, so I plan on drawing tartan patterns at the vertical edges of the screen as backgrounds for areas where spectator ants generate based on player performance. I figured I would make my own patterns, but after browsing tartans available in the official register, I decided to use existing ones instead.

I made a list of the tartans that had what I thought were interesting titles and chose 30 to base the game's levels on. I sequenced them, using their titles to form a loose narrative related to the concept of sending. Here are three tartans in the sequence (levels 6, 7 and 8) generated by an algorithm I inferred by looking at examples that reads a tartan specification and draws its pattern using a simple dithering technique to blend the color stripes.


Acadia


Eve


Spice Apple

It would be wasting an opportunity if I didn't animate the tartans, so I'm thinking about animations for them. One effect I want to try is making them look like water washing over the area where the ants are spectating. I've also recorded some music for the game. Here are the loops for the game over and high scores screens.

Game Over

High Scores