from pygame import Surface
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild

class Background(Surface, GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_screen()
        self.init_surface()
        self.load_tile()
        self.draw_tiles()

    def init_surface(self):
        Surface.__init__(self, self.display_surface.get_size())

    def load_tile(self):
        self.tile = load(self.get_resource("overworld",
                                           "background-tile-path")).convert()

    def draw_tiles(self):
        tile = self.tile
        width = tile.get_width()
        x_limit, y_limit = self.get_size()
        x, y = 0, 0
        for y in xrange(0, y_limit, width):
            for x in xrange(0, x_limit, width):
                self.blit(tile, (x, y))

    def update(self):
        self.display_surface.blit(self, (0, 0))
from pygame import Surface, Rect
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild

class Section(Surface, GameChild):

    def __init__(self, parent, left=True):
        GameChild.__init__(self, parent)
        Surface.__init__(self, self.parent.size)
        self.left = left
        self.display_surface = self.get_display_surface()
        self.rect = self.get_rect()
        self.draw_tiles()
        self.fill_borders()
        self.place()

    def draw_tiles(self):
        tile = self.parent.tile
        x_limit, y_limit = self.get_size()
        width = tile.get_width()
        for y in xrange(0, y_limit, width):
            for x in xrange(0, x_limit, width):
                self.blit(tile, (x, y))

    def place(self):
        rect = self.rect
        rect.top = self.parent.top
        if not self.left:
            rect.right = self.display_surface.get_rect().right

    def fill_borders(self):
        reference = self.rect
        top_rect = Rect(reference.topleft, (reference.w, 1))
        bottom_rect = Rect((0, 0), (reference.w, 1))
        bottom_rect.bottom = reference.bottom
        side_rect = Rect((0, 0), (1, reference.h))
        if self.left:
            side_rect.right = reference.right
        color = self.parent.border
        self.fill(color, top_rect)
        self.fill(color, bottom_rect)
        self.fill(color, side_rect)

    def update(self):
        self.display_surface.blit(self, self.rect)
from pygame import Surface
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild
from esp_hadouken.overworld.wall.Section import Section

class Wall(Surface, GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_configuration()
        self.load_tile()
        self.set_sections()
        # 224x25 gap: 52

    def load_configuration(self):
        config = self.get_configuration("overworld")
        self.size = config["wall-size"]
        self.top = config["wall-position"]
        self.border = config["wall-border"]

    def load_tile(self):
        self.tile = load(self.get_resource("overworld",
                                           "wall-tile-path")).convert()

    def set_sections(self):
        self.sections = [Section(self, left) for left in False, True]

    def update(self):
        for section in self.sections:
            section.update()
from random import choice

from pygame import PixelArray, Color
from pygame.image import load

from esp_hadouken.pgfw.Sprite import Sprite
from esp_hadouken.dot.engine.Engine import Engine

class Dot(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.engine = Engine(self)
        self.delegate = self.get_delegate()
        self.load_configuration()
        self.load_image()
        self.load_palette()
        self.add_frames()
        self.reset()
        self.subscribe(self.respond)

    def load_configuration(self):
        config = self.get_configuration("dot")
        self.framerate_range = config["framerate-range"]
        self.transparent_color = config["transparent-color"]
        self.frame_count = config["frame-count"]

    def load_image(self):
        image = load(self.get_resource("dot", "image-path")).convert()
        image.set_colorkey(self.transparent_color)
        self.image = image

    def load_palette(self):
        config = self.get_configuration("sprite")
        self.palette = [map(Color, palette) for palette in
                        config["dark-palette"], config["light-palette"]]

    def add_frames(self):
        for ii in xrange(self.frame_count):
            surface = self.image.copy()
            self.paint_frame(surface)
            self.add_frame(surface)

    def paint_frame(self, surface):
        pixels = PixelArray(surface)
        dark, light = map(choice, self.palette)
        pixels.replace((255, 255, 255), light)
        pixels.replace((0, 0, 0), dark)

    def reset(self):
        self.set_framerate(self.framerate_range[1])
        self.engine.reset()

    def respond(self, event):
        if self.delegate.compare(event, "reset-game"):
            self.reset()

    def is_active(self):
        return self.parent.active

    def update(self):
        if self.is_active():
            self.engine.update()
            self.move(*self.engine)
            Sprite.update(self)
from pygame.font import Font

from esp_hadouken.pgfw.GameChild import GameChild
from esp_hadouken.pgfw.Vector import Vector
from esp_hadouken.dot.engine.accelerator.Accelerator import Accelerator
from esp_hadouken.dot.engine.Calibrator import Calibrator
from esp_hadouken.dot.engine.Profile import Profile

class Engine(GameChild, Vector):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.input = self.get_input()
        self.display_surface = self.get_screen()
        self.load_configuration()
        self.display_active = self.check_command_line(self.display_flag)
        self.calibration_active = self.check_command_line(self.calibrate_flag)
        self.accelerator = Accelerator(self)
        self.profile = Profile(self)
        self.accelerator.set_slopes()
        self.add_calibrator()
        self.reset()
        self.init_display()

    def load_configuration(self):
        config = self.get_configuration("engine")
        self.display_flag = config["display-flag"]
        self.calibrate_flag = config["calibrate-flag"]
        self.initial_profile = config["initial-profile"]
        self.font_path = self.get_resource("engine", "font-path")

    def add_calibrator(self):
        if self.calibration_active:
            self.calibrator = Calibrator(self)

    def reset(self):
        Vector.__init__(self)
        self.accelerator.reset()

    def init_display(self):
        if self.display_active:
            self.display_surface = self.get_screen()
            self.font = Font(self.font_path, 14)
            self.render()

    def render(self):
        string = str(self)
        self.text = self.font.render(string, False, (0, 0, 0), (255, 255, 255))
        self.string = string

    def set(self, max_velocity, deceleration):
        self.max_velocity = max_velocity
        self.deceleration = deceleration

    def update(self):
        if self.calibration_active:
            self.calibrator.update()
        self.accelerator.update()
        self.apply_accelerator()
        self.constrain()
        self.decelerate()
        self.display()

    def apply_accelerator(self):
        self += self.accelerator.get_sum()

    def constrain(self):
        self.apply_to_components(self.constrain_component)

    def constrain_component(self, magnitude):
        if magnitude > self.max_velocity:
            return self.max_velocity
        if magnitude < -self.max_velocity:
            return -self.max_velocity
        return magnitude

    def decelerate(self):
        self.apply_to_components(self.decelerate_component)

    def decelerate_component(self, magnitude):
        deceleration = self.deceleration
        if abs(magnitude) < deceleration:
            magnitude = 0
        else:
            if magnitude > 0:
                magnitude -= deceleration
            else:
                magnitude += deceleration
        return magnitude

    def display(self):
        if self.display_active:
            if self.string != str(self):
                self.render()
            self.display_surface.blit(self.text, (0, 0))

    def __str__(self):
        return "[{0: .2f}, {1: .2f}]".format(*self)
44.212.99.248
44.212.99.248
44.212.99.248
 
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.