from pygame import Rect

from dark_stew.pgfw.GameChild import GameChild
from dark_stew.pool.Water import Water
from dark_stew.pool.Ring import Ring
from dark_stew.pool.stool.Stools import Stools
from dark_stew.pool.LED import LED

class Pool(GameChild, Rect):

    def __init__(self, parent, center, index):
        GameChild.__init__(self, parent)
        self.init_rect(center)
        self.water = Water(self, index)
        self.ring = Ring(self, index)
        self.stools = Stools(self)
        self.grow()
        # self.led = LED(self)

    def init_rect(self, center):
        Rect.__init__(self, 0, 0, 0, 0)
        self.center = center

    def grow(self):
        padding = self.get_configuration("pool", "padding")
        radius = self.ring.rect.w / 2 + padding
        self.inflate_ip(radius * 2, radius * 2)

    def update(self):
        self.water.update()
        self.ring.update()
        self.stools.draw()
        # self.led.update()
from os import listdir
from os.path import join
from math import sin, cos

from dark_stew.pgfw.Sprite import Sprite

class LED(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent, 20000)
        self.load_configuration()
        self.load_frames()
        self.set_position()

    def load_configuration(self):
        config = self.get_configuration("led")
        self.angle = config["angle"]
        self.offset = config["offset"]
        self.root = self.get_resource("led", "path")

    def load_frames(self):
        root = self.root
        for directory in listdir(root):
            self.load_from_path(join(root, directory), transparency=True,
                                ppa=False)

    def set_position(self):
        parent = self.parent
        cx, cy = parent.center
        distance = parent.ring.rect.w / 2 + self.offset
        angle = self.angle
        dx, dy = cos(angle) * distance, sin(angle) * distance
        self.rect.topleft = cx + dx, cy - dy

    def draw(self):
        x, y = self.rect.topleft
        surface = self.display_surface
        frame = self.get_current_frame()
        for dx, dy in self.parent.parent.parent.get_corners():
            surface.blit(frame, (x + dx, y + dy))
from pygame import Rect

from dark_stew.pgfw.GameChild import GameChild

class Stool(GameChild, Rect):

    def __init__(self, parent, center):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.image = self.parent.image
        self.init_rect(center)

    def init_rect(self, center):
        Rect.__init__(self, (0, 0), self.image.get_size())
        self.center = center

    def draw(self):
        x, y = self.topleft
        surface = self.display_surface
        for dx, dy in self.parent.parent.parent.parent.get_corners():
            surface.blit(self.image, (x + dx, y + dy))
from os.path import join
from math import sin, cos

from pygame import Surface
from pygame.image import load

from dark_stew.pgfw.GameChild import GameChild
from dark_stew.pool.stool.Stool import Stool

class Stools(GameChild, list):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.transparent_color = (0, 0, 0)
        self.load_configuration()
        self.load_image()
        self.add_stools()

    def load_configuration(self):
        config = self.get_configuration("stool")
        self.angles = config["angles"]
        self.offsets = config["offsets"]
        self.image_path = self.get_resource("stool", "path")

    def load_image(self):
        image = load(self.image_path)
        surface = Surface(image.get_size())
        color = self.transparent_color
        surface.fill(color)
        surface.set_colorkey(color)
        surface.blit(image, (0, 0))
        self.image = surface

    def add_stools(self):
        parent = self.parent
        radius = parent.ring.rect.w / 2
        cx, cy = parent.center
        for ii, measure in enumerate(zip(self.angles, self.offsets)):
            angle, offset = measure
            distance = radius + offset
            dx = cos(angle) * distance
            dy = sin(angle) * distance * (-1 if ii == 0 else 1)
            self.append(Stool(self, (cx - dx, cy + dy)))
            self.append(Stool(self, (cx + dx, cy + dy)))

    def draw(self):
        for stool in self:
            stool.draw()
from os import listdir
from os.path import join
from random import shuffle, random
from math import atan2, pi

from pygame import Rect
from pygame.image import load
from pygame.transform import flip

from dark_stew.pgfw.Sprite import Sprite
from dark_stew.aphids.Rod import Rod

class Aphids(Sprite):

    directions = 0, 1, 2, 3

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.input = self.get_input()
        self.delegate = self.get_delegate()
        self.direction = self.directions[2]
        self.center = self.display_surface.get_rect().center
        self.sitting = False
        self.load_configuration()
        self.reset_chance()
        self.rod = Rod(self)
        self.load_frames()
        self.activate_frames(self.standing_front_frames, self.rest_framerate)
        self.place_collision_rect()
        self.play()
        self.subscribe(self.respond)

    def load_configuration(self):
        config = self.get_configuration("aphids")
        self.rest_framerate = config["rest-framerate"]
        self.walk_framerate = config["walk-framerate"]
        self.run_framerate = config["run-framerate"]
        self.collision_rect = Rect(config["collision-rect"])
        self.initial_chance = config["initial-chance"]
        self.chance_increase = config["chance-increase"]
        root = self.get_resource("aphids", "path")
        animation_path = config["animation-path"]
        self.moving_path = join(root, config["moving-path"], animation_path)
        standing_path = join(root, config["standing-path"])
        front_path = config["front-path"]
        self.standing_front_path = join(standing_path, front_path,
                                        animation_path)
        self.standing_side_path = join(standing_path, config["side-path"],
                                       animation_path)
        back_path = config["back-path"]
        self.standing_back_path = join(standing_path, config["back-path"],
                                       animation_path)
        sitting_path = join(root, config["sitting-path"])
        self.sitting_front_path = join(sitting_path, front_path, animation_path)
        self.sitting_back_path = join(sitting_path, back_path, animation_path)
        self.sitting_path = sitting_path

    def reset_chance(self):
        self.catch_chance = self.initial_chance

    def load_frames(self):
        load_path = self.load_path
        self.moving_right_frames = load_path(self.moving_path, random=False)
        self.moving_left_frames = load_path(self.moving_path, True, False)
        self.standing_front_frames = load_path(self.standing_front_path)
        self.standing_right_frames = load_path(self.standing_side_path)
        self.standing_left_frames = load_path(self.standing_side_path, True)
        self.standing_back_frames = load_path(self.standing_back_path)
        self.sitting_northwest_frames = load_path(self.sitting_front_path)
        self.sitting_northeast_frames = load_path(self.sitting_front_path, True)
        self.sitting_southwest_frames = load_path(self.sitting_back_path)
        self.sitting_southeast_frames = load_path(self.sitting_back_path, True)

    def load_path(self, root, mirror=False, random=True):
        frames = []
        names = listdir(root)
        if random:
            shuffle(names)
        else:
            names.sort()
        for path in [join(root, name) for name in names]:
            image = load(path)
            if mirror:
                image = flip(image, True, False)
            frames.append(self.fill_colorkey(image))
        return frames

    def place_collision_rect(self):
        self.collision_rect.move_ip(self.rect.topleft)

    def respond(self, event):
        if self.delegate.compare(event, "action"):
            monsters = self.parent.parent.monsters
            if not self.sitting and not monsters.current:
                self.find_seat()
            elif monsters.current:
                monsters.close()
            else:
                self.sitting = False
                self.rod.deactivate()
                if random() <= self.catch_chance:
                    self.parent.parent.monsters.open_random()
                self.reset_chance()

    def find_seat(self):
        parent = self.parent
        for dx, dy in parent.get_corners():
            rect = self.collision_rect.move(-dx, -dy)
            for pool in parent.pools:
                if rect.colliderect(pool):
                    self.sit(pool, rect)
                    return

    def sit(self, pool, rect):
        cx, cy = pool.center
        x, y = rect.center
        angle = atan2(y - cy, x - cx)
        framerate = self.rest_framerate
        stools = pool.stools
        self.parent.reset_active_directions()
        if angle >= 0 and angle < pi / 2:
            self.activate_frames(self.sitting_southeast_frames, framerate)
            stool = stools[3]
            dx, dy = 11, -27
            self.rod.activate(-33, -9)
        elif angle >= pi / 2 and angle < pi:
            self.activate_frames(self.sitting_southwest_frames, framerate)
            stool = stools[2]
            dx, dy = -14, -27
            self.rod.activate(12, -8, True)
        elif angle >= -pi and angle < -pi / 2:
            self.activate_frames(self.sitting_northwest_frames, framerate)
            stool = stools[0]
            dx, dy = -13, -29
            self.rod.activate(10, -12, True)
        else:
            self.activate_frames(self.sitting_northeast_frames, framerate)
            stool = stools[1]
            dx, dy = 11, -29
            self.rod.activate(-30, -12)
        self.sitting = True
        self.parent.shift(rect.centerx - stool.centerx + dx,
                          rect.centery - stool.centery + dy, False)

    def activate_frames(self, frames, framerate):
        if self.frames != frames:
            self.frames = frames
            self.frame_index = 0
            self.measure_rect()
            self.rect.center = self.center
            self.set_framerate(framerate)

    def update(self):
        if self.sitting:
            self.catch_chance += (1 - self.catch_chance) * self.chance_increase
        self.set_frames()
        Sprite.update(self)
        self.rod.update()

    def set_frames(self):
        parent = self.parent
        direction = parent.direction
        up, right, down, left = parent.directions
        if self.parent.scroll_active:
            if self.input.is_command_active("run"):
                framerate = self.run_framerate
            else:
                framerate = self.walk_framerate
            if direction in [right, up]:
                self.activate_frames(self.moving_right_frames, framerate)
            else:
                self.activate_frames(self.moving_left_frames, framerate)
        elif not self.sitting:
            framerate = self.rest_framerate
            if direction == up:
                self.activate_frames(self.standing_back_frames, framerate)
            elif direction == right:
                self.activate_frames(self.standing_right_frames, framerate)
            elif direction == down:
                self.activate_frames(self.standing_front_frames, framerate)
            else:
                self.activate_frames(self.standing_left_frames, framerate)
216.73.216.133
216.73.216.133
216.73.216.133
 
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.