import sys

import pygame
from pygame import mouse, display, key
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 *
from Timer import *
from PauseScreen import *
from introduction.Introduction import *
from overworld.Overworld import *
from levels.LevelFactory import *
from ending.Ending import *
from scoreboard.HighScores import *
from scoreboard.GlyphPalette import *

class Game(GameChild, Animation):

    def __init__(self):
        GameChild.__init__(self)
        self.configuration = Configuration()
        Animation.__init__(self, self.configuration["display-frame-duration"])
        pygame.init()
        mouse.set_visible(False)
        self.add_children()
        self.subscribe_to_events()
        self.introduction.activate()
        self.clear_queue()
        self.delegate.enable()

    def add_children(self):
        self.delegate = EventDelegate(self)
        self.display = Display(self)
        self.input = Input(self)
        self.audio = Audio(self)
        self.screen_grabber = ScreenGrabber(self)
        self.timer = Timer(self)
        self.pause_screen = PauseScreen(self)
        self.introduction = Introduction(self)
        self.overworld = Overworld(self)
        self.level_factory = LevelFactory(self)
        self.glyph_palette = GlyphPalette(self)
        self.ending = Ending(self)
        self.high_scores = HighScores(self)

    def subscribe_to_events(self):
        self.subscribe_to(QUIT, self.end)
        self.subscribe_to(Input.command_event, self.end)

    def get_configuration(self):
        return self.configuration

    def get_input(self):
        return self.input

    def sequence(self):
        self.delegate.dispatch_events()
        self.introduction.update()
        self.overworld.update()
        self.level_factory.update()
        display.update()

    def end(self, evt):
        if evt.type == QUIT or evt.command == "quit":
            sys.exit()
        elif evt.command == "skip" and key.get_mods() & KMOD_LSHIFT:
            self.skip_to_ending()

    def skip_to_ending(self):
        self.timer.conceal()
        self.introduction.deactivate()
        self.ending.display()
from pygame import Color

from esp_hadouken.GameChild import *
from esp_hadouken.Font import *

class InputMessage(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.render()
        self.set_rect()

    def render(self):
        config = self.get_configuration()
        size = config["ending-input-message-size"]
        text = config["ending-input-message"]
        color = Color(config["ending-input-message-color"])
        background = Color(config["ending-input-message-bg"])
        self.message = Font(self, size).render(text, True, color, background)

    def set_rect(self):
        relative = self.parent.get_rect()
        rect = self.message.get_rect()
        rect.centerx = relative.centerx
        rect.top = self.get_configuration()["ending-input-message-y"]
        self.rect = rect

    def activate(self):
        self.active = True

    def deactivate(self):
        self.active = False

    def update(self):
        if self.active:
            self.draw()

    def draw(self):
        self.parent.blit(self.message, self.rect)
from pygame import Surface, image

from esp_hadouken.GameChild import *
from esp_hadouken.Input import *
from esp_hadouken.Toy import *
from Score import *
from name_prompt.NamePrompt import *
from InputMessage import *
from esp_hadouken.scoreboard.Scoreboard import *

class Ending(GameChild, Surface):

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.display_surface = self.get_screen()
        Surface.__init__(self, self.display_surface.get_size())
        self.add_children()
        self.subscribe_to_events()
        self.set_background()
        self.deactivate()

    def add_children(self):
        self.score = Score(self)
        self.name_prompt = NamePrompt(self)
        self.input_message = InputMessage(self)
        self.scoreboard = Scoreboard(self)

    def subscribe_to_events(self):
        self.subscribe_to(USEREVENT, self.display)
        self.subscribe_to(Input.command_event, self.run_command)

    def display(self, evt=None):
        if not evt or evt.name == "toy-collected":
            self.deactivate()
            self.activate()
            self.activate_initial_widgets()
            self.update()

    def activate(self):
        self.active = True
        self.start_music()

    def activate_initial_widgets(self):
        self.input_message.activate()
        self.name_prompt.activate()
        self.score.activate()

    def deactivate(self):
        self.active = False
        self.deactivate_initial_widgets()
        self.scoreboard.deactivate()

    def deactivate_initial_widgets(self):
        self.input_message.deactivate()
        self.name_prompt.deactivate()
        self.score.deactivate()

    def run_command(self, evt):
        if evt.command == "reset":
            self.deactivate()
        if self.active and evt.command == "advance":
            if self.name_prompt.active:
                self.deactivate_initial_widgets()
                self.update()
                self.get_high_scores().add(self.name_prompt.get_initials())
                self.scoreboard.activate()
            else:
                self.get_input().post_command("reset")

    def set_background(self):
        path = self.get_resource("ending-bg-path")
        background = image.load(path).convert()
        self.background = background

    def set_toy(self):
        toy = Toy(self)
        config = self.get_configuration()
        toy.resize(config["ending-toy-size"])
        toy.place(config["ending-toy-position"])
        toy.show()
        self.toy = toy

    def start_music(self):
        path = self.get_resource("ending-audio-path")
        self.get_audio().play_bgm(path)

    def update(self):
        self.clear()
        self.score.update()
        self.name_prompt.update()
        self.scoreboard.update()
        self.draw()

    def clear(self):
        self.blit(self.background, (0, 0))

    def draw(self):
        self.display_surface.blit(self, (0, 0))
3.16.203.151
3.16.203.151
3.16.203.151
 
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.