from os import makedirs, walk, sep, remove
from os.path import join, dirname, basename, exists
from shutil import rmtree, copy, rmtree
from itertools import chain
from zipfile import ZipFile

import py2exe

from Setup import Setup

class SetupWin(Setup):

    def __init__(self):
        Setup.__init__(self)
        self.replace_isSystemDLL()

    def replace_isSystemDLL(self):
        origIsSystemDLL = py2exe.build_exe.isSystemDLL
        def isSystemDLL(pathname):
            if basename(pathname).lower() in ("libogg-0.dll", "sdl_ttf.dll"):
                return 0
            return origIsSystemDLL(pathname)
        py2exe.build_exe.isSystemDLL = isSystemDLL

    def setup(self):
        config = self.config.get_section("setup")
	windows = [{}]
	if config["init-script"]:
	    windows[0]["script"] = config["init-script"]
	if config["icon-path"]:
	    windows[0]["icon-resources"] = [(1, config["icon-path"])] 
        Setup.setup(self, windows,
                    {"py2exe": {"packages": self.build_package_list(),
                                "dist_dir": config["windows-dist-path"]}})
        rmtree("build")
        self.copy_data_files()
        self.create_archive()

    def copy_data_files(self):
        for path in chain(*zip(*self.build_data_map())[1]):
            dest = join(self.config.get("setup", "windows-dist-path"),
                          dirname(path))
            if not exists(dest):
                makedirs(dest)
            copy(path, dest)

    def create_archive(self):
        config = self.config.get_section("setup")
        title = self.translate_title() + "-" + config["version"] + "-win"
        archive_name = title + ".zip"
        archive = ZipFile(archive_name, "w")
        destination = config["windows-dist-path"]
        for root, dirs, names in walk(destination):
            for name in names:
                path = join(root, name)
                archive.write(path, path.replace(destination, title + sep))
        archive.close()
        copy(archive_name, "dist")
        remove(archive_name)
        rmtree(destination)
from os import environ

import pygame
from pygame import display
from pygame.locals import *

from GameChild import *
from Animation import *
from Audio import *
from Display import *
from Configuration import *
from EventDelegate import *
from Input import *
from ScreenGrabber import *

class Game(GameChild, Animation):
    
    resources_path = None

    def __init__(self, config_rel_path=None, type_declarations=None):
        self.init_gamechild()
        self.print_debug(pygame.version.ver)
        self.config_rel_path = config_rel_path
        self.type_declarations = type_declarations
        self.set_configuration()
        self.init_animation()
        self.align_window()
        pygame.init()
        self.set_children()
        self.subscribe_to(QUIT, self.end)
        self.subscribe_to(self.get_custom_event_id(), self.end)
        self.clear_queue()
        self.delegate.enable()

    def init_gamechild(self):
        GameChild.__init__(self)

    def set_configuration(self):
        self.configuration = Configuration(self.config_rel_path,
                                           self.resources_path,
                                           self.type_declarations)

    def init_animation(self):
        Animation.__init__(self,
                           self.configuration.get("display", "frame-duration"))

    def align_window(self):
        if self.configuration.get("display", "centered"):
            environ["SDL_VIDEO_CENTERED"] = "1"

    def set_children(self):
        self.set_delegate()
        self.set_display()
        self.set_input()
        self.set_audio()
        self.set_screen_grabber()

    def set_display(self):
        self.display = Display(self)

    def set_delegate(self):
        self.delegate = EventDelegate(self)

    def set_input(self):
        self.input = Input(self)

    def set_audio(self):
        self.audio = Audio(self)

    def set_screen_grabber(self):
        self.screen_grabber = ScreenGrabber(self)

    def sequence(self):
        self.delegate.dispatch_events()
        self.update()
        display.update()

    def update(self):
        pass

    def end(self, evt):
        if evt.type == QUIT or self.is_command(evt, "quit"):
            self.stop()
from os import listdir
from os.path import join

from pygame.mixer import Channel, Sound, music, find_channel

from GameChild import *
from Input import *

class Audio(GameChild):

    current_channel = None
    paused = False
    muted = False

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.load_fx()
        self.subscribe_to(self.get_custom_event_id(), self.mute)

    def load_fx(self):
        fx = {}
        if self.get_configuration().has_option("audio", "sfx-path"):
            root = self.get_resource("audio", "sfx-path")
            if root:
                for name in listdir(root):
                    fx[name.split(".")[0]] = Sound(join(root, name))
        self.fx = fx

    def mute(self, event):
        if self.is_command(event, "mute"):
            self.muted = not self.muted
            self.set_volume()

    def set_volume(self):
        volume = int(not self.muted)
        music.set_volume(volume)
        if self.current_channel:
            self.current_channel.set_volume(volume)

    def play_bgm(self, path, stream=False):
        self.stop_current_channel()
        if stream:
            music.load(path)
            music.play(-1)
        else:
            self.current_channel = Sound(path).play(-1)
        self.set_volume()

    def stop_current_channel(self):
        music.stop()
        if self.current_channel:
            self.current_channel.stop()
        self.current_channel = None
        self.paused = False

    def play_fx(self, name, panning=.5):
        if not self.muted:
            channel = find_channel(True)
            if panning != .5:
                offset = 1 - abs(panning - .5) * 2
                if panning < .5:
                    channel.set_volume(1, offset)
                else:
                    channel.set_volume(offset, 1)
            channel.play(self.fx[name])

    def pause(self):
        channel = self.current_channel
        paused = self.paused
        if paused:
            music.unpause()
            if channel:
                channel.unpause()
        else:
            music.pause()
            if channel:
                channel.pause()
        self.paused = not paused

    def is_bgm_playing(self):
        current = self.current_channel
        if current and current.get_sound():
            return True
        return music.get_busy()
54.91.16.95
54.91.16.95
54.91.16.95
 
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:


↠ RSS Feed ↞