本文整理汇总了Python中pygame.mixer.Sound类的典型用法代码示例。如果您正苦于以下问题:Python Sound类的具体用法?Python Sound怎么用?Python Sound使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Sound类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SoundDecorator
class SoundDecorator(FrameDecorator):
def __init__(self, parent_component=NullComponent(), sound_file_path=''):
self.sound_file_path = sound_file_path
self.parent = parent_component
self.sound = Sound(sound_file_path)
self.start_time = 0
def get_parent(self):
return self.parent
def set_parent(self, new_parent):
self.parent = new_parent
def get_landmarks(self):
self.parent.get_landmarks()
def get_image(self):
self.play()
return self.parent.get_image()
def play(self):
if not self.is_playing():
self.start_time = time()
self.sound.play()
def is_playing(self):
now = time()
return self.sound.get_length() > now - self.start_time
def stop(self):
self.sound.stop()
self.start_time = 0
def copy(self):
return SoundDecorator(parent_component=self.parent.copy(), sound_file_path=self.sound_file_path)
示例2: setup
def setup(self, all_sprites):
all_sprites.add(LcarsBackgroundImage("assets/lcars_screen_2.png"),
layer=0)
all_sprites.add(LcarsText(colours.ORANGE, (270, -1), "AUTHORIZATION REQUIRED", 2),
layer=1)
all_sprites.add(LcarsText(colours.BLUE, (330, -1), "ONLY AUTHORIZED PERSONNEL MAY ACCESS THIS TERMINAL", 1.5),
layer=1)
all_sprites.add(LcarsText(colours.BLUE, (360, -1), "TOUCH TERMINAL TO PROCEED", 1.5),
layer=1)
all_sprites.add(LcarsText(colours.BLUE, (390, -1), "FAILED ATTEMPTS WILL BE REPORTED", 1.5),
layer=1)
all_sprites.add(LcarsGifImage("assets/gadgets/stlogorotating.gif", (103, 369), 50), layer=1)
# sounds
Sound("assets/audio/panel/215.wav").play()
Sound("assets/audio/enter_authorization_code.wav").play()
self.sound_granted = Sound("assets/audio/accessing.wav")
self.sound_beep1 = Sound("assets/audio/panel/206.wav")
self.sound_denied = Sound("assets/audio/access_denied.wav")
self.sound_deny1 = Sound("assets/audio/deny_1.wav")
self.sound_deny2 = Sound("assets/audio/deny_2.wav")
self.attempts = 0
self.granted = False
示例3: LcarsButton
class LcarsButton(LcarsWidget):
"""Button - either rounded or rectangular if rectSize is spcified"""
def __init__(self, colour, pos, text, handler=None, rectSize=None):
if rectSize == None:
image = pygame.image.load("assets/button.png").convert()
size = (image.get_rect().width, image.get_rect().height)
else:
size = rectSize
image = pygame.Surface(rectSize).convert()
image.fill(colour)
self.colour = colour
self.image = image
font = Font("assets/swiss911.ttf", 19)
textImage = font.render(text, False, colours.BLACK)
image.blit(textImage,
(image.get_rect().width - textImage.get_rect().width - 10,
image.get_rect().height - textImage.get_rect().height - 5))
LcarsWidget.__init__(self, colour, pos, size, handler)
self.applyColour(colour)
self.highlighted = False
self.beep = Sound("assets/audio/panel/202.wav")
def handleEvent(self, event, clock):
if (event.type == MOUSEBUTTONDOWN and self.rect.collidepoint(event.pos)):
self.applyColour(colours.WHITE)
self.highlighted = True
self.beep.play()
if (event.type == MOUSEBUTTONUP and self.highlighted):
self.applyColour(self.colour)
return LcarsWidget.handleEvent(self, event, clock)
示例4: LcarsButton
class LcarsButton(LcarsWidget):
def __init__(self, colour, pos, text, handler=None):
self.handler = handler
image = pygame.image.load("assets/button.png").convert()
size = (image.get_rect().width, image.get_rect().height)
font = Font("assets/swiss911.ttf", 19)
textImage = font.render(text, False, colours.BLACK)
image.blit(textImage,
(image.get_rect().width - textImage.get_rect().width - 10,
image.get_rect().height - textImage.get_rect().height - 5))
self.image = image
self.colour = colour
LcarsWidget.__init__(self, colour, pos, size)
self.applyColour(colour)
self.highlighted = False
self.beep = Sound("assets/audio/panel/202.wav")
def handleEvent(self, event, clock):
handled = False
if (event.type == MOUSEBUTTONDOWN and self.rect.collidepoint(event.pos)):
self.applyColour(colours.WHITE)
self.highlighted = True
self.beep.play()
handled = True
if (event.type == MOUSEBUTTONUP and self.highlighted):
self.applyColour(self.colour)
if self.handler:
self.handler(self, event, clock)
handled = True
LcarsWidget.handleEvent(self, event, clock)
return handled
示例5: _make_sample
def _make_sample(slot):
"""
Makes Sound instances tuple and sets Sound volumes
"""
global SAMPLES_DIR
sample_file = os.path.join(SAMPLES_DIR, slot['sample'])
sample = Sound(sample_file)
sample.set_volume(slot.get('volume', 100) / 100.0)
return sample
示例6: __init__
class Audio:
PLAY = 0
PAUSE = 1
STOP = 2
GRAVITY = 0
DEATH = 1
def __init__(self):
mixer.init(frequency=44100, size=-16, channels=4, buffer=128)
self._background_s = Sound(os.path.join("res", "background.ogg"))
self._background_s.set_volume(0.8)
self._grav_fxs_s = [
Sound(os.path.join("res", "grav1.wav")),
Sound(os.path.join("res", "grav2.wav")),
Sound(os.path.join("res", "grav3.wav")),
]
for s in self._grav_fxs_s:
s.set_volume(0.1)
self._death_s = Sound(os.path.join("res", "death.wav"))
self._background_channel = mixer.Channel(0)
self._fx_channel = mixer.Channel(1)
self.effect_playing = False
self.bg_playing = False
def bg(self, state=0):
if state == self.PLAY:
if self._background_channel.get_sound() == None:
self._background_channel.play(self._background_s, loops=-1, fade_ms=250)
else:
self._background_channel.unpause()
self.bg_playing = True
elif state == self.PAUSE:
self._background_channel.pause()
self.bg_playing = False
else:
self._background_channel.stop()
self.bg_playing = False
def fx(self, state=0, fx=0):
if state == self.PLAY:
if fx == self.GRAVITY:
if self._fx_channel.get_sound() not in self._grav_fxs_s:
self._fx_channel.stop()
self._fx_channel.play(choice(self._grav_fxs_s), loops=0, fade_ms=0)
else:
if self._fx_channel.get_sound() != self._death_s:
self._fx_channel.stop()
self._fx_channel.play(self._death_s, loops=0, fade_ms=0)
elif state == self.PAUSE:
self._fx_channel.pause()
else:
self._fx_channel.stop()
示例7: ScreenAuthorize
class ScreenAuthorize(LcarsScreen):
def setup(self, all_sprites):
all_sprites.add(LcarsBackgroundImage("assets/lcars_screen_2.png"),
layer=0)
all_sprites.add(LcarsText(colours.ORANGE, (270, -1), "AUTHORIZATION REQUIRED", 2),
layer=1)
all_sprites.add(LcarsText(colours.BLUE, (330, -1), "ONLY AUTHORIZED PERSONNEL MAY ACCESS THIS TERMINAL", 1.5),
layer=1)
all_sprites.add(LcarsText(colours.BLUE, (360, -1), "TOUCH TERMINAL TO PROCEED", 1.5),
layer=1)
all_sprites.add(LcarsText(colours.BLUE, (390, -1), "FAILED ATTEMPTS WILL BE REPORTED", 1.5),
layer=1)
all_sprites.add(LcarsGifImage("assets/gadgets/stlogorotating.gif", (103, 369), 50), layer=1)
# sounds
Sound("assets/audio/panel/215.wav").play()
Sound("assets/audio/enter_authorization_code.wav").play()
self.sound_granted = Sound("assets/audio/accessing.wav")
self.sound_beep1 = Sound("assets/audio/panel/206.wav")
self.sound_denied = Sound("assets/audio/access_denied.wav")
self.sound_deny1 = Sound("assets/audio/deny_1.wav")
self.sound_deny2 = Sound("assets/audio/deny_2.wav")
self.attempts = 0
self.granted = False
def handleEvents(self, event, fpsClock):
LcarsScreen.handleEvents(self, event, fpsClock)
if event.type == pygame.MOUSEBUTTONDOWN:
if (self.attempts > 1):
self.granted = True
self.sound_beep1.play()
else:
if self.attempts == 0: self.sound_deny1.play()
else: self.sound_deny2.play()
self.granted = False
self.attempts += 1
if event.type == pygame.MOUSEBUTTONUP:
if (self.granted):
self.sound_granted.play()
from screens.main import ScreenMain
self.loadScreen(ScreenMain())
else:
self.sound_denied.play()
return False
示例8: play_wav
def play_wav(name, *args):
"""This is a convenience method to play a wav.
*args
These are passed to play.
Return the sound object.
"""
s = Sound(filepath(name))
s.play(*args)
return s
示例9: show
def show(self):
# Initialize options
font = resources.get_font('prstartcustom.otf')
self.options.init(font, 15, True, MORE_WHITE)
# Initialize sounds
self.select_sound = Sound(resources.get_sound('menu_select.wav'))
示例10: show
def show(self):
# Start the music
pygame.mixer.music.load(resources.get_music('whatislove.ogg'))
pygame.mixer.music.play(-1)
# Get font name
font = resources.get_font('prstartcustom.otf')
# Make Hi-score and rights
font_renderer = pygame.font.Font(font, 12)
self.hiscore_label_surface = font_renderer.render('Hi-score', True, NOT_SO_BLACK)
self.hiscore_surface = font_renderer.render(self.hiscore, True, NOT_SO_BLACK)
self.rights_surface = font_renderer.render(self.rights, True, NOT_SO_BLACK)
# Make title
font_renderer = pygame.font.Font(font, 36)
self.title_surface = font_renderer.render(GAME_TITLE, False, NOT_SO_BLACK)
# Make all options and change to the main menu
self.play_menu_options.init(font, 15, True, NOT_SO_BLACK)
self.main_menu_options.init(font, 15, True, NOT_SO_BLACK)
self.change_menu_options(self.main_menu_options)
# Load all sounds
self.select_sound = Sound(resources.get_sound('menu_select.wav'))
示例11: __init__
def __init__(self):
"""Declare and initialize instance variables."""
self.is_running = False
self.voice = Sound(VOICE_PATH)
self.voice_duration = (self.voice.get_length() * FRAME_RATE)
self.voice_timer = 0
self.voice_has_played = False
示例12: __init__
class Bass:
__author__ = 'James Dewes'
def __init__(self):
try:
import pygame.mixer
from pygame.mixer import Sound
except RuntimeError:
print("Unable to import pygame mixer sound")
raise Exception("Unable to import pygame mixer sound")
self.soundfile = "launch_bass_boost_long.wav"
self.mixer = pygame.mixer.init() #instance of pygame
self.bass = Sound(self.soundfile)
def start(self):
state = self.bass.play()
while state.get_busy() == True:
continue
def stop(self):
try:
Pass
except Exception:
raise Exception("unable to stop")
示例13: __init__
def __init__(self, pos, color_interval, input):
PhysicsObject.__init__(self, pos, (0, 0), (28, 48), BODY_DYNAMIC)
self.change_color_mode = False
self.color_interval = 0
self.color_timer_text = None
if color_interval > 0:
self.change_color_mode = True
self.color_interval = color_interval
font = Font('./assets/font/vcr.ttf', 18)
self.color_timer_text = Text(font, str(color_interval), (740, 5))
self.color_timer_text.update = True
self.color_timer = 0.0
self.input = input
self.sprite = Sprite("./assets/img/new_guy.png", (64, 64), (1.0 / 12.0))
self.set_foot(True)
self.active_color = 'red'
self.acceleration = 400
self.dead = False
self.sound_jump = Sound('./assets/audio/jump.wav')
self.sound_land = Sound('./assets/audio/land.wav')
self.sound_push = Sound('./assets/audio/push.wav')
self.sound_timer = 0.0
self.sound_min_interval = 0.5
self.id = ID_PLAYER
self.bounce_timer = 0.0
# view rectangle for HUD stuff
self.view_rect = Rect(0, 0, 0, 0)
示例14: show
def show(self):
font = resources.get_font('prstartcustom.otf')
# Make title
font_renderer = pygame.font.Font(font, 15)
self.title_surface = font_renderer.render('Hi-scores', True, NOT_SO_BLACK)
# Make all scores
# Get the score with highest width
max_width_score = max(self.scores, key=self.score_width)
# Calculate its width, and add 4 dots
max_width = self.score_width(max_width_score) + 4
font_renderer = pygame.font.Font(font, 12)
for score in self.scores:
self.scores_surfaces.append(
font_renderer.render(
score.name + '.' * (max_width - self.score_width(score)) + str(score.score),
True,
NOT_SO_BLACK
)
)
# Make the back option
self.back_options.init(font, 15, True, NOT_SO_BLACK)
# Load all sounds
self.select_sound = Sound(resources.get_sound('menu_select.wav'))
示例15: PauseState
class PauseState(GameState):
CONTINUE_OPTION = 0
RESTART_OPTION = 1
EXIT_OPTION = 2
def __init__(self, game, restart_state):
super(PauseState, self).__init__(game)
self.listen_keys = (pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN, pygame.K_RETURN)
self.restart_state = restart_state
self.options = VerticalMenuOptions(
['Continue', 'Restart', 'Exit'],
self.on_click,
self.on_change,
)
self.select_sound = None
def show(self):
# Initialize options
font = resources.get_font('prstartcustom.otf')
self.options.init(font, 15, True, MORE_WHITE)
# Initialize sounds
self.select_sound = Sound(resources.get_sound('menu_select.wav'))
def update(self, delta):
self.options.update(self.input)
def render(self, canvas):
canvas.fill(NOT_SO_BLACK)
self.options.render(canvas, GAME_WIDTH / 2, GAME_HEIGHT / 2 - self.options.get_height() / 2)
def on_click(self, option):
self.select_sound.play()
if option == PauseState.CONTINUE_OPTION:
self.state_manager.pop_overlay()
elif option == PauseState.RESTART_OPTION:
self.state_manager.set_state(self.restart_state)
elif option == PauseState.EXIT_OPTION:
self.state_manager.set_state(MenuState(self.game))
def on_change(self, old_option, new_option):
self.select_sound.play()
def dispose(self):
pass