from time import time
from random import randint

from pygame import Surface, Color

from antidefense.pgfw.GameChild import GameChild
from antidefense.map.scale.Scale import Scale
from antidefense.map.Layer import Layer
from antidefense.map.Stamps import Stamps

class Map(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.deactivate()
        self.set_surface()
        self.set_dimensions()
        self.subscribe_to(self.get_custom_event_id(), self.respond_to_event)
        self.scale = Scale(self)
        self.stamps = Stamps(self)
        self.reset()

    def deactivate(self):
        self.active = False

    def set_surface(self):
        Surface.__init__(self, self.get_screen().get_size())

    def set_dimensions(self):
        magnitude = self.get_configuration("map", "magnitude")
        self.dimensions = tuple(map(lambda x: x ** magnitude, self.get_size()))

    def respond_to_event(self, evt):
        if evt.command == "reset-game":
            self.reset()

    def reset(self):
        self.visible_dimensions = self.dimensions
        self.scale.update()
        self.set_target()
        self.set_target_pixel()
        self.stamps.reset()
        self.current_layer = Layer(self)

    def set_target(self):
        w, h = self.dimensions
        self.target = randint(0, w - 1), randint(0, h - 1)

    def set_target_pixel(self):
        vx, vy = self.visible_dimensions
        x, y = self.target
        w, h = self.get_screen().get_size()
        self.target_pixel = int(float(x) / vx * w), int(float(y) / vy * h)

    def activate(self):
        self.active = True

    def get_zoom_level(self):
        return 1 - float(self.visible_dimensions[0]) / self.dimensions[0]
        
    def update(self):
        if self.active:
            self.clear()
            self.draw()

    def clear(self):
        self.blit(self.current_layer, (0, 0))

    def draw(self):
        self.scale.draw()
        self.get_screen().blit(self, (0, 0))
from pygame import Surface, Color

from antidefense.pgfw.GameChild import GameChild

class Bar(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.config = self.get_configuration("scale")
        self.init_surface()
        self.set_rect()
        self.paint()

    def init_surface(self):
        config = self.config
        Surface.__init__(self, config["bar-dimensions"])
        self.set_alpha(config["bar-alpha"])

    def set_rect(self):
        rect = self.get_rect()
        rect.bottom = self.parent.get_height()
        self.rect = rect

    def paint(self):
        config = self.config
        colors = map(Color, config["colors"])
        w = self.get_width()
        segment_w = w / config["segments"]
        for ii, x in enumerate(range(0, w, segment_w)):
            color = colors[ii % len(colors)]
            self.fill(color, (x, 0, segment_w, self.get_height()))

    def draw(self):
        self.parent.blit(self, self.rect)
from pygame import Surface, Color
from pygame.locals import *

from antidefense.pgfw.GameChild import GameChild
from antidefense.map.scale.Bar import Bar
from antidefense.map.scale.Text import Text

class Scale(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.config = self.get_configuration("scale")
        self.init_surface()
        self.set_rect()
        self.bar = Bar(self)
        self.text = Text(self)

    def init_surface(self):
        config = self.config
        bar_w, bar_h = config["bar-dimensions"]
        h = bar_h + config["font-size"] + config["font-padding"] * 2
        Surface.__init__(self, (bar_w, h), SRCALPHA)
        self.clear()

    def set_rect(self):
        rect = self.get_rect()
        x, y = self.config["offset"]
        rect.left = x
        rect.bottom = self.parent.get_height() - y
        self.rect = rect

    def update(self):
        self.clear()
        self.text.update()
        self.bar.draw()

    def clear(self):
        self.fill((0, 0, 0, 0))

    def draw(self):
        self.parent.blit(self, self.rect)
from math import log

from pygame import Surface, Color
from pygame.font import Font
from pygame.locals import *

from antidefense.pgfw.GameChild import GameChild

class Text(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.config = self.get_configuration("scale")
        self.init_surface()
        self.set_rect()
        self.set_fonts()
        self.text = ""

    def init_surface(self):
        parent = self.parent
        h = parent.get_height() - self.config["bar-dimensions"][1]
        Surface.__init__(self, (self.parent.get_width(), h), SRCALPHA)

    def set_rect(self):
        self.rect = self.get_rect()

    def set_fonts(self):
        config = self.config
        path = self.get_resource("scale", "font-path")
        font = Font(path, config["font-size"])
        font.set_bold(True)
        self.font = font

    def update(self):
        self.set_text()
        self.clear()
        self.write_text()
        self.draw()

    def set_text(self):
        config = self.config
        grandparent = self.parent.parent
        ratio = float(self.get_width()) / grandparent.get_width()
        distance = ratio * grandparent.visible_dimensions[0]
        magnitude = int(log(distance, 10))
        resolution = config["resolution"]
        interval = magnitude / resolution
        prefix = config["prefixes"][interval]
        converted = int(distance / 10 ** (interval * resolution))
        self.text = ("%i %s%s" % (converted, prefix, config["unit"])).decode("utf-8")

    def clear(self):
        self.fill((0, 0, 0))
        self.fill((0, 0, 0, 0))

    def write_text(self):
        self.write_stroke()
        block = self.font.render(self.text, True,
                                 Color(self.config["font-color"]))
        rect = block.get_rect()
        rect.center = self.rect.center
        self.blit(block, rect)

    def write_stroke(self):
        config = self.config
        font = self.font
        text = self.text
        color = Color(config["stroke-color"])
        offset = config["stroke-offset"]
        left, right = [font.render(text, True, color) for _ in range(2)]
        lrect = left.get_rect()
        center = self.rect.center
        lrect.center = center
        lrect.x -= 1
        self.blit(left, lrect)
        rrect = right.get_rect()
        rrect.center = center
        rrect.x += 1
        self.blit(right, rrect)

    def draw(self):
        self.parent.blit(self, self.rect)
3.133.129.8
3.133.129.8
3.133.129.8
 
September 13, 2013

from array import array
from time import sleep

import pygame
from pygame.mixer import Sound, get_init, pre_init

class Note(Sound):

    def __init__(self, frequency, volume=.1):
        self.frequency = frequency
        Sound.__init__(self, self.build_samples())
        self.set_volume(volume)

    def build_samples(self):
        period = int(round(get_init()[0] / self.frequency))
        samples = array("h", [0] * period)
        amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
        for time in xrange(period):
            if time < period / 2:
                samples[time] = amplitude
            else:
                samples[time] = -amplitude
        return samples

if __name__ == "__main__":
    pre_init(44100, -16, 1, 1024)
    pygame.init()
    Note(440).play(-1)
    sleep(5)

This program generates and plays a 440 Hz tone for 5 seconds. It can be extended to generate the spectrum of notes with a frequency table or the frequency formula. Because the rewards in Send are idealized ocean waves, they can also be represented as tones. Each level has a tone in its goal and a tone based on where the player's disc lands. Both play at the end of a level, sounding harmonic for a close shot and discordant for a near miss. The game can dynamically create these tones using the program as a basis.

I'm also building an algorithmically generated song: Silk Routes (Scissored). Here is an example of how it sounds so far.