本文整理汇总了Python中pygame.mixer.Sound.play方法的典型用法代码示例。如果您正苦于以下问题:Python Sound.play方法的具体用法?Python Sound.play怎么用?Python Sound.play使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.mixer.Sound
的用法示例。
在下文中一共展示了Sound.play方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SoundDecorator
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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: devil
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
def devil(self):
self.lightning.set_A(ON)
self.lightning.set_C(ON)
self.lightning.send()
poodle = Sound("%s/poodle.wav" % (self._data_folder))
poodle.play()
time.sleep(0.3)
self.lightning.set_A(OFF)
self.lightning.send()
time.sleep(0.7)
for i in range(10):
wait = random.randint(0, 3)
while self._last == wait:
wait = random.randint(0, 3)
print "devil intensity:", wait
self._last = wait
thunder = Sound("%s/thunder_0%d.wav" % (self._data_folder, wait))
thunder.play()
if wait < 2:
off_for = 0.05 + random.random() * 0.3
print "flicker for", off_for
self.lightning.set_B(OFF)
self.lightning.send()
time.sleep(off_for)
self.lightning.set_B(ON)
self.lightning.send()
time.sleep(2)
self.lightning.set_C(OFF)
self.lightning.send()
time.sleep(1)
self.lightning.set_B(ON)
self.lightning.send()
示例3: thunder
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
def thunder(self, inc):
wait = random.randint(0, 2+inc)
while (self._last == wait):
wait = random.randint(0, 2+inc)
self._last = wait
print "intensity:", wait
thunder = Sound("%s/thunder_0%d.wav" % (self._data_folder, wait))
if wait < 6:
self.lightning.set_C(ON)
self.lightning.send()
time.sleep(random.uniform(0.5, 1.5))
self.lightning.set_C(OFF)
self.lightning.send()
time.sleep(wait)
if wait < 6:
self.lightning.set_B(OFF)
self.lightning.send()
thunder.play()
if wait < 6:
time.sleep(0.3)
self.lightning.set_B(ON)
self.lightning.send()
time.sleep(thunder.get_length()-0.3)
thunder.fadeout(200)
time.sleep(wait)
示例4: LcarsButton
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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)
示例5: LcarsButton
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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
示例6: ScreenAuthorize
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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
示例7: play_wav
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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
示例8: __init__
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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")
示例9: PauseState
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
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
示例10: LcarsElbow
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
class LcarsElbow(LcarsWidget):
"""The LCARS corner elbow - not currently used"""
#STYLE_BOTTOM_LEFT = 0
#STYLE_TOP_LEFT = 1
#STYLE_BOTTOM_RIGHT = 2
#STYLE_TOP_RIGHT = 3
def __init__(self, colour, pos, text, handler=None):
self.handler = handler
image = pygame.image.load("assets/elbow.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 - 219,
image.get_rect().height - textImage.get_rect().height - 35))
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
示例11: __init__
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
class Sound:
"""Class wrapping ``pygame.mixer.Sound`` with the ability to enable/disable sound globally
Use this instead of ``pygame.mixer.Sound``. The interface is fully transparent.
"""
def __init__(self, source):
if config.SOUND:
self.sound = OldSound(source)
def play(self, loops=0, maxtime=0, fade_ms=0):
if config.SOUND:
self.sound.play(loops, maxtime, fade_ms)
def stop(self):
if config.SOUND:
self.sound.stop()
def fadeout(self, time):
if config.SOUND:
self.sound.fadeout(time)
def set_volume(self, value):
if config.SOUND:
self.sound.set_volume(value)
def get_volume(self):
if config.SOUND:
return self.sound.get_volume()
def get_num_channels(self):
if config.SOUND:
return self.sound.get_num_channels()
def get_length(self):
if config.SOUND:
return self.get_length()
def get_raw(self):
if config.SOUND:
return self.sound.get_raw()
示例12: MotionSensor
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
import pygame.mixer
from pygame.mixer import Sound
import time
from gpiozero import MotionSensor
from gpiozero import Button
from signal import pause
pygame.mixer.init()
pir = MotionSensor(4)
button = Button(2)
drum = Sound("test/ka_ru_na_2.wav")
while True:
button.when_pressed = drum.play
time.sleep(4)
if pir.motion_detected:
drum.play()
print("Motion Detect!: !")
time.sleep(4)
示例13: MenuState
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
class MenuState(GameState):
PLAY_OPTION = 0
HISCORES_OPTION = 1
EXIT_OPTION = 2
ONE_PLAYER_OPTION = 0
TWO_PLAYERS_OPTION = 1
BACK_OPTION = 2
KONAMI_CODE = (pygame.K_UP, pygame.K_UP, pygame.K_DOWN, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT,
pygame.K_LEFT, pygame.K_RIGHT, pygame.K_b, pygame.K_a)
def __init__(self, game):
super(MenuState, self).__init__(game)
# Listen to up, down, and enter and all of the Konami code ones
self.set_listen_keys((pygame.K_DOWN, pygame.K_UP, pygame.K_RETURN) + MenuState.KONAMI_CODE)
# Konami code step
self.konami_code_step = 0
# Model of the menu
self.title = GAME_TITLE
self.selected = 0
self.hiscore = str(self.game.hiscores.get_hiscore().name) + " - " + str(self.game.hiscores.get_hiscore().score)
self.rights = u'\u00a9 Dezassete'
self.current_menu_options = None
self.main_menu_options = VerticalMenuOptions(
['Play', 'Hi-scores', 'Exit'],
self.main_menu_on_click,
self.on_change
)
self.play_menu_options = VerticalMenuOptions(
['1 Player', '2 Players', 'Back'],
self.play_menu_on_click,
self.on_change
)
# Surfaces
self.hiscore_surface = None
self.hiscore_label_surface = None
self.title_surface = None
self.rights_surface = None
# Sounds
self.select_sound = None
def set_listen_keys(self, listen_keys_tuple):
listen_keys_list = []
for key in listen_keys_tuple:
if key not in listen_keys_list:
listen_keys_list.append(key)
self.listen_keys = tuple(listen_keys_list)
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'))
def update(self, delta):
# Konami code listener
for key in self.listen_keys:
if self.input.key_clicked(key):
if MenuState.KONAMI_CODE[self.konami_code_step] == key:
self.konami_code_step += 1
if self.konami_code_step >= len(MenuState.KONAMI_CODE):
from state.god_state import GodState
self.state_manager.set_state(GodState(self.game))
self.konami_code_step = 0
return
else:
self.konami_code_step = 0
self.current_menu_options.update(self.input)
def render(self, canvas):
canvas.fill(NOT_SO_WHITE)
# Render hiscore
canvas.blit(self.hiscore_label_surface, (GAME_WIDTH / 2 - self.hiscore_label_surface.get_width() / 2, 15))
canvas.blit(self.hiscore_surface, (
#.........这里部分代码省略.........
示例14: ScreenAuthorize
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
class ScreenAuthorize(LcarsScreen):
def setup(self, all_sprites):
all_sprites.add(LcarsBackgroundImage("assets/lcars_screen_2.png"),
layer=0)
all_sprites.add(LcarsGifImage("assets/gadgets/stlogorotating.gif", (103, 369), 50),
layer=0)
all_sprites.add(LcarsText(colours.ORANGE, (270, -1), "AUTHORIZATION REQUIRED", 2),
layer=0)
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(LcarsButton(colours.GREY_BLUE, (320, 130), "1", self.num_1), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (370, 130), "2", self.num_2), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (320, 270), "3", self.num_3), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (370, 270), "4", self.num_4), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (320, 410), "5", self.num_5), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (370, 410), "6", self.num_6), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (320, 550), "7", self.num_7), layer=2)
all_sprites.add(LcarsButton(colours.GREY_BLUE, (370, 550), "8", self.num_8), layer=2)
self.layer1 = all_sprites.get_sprites_from_layer(1)
self.layer2 = all_sprites.get_sprites_from_layer(2)
# sounds
Sound("assets/audio/panel/215.wav").play()
self.sound_granted = Sound("assets/audio/accessing.wav")
self.sound_beep1 = Sound("assets/audio/panel/201.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")
############
# SET PIN CODE WITH THIS VARIABLE
############
self.pin = 1234
############
self.reset()
def reset(self):
# Variables for PIN code verification
self.correct = 0
self.pin_i = 0
self.granted = False
for sprite in self.layer1: sprite.visible = True
for sprite in self.layer2: sprite.visible = False
def handleEvents(self, event, fpsClock):
if event.type == pygame.MOUSEBUTTONDOWN:
# Play sound
self.sound_beep1.play()
if event.type == pygame.MOUSEBUTTONUP:
if (not self.layer2[0].visible):
for sprite in self.layer1: sprite.visible = False
for sprite in self.layer2: sprite.visible = True
Sound("assets/audio/enter_authorization_code.wav").play()
elif (self.pin_i == len(str(self.pin))):
# Ran out of button presses
if (self.correct == 4):
self.sound_granted.play()
from screens.main import ScreenMain
self.loadScreen(ScreenMain())
else:
self.sound_deny2.play()
self.sound_denied.play()
self.reset()
return False
def num_1(self, item, event, clock):
if str(self.pin)[self.pin_i] == '1':
self.correct += 1
self.pin_i += 1
def num_2(self, item, event, clock):
if str(self.pin)[self.pin_i] == '2':
self.correct += 1
self.pin_i += 1
def num_3(self, item, event, clock):
if str(self.pin)[self.pin_i] == '3':
self.correct += 1
self.pin_i += 1
def num_4(self, item, event, clock):
if str(self.pin)[self.pin_i] == '4':
self.correct += 1
#.........这里部分代码省略.........
示例15: SoundControl
# 需要导入模块: from pygame.mixer import Sound [as 别名]
# 或者: from pygame.mixer.Sound import play [as 别名]
class SoundControl(object):
def __init__(self, serial_port, data_folder):
self.lightning = SocketControl(serial_port)
self._data_folder = data_folder
self._rain = Sound("%s/rain.wav" % data_folder)
self._thunder = 0
self._last = 0
self.lightning.set_B(ON)
self.lightning.send()
def rain(self):
self._rain.play(-1)
def exit(self, time):
pygame.mixer.fadeout(time)
def smoke(self):
self.lightning.set_A(ON)
self.lightning.send()
time.sleep(0.3)
self.lightning.set_A(OFF)
self.lightning.send()
# 0 - 6
def thunder(self, inc):
wait = random.randint(0, 2+inc)
while (self._last == wait):
wait = random.randint(0, 2+inc)
self._last = wait
print "intensity:", wait
thunder = Sound("%s/thunder_0%d.wav" % (self._data_folder, wait))
if wait < 6:
self.lightning.set_C(ON)
self.lightning.send()
time.sleep(random.uniform(0.5, 1.5))
self.lightning.set_C(OFF)
self.lightning.send()
time.sleep(wait)
if wait < 6:
self.lightning.set_B(OFF)
self.lightning.send()
thunder.play()
if wait < 6:
time.sleep(0.3)
self.lightning.set_B(ON)
self.lightning.send()
time.sleep(thunder.get_length()-0.3)
thunder.fadeout(200)
time.sleep(wait)
def devil(self):
self.lightning.set_A(ON)
self.lightning.set_C(ON)
self.lightning.send()
poodle = Sound("%s/poodle.wav" % (self._data_folder))
poodle.play()
time.sleep(0.3)
self.lightning.set_A(OFF)
self.lightning.send()
time.sleep(0.7)
for i in range(10):
wait = random.randint(0, 3)
while self._last == wait:
wait = random.randint(0, 3)
print "devil intensity:", wait
self._last = wait
thunder = Sound("%s/thunder_0%d.wav" % (self._data_folder, wait))
thunder.play()
if wait < 2:
off_for = 0.05 + random.random() * 0.3
print "flicker for", off_for
self.lightning.set_B(OFF)
self.lightning.send()
time.sleep(off_for)
self.lightning.set_B(ON)
self.lightning.send()
time.sleep(2)
self.lightning.set_C(OFF)
self.lightning.send()
time.sleep(1)
self.lightning.set_B(ON)
self.lightning.send()