view gamelib/sound.py @ 96:a851461d345e

Iterate over a copy of the loop when changing it
author Neil Muller <drnlmuller@gmail.com>
date Wed, 02 Sep 2009 11:40:51 +0000
parents 23a8b2e49e9f
children 529a4d41c67a
line wrap: on
line source

import os
import pygame

import data
import constants

SOUND_INITIALIZED = False

def init_sound():
    """initialize the sound system"""
    global SOUND_INITIALIZED
    try:
        pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER)
        SOUND_INITIALIZED = True
    except pygame.error, exc:
        print >>sys.stderr, "Could not initialize sound system: %s" % exc

def play_sound(filename):
    """plays the sound with the given filename from the data sounds directory"""
    if not SOUND_INITIALIZED:
        return
    file_path = data.filepath("sounds", filename)
    if not os.path.exists(file_path):
        return
    pygame.mixer.music.load(file_path)
    pygame.mixer.music.play()