from lib.pgfw.pgfw.GameChild import GameChild
from food_spring.level.watermelon.Watermelon import Watermelon
from food_spring.level.peach.Peach import Peach
from food_spring.level.pineapple.Pineapple import Pineapple
from food_spring.level.grapes.Grapes import Grapes

class Levels(GameChild, list):

    WATERMELON, PEACH, GRAPES, PINEAPPLE = range(4)

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        list.__init__(self, (Watermelon(self), Peach(self), Grapes(self),
                             Pineapple(self)))

    def activate(self, index):
        self[index].activate()

    def update(self):
        for level in self:
            level.update()
from random import randrange, choice

from pygame import Surface, PixelArray, Color
from pygame.image import load, save
from pygame.locals import *

from lib.pgfw.pgfw.Animation import Animation

class Window(Animation):

    def __init__(self, parent):
        Animation.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.load_configuration()
        self.set_images()
        self.set_x_range()
        self.alpha_ratio = randrange(100) * .01
        self.alpha_direction = choice((1, -1))

    def load_configuration(self):
        config = self.get_configuration("window")
        self.alpha = config["alpha"]
        self.remain_length = config["remain-length"]
        self.fade_step = config["fade-step"]
        self.margin = config["margin"]

    def set_images(self):
        image = load(self.get_resource("window", "path")).convert()
        inverted = Surface(image.get_size())
        inverted.fill((255, 255, 255))
        inverted.blit(image, (0, 0), None, BLEND_SUB)
        pixels = PixelArray(inverted)
        for x in xrange(len(pixels) - 1):
            for y in xrange(len(pixels[x]) - 1):
                color = Color(*image.unmap_rgb(pixels[x][y]))
                hue, saturation, value, alpha = color.hsva
                color.hsva = hue, saturation, 100 - value, alpha
                pixels[x][y] = color
        del pixels
        self.images = image, inverted
        self.rect = image.get_rect()
        self.rect.right = self.display_surface.get_rect().right - self.margin

    def set_x_range(self):
        margin = self.margin
        self.x_range = margin, \
                       self.display_surface.get_width() - margin - self.rect.w

    def reset(self):
        pass

    def update(self):
        Animation.update(self)
        self.update_alpha()
        intermediate = Surface(self.rect.size)
        intermediate.blit(self.images[0], (0, 0))
        intermediate.blit(self.images[1], (0, 0))
        self.display_surface.blit(intermediate, self.rect, None, BLEND_MAX)

    def update_alpha(self):
        self.alpha_ratio += self.fade_step * self.alpha_direction
        if self.alpha_ratio < 0:
            self.alpha_ratio = 0
            self.alpha_direction = 1
        elif self.alpha_ratio > 1:
            self.alpha_ratio = 1
            self.alpha_direction = -1
        alpha = self.alpha
        self.images[0].set_alpha(int(alpha * self.alpha_ratio))
        self.images[1].set_alpha(int(alpha * (1 - self.alpha_ratio)))
from food_spring.level.Level import Level

class Peach(Level):

    def __init__(self, parent):
        Level.__init__(self, parent, parent.PEACH)
from food_spring.level.Level import Level

class Grapes(Level):

    def __init__(self, parent):
        Level.__init__(self, parent, parent.GRAPES)
from pygame import Surface, Color

from lib.pgfw.pgfw.GameChild import GameChild

class Plate(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_configuration()
        self.init_surface()
        self.set_background()

    def load_configuration(self):
        config = self.get_configuration("land")
        self.height = config["height"]
        self.speed = config["fade-speed"]

    def init_surface(self):
        Surface.__init__(self, (self.get_display_surface().get_width(),
                                self.height))

    def set_background(self):
        width, height = self.get_size()
        background = Surface((width, height))
        color = Color(0, 0, 0)
        for y in xrange(height):
            hue = int(float(y) / (height) * 360)
            color.hsva = hue, 100, 100, 100
            background.fill(color, (0, y, width, 1))
        self.background = background

    def reset(self):
        self.offset = 0

    def update(self):
        self.update_offset()
        self.draw()

    def update_offset(self):
        offset = self.offset - self.speed
        height = self.get_height()
        if offset < -height:
            offset += height
        self.offset = offset

    def draw(self):
        offset = int(self.offset)
        background = self.background
        self.blit(background, (0, offset))
        self.blit(background, (0, offset + self.get_height()))
from pygame import Rect
from pygame.locals import *

from lib.pgfw.pgfw.GameChild import GameChild
from food_spring.level.land.Mask import Mask
from food_spring.level.land.Plate import Plate

class Land(GameChild, Rect):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_configuration()
        self.mask = Mask(self)
        self.plate = Plate(self)
        self.display_surface = self.get_display_surface()
        self.init_rect()
        self.reset()

    def load_configuration(self):
        config = self.get_configuration("land")
        self.height = config["height"]
        self.altitude_ratio = config["altitude-ratio"]

    def init_rect(self):
        Rect.__init__(self, (0, self.get_initial_top()), self.mask.get_size())

    def get_initial_top(self):
        return self.display_surface.get_height() - self.h

    def reset(self):
        self.top = self.get_initial_top()
        self.mask.reset()
        self.plate.reset()

    def update(self):
        self.mask.update()
        self.plate.update()
        self.display_surface.blit(self.plate, self)
        self.display_surface.blit(self.mask, self, None, BLEND_RGB_MIN)
3.139.72.78
3.139.72.78
3.139.72.78
 
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.