from pygame import Surface

from lib.pgfw.pgfw.Sprite import Sprite

class Spikes(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_configuration()
        self.set_frames()
        self.register(self.slide)
        self.reset()

    def load_configuration(self):
        config = self.get_configuration("spikes")
        self.delay = config["delay"]
        self.speed = config["speed"]

    def set_frames(self):
        surface = Surface((32, 8))
        surface.fill((0, 255, 0))
        self.add_frame(surface, omit=True)
        self.add_frameset(0, name="sliding")
        blank = Surface(self.location.size)
        blank.set_colorkey((0, 0, 0))
        self.add_frame(blank, omit=True)
        self.add_frameset([0, 1], self.parent.blink_rate, "queued")

    def reset(self):
        self.set_frameset("queued")
        self.halt(self.slide)
        self.active = False
        self.queue_reset = False
        self.location.center = self.display_surface.get_rect().right, \
                               self.parent.parent.platforms[0].location.top

    def queue_slide(self):
        self.active = True
        self.play(self.slide, delay=self.delay)

    def slide(self):
        self.set_frameset("sliding")
        self.move(-self.speed)
        if self.location.right <= self.display_surface.get_rect().left:
            self.queue_reset = True
        if self.location.colliderect(self.parent.parent.food):
            self.parent.parent.food.freeze()

    def update(self):
        if self.active:
            Sprite.update(self)
        if self.queue_reset:
            self.reset()
from random import randrange

from pygame import Surface

from lib.pgfw.pgfw.Sprite import Sprite

class Fireball(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_configuration()
        self.set_frames()
        self.register(self.spew)
        self.reset()

    def load_configuration(self):
        config = self.get_configuration("fireball")
        self.delay = config["delay"]
        self.speed = config["speed"]
        self.peak = config["peak"]

    def set_frames(self):
        surface = Surface((24, 8))
        surface.fill((0, 255, 255))
        self.add_frame(surface, omit=True)
        self.add_frameset(0, name="spewing")
        blank = Surface(self.location.size)
        blank.set_colorkey((0, 0, 0))
        self.add_frame(blank, omit=True)
        self.add_frameset([0, 1], self.parent.blink_rate, "queued")

    def reset(self):
        self.halt(self.spew)
        self.set_frameset("queued")
        self.active = False
        self.queue_reset = False
        limit = self.display_surface.get_rect()
        self.location.midleft = randrange(0, limit.w - self.location.w), limit.h
        self.direction = -1

    def queue_spew(self):
        self.active = True
        self.play(self.spew, delay=self.delay)

    def spew(self):
        self.set_frameset("spewing")
        self.move(0, self.direction * self.speed)
        limit = self.parent.parent.platforms[0].location.top - self.peak
        if self.direction == -1 and self.location.centery <= limit:
            self.location.centery = limit
            self.direction = 1
        elif self.direction == 1 and \
             self.location.top >= self.display_surface.get_height():
            self.queue_reset = True
        if self.location.colliderect(self.parent.parent.food):
            self.parent.parent.food.freeze()

    def update(self):
        if self.active:
            Sprite.update(self)
        if self.queue_reset:
            self.reset()
from math import atan, degrees, radians, sqrt, sin, cos
from random import randint, randrange

from pygame import Surface
from pygame.transform import rotate

from lib.pgfw.pgfw.Sprite import Sprite

class Missile(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.load_configuration()
        self.set_step()
        self.set_frames()
        self.register(self.fly)
        self.reset()

    def load_configuration(self):
        config = self.get_configuration("missile")
        self.margin = config["margin"]
        self.speed = config["speed"]
        self.peak = config["peak"]
        self.delay = config["delay"]
        self.angle = config["angle"]

    def set_step(self):
        angle = radians(self.angle)
        self.step = sin(angle) * self.speed, cos(angle) * self.speed

    def set_frames(self):
        surface = Surface((8, 32))
        surface.fill((255, 0, 0))
        surface.set_colorkey((255, 0, 255))
        self.add_frame(rotate(surface, self.angle), omit=True)
        self.add_frameset(0, name="flying")
        blank = Surface(self.location.size)
        blank.set_colorkey((0, 0, 0))
        self.add_frame(blank, omit=True)
        self.add_frameset([0, 1], self.parent.blink_rate, "queued")

    def reset(self):
        self.halt(self.fly)
        self.active = False
        self.queue_reset = False
        self.location.center = randrange(40, 640), 0
        self.set_frameset("queued")

    def is_flying(self):
        return self.is_playing(self.fly)

    def queue_fly(self):
        self.active = True
        self.play(self.fly, delay=self.delay)

    def fly(self):
        self.set_frameset("flying")
        self.move(*self.step)
        location = self.location
        if location.colliderect(self.parent.parent.food):
            self.parent.parent.food.freeze()
        if location.top > self.display_surface.get_height():
            self.queue_reset = True

    def update(self):
        if self.active:
            Sprite.update(self)
        if self.queue_reset:
            self.reset()
from lib.pgfw.pgfw.Sprite import Sprite

class Blob(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_from_path(self.get_resource("blob", "path"), True, True,
                            query="*.png")
        self.hide()

    def hide(self):
        self.visible = False

    def show(self):
        self.visible = True

    def update(self):
        if self.visible:
            if not self.parent.horizontal:
                self.location.centerx = self.parent.parent.food.location.centerx
            else:
                self.location.centery = self.parent.parent.food.location.centery
            Sprite.update(self)
216.73.216.212
216.73.216.212
216.73.216.212
 
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.