- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试一个奇怪的想法,就是那天我得到了,我想知道是否有人知道解决我遇到的问题的方法
因此,我想做的是将一系列声音循环播放一次,然后移动到下一个声音,然后再循环播放另一次随机播放的声音,然后继续播放下一个声音,并继续执行此操作,假设还有其他100种声音?
这就像是按固定顺序激活的预定声音列表,但是每种声音都有一个“循环”变量,该变量本质上是两个数字之间的随机整数,用于确定在进入下一个声音之前该声音循环多少次。
我之所以制作此动画,是因为我精于解释事物。
我知道这听起来很愚蠢,毫无意义并且有点疯狂,但是正如我所说的,这是一个实验。
看起来很容易,所以我尝试了这个:
(顺便说一句,我正在使用Tkinter和pygame)
def activate():
random_number_1 = randint(0, 3)
sound1 = pygame.mixer.Sound("sound2.wav")
pygame.mixer.Sound.play(sound1, random_number_1)
random_number_2 = randint(0, 3)
sound2 = pygame.mixer.Sound("sound3.wav")
pygame.mixer.Sound.play(sound2, random_number_2)
random_number_3 = randint(0, 3)
sound3 = pygame.mixer.Sound("sound4.wav")
pygame.mixer.Sound.play(sound3, random_number_3)
random_number_4 = randint(0, 3)
sound4 = pygame.mixer.Sound("sound5.wav")
pygame.mixer.Sound.play(sound4, random_number_4)
random_number_5 = randint(0, 3)
sound5 = pygame.mixer.Sound("sound6.wav")
pygame.mixer.Sound.play(sound5, random_number_5)
button = Button(root, text="button", command=activate)
button.pack()
def activate():
randomnumber1 = randint(0, 3)
for i in range(randomnumber1):
print(1)
randomnumber2 = randint(0, 3)
for i in range(randomnumber2):
print(2)
randomnumber3 = randint(0, 3)
for i in range(randomnumber3):
print(3)
randomnumber4 = randint(0, 3)
for i in range(randomnumber4):
print(4)
def activate():
randomnumber1 = randint(0, 3)
for i in range(randomnumber1):
sound1 = pygame.mixer.Sound("sound2.wav")
pygame.mixer.Sound.play(sound1)
randomnumber2 = randint(0, 3)
for i in range(randomnumber2):
sound2 = pygame.mixer.Sound("sound3.wav")
pygame.mixer.Sound.play(sound2)
randomnumber3 = randint(0, 3)
for i in range(randomnumber3):
sound3 = pygame.mixer.Sound("sound4.wav")
pygame.mixer.Sound.play(sound3)
randomnumber4 = randint(0, 3)
for i in range(randomnumber4):
sound4 = pygame.mixer.Sound("sound5.wav")
pygame.mixer.Sound.play(sound4)
def activate():
randomnumber1 = randint(0, 3)
for i in range(randomnumber1):
winsound.PlaySound("sound2.wav", winsound.SND_ASYNC)
randomnumber2 = randint(0, 3)
for i in range(randomnumber2):
winsound.PlaySound("sound3.wav", winsound.SND_ASYNC)
randomnumber3 = randint(0, 3)
for i in range(randomnumber3):
winsound.PlaySound("sound4.wav", winsound.SND_ASYNC)
randomnumber4 = randint(0, 3)
for i in range(randomnumber4):
winsound.PlaySound("sound5.wav", winsound.SND_ASYNC)
random_number_1 = randint(0, 3)
sound1 = pygame.mixer.Sound("sound2.wav")
pygame.mixer.Sound.play(sound1, random_number_1)
time.sleep(random_number_1*1.5)
最佳答案
一种简单的方法是使用pygame.mixer.Channel()
函数set_endevent()
。
channel2 = pygame.mixer.Channel(1)
channel2.set_endevent( pygame.USEREVENT+1 )
pygame.USEREVENT+1
)。
pygame.USEREVENT+1
事件后,立即会发出新声音。
import pygame
import random
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
DARK_BLUE = ( 3, 5, 54)
### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Random Sound")
### sound
# create separate Channel objects for simultaneous playback
channel1 = pygame.mixer.Channel(0) # argument must be int
channel2 = pygame.mixer.Channel(1)
# define the event that's sent when a sound stops playing:
channel2.set_endevent( pygame.USEREVENT+1 )
# Rain sound from: https://www.freesoundslibrary.com/sound-of-rain-falling-mp3/ (CC BY 4.0)
rain_sound = pygame.mixer.Sound( 'rain-falling.ogg' )
channel1.play( rain_sound, -1 ) # loop the rain sound forever
# and ... All are (CC BY 4.0)
# https://www.freesoundslibrary.com/car-horn-sound-effect/
# https://www.freesoundslibrary.com/duck-quack/
# https://www.freesoundslibrary.com/cash-register-sound-effect/
# https://www.freesoundslibrary.com/turkey-gobble-call/
# https://www.freesoundslibrary.com/single-ding-sound-effect/
# https://www.freesoundslibrary.com/dog-bark-sound-effect/
#
horn = pygame.mixer.Sound( 'car-horn2.ogg' ) # converted from MP3 to OGG
quack = pygame.mixer.Sound( 'duck-quack.ogg' )
ca_ching = pygame.mixer.Sound( 'cash-register.ogg' )
bark = pygame.mixer.Sound( 'dog-bark.ogg' )
ding = pygame.mixer.Sound( 'single-ding.ogg' )
gobble = pygame.mixer.Sound( 'turkey-gobble.ogg' )
# Sound list
all_sounds = [ horn, quack, ca_ching, bark, ding, gobble ]
# Start with a random sound
channel2.play( random.choice( all_sounds ) )
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.USEREVENT+1 ):
# Channel2 sound ended, start another!
channel2.play( random.choice( all_sounds ) )
print( "Sound ended" )
# Update the window, but not more than 60fps
window.fill( DARK_BLUE )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
# Sound list
all_sounds = [ horn, quack, ca_ching, bark, ding, gobble ]
# Start with the first sound
channel2.play( all_sounds[ 0 ] )
current_sound_index = 0
...
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.USEREVENT+1 ):
# Channel2 sound ended, start the next sound!
current_sound_index += 1
if ( current_sound_index < len( all_sounds ) ):
channel2.play( all_sounds[ current_sound_index ] )
print( "Sound ended" )
else:
print( "Played all sounds" )
关于python - 如何使用pygame依次运行声音循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61067003/
Surface.blit在 1.8 中有一个新参数:混合。定义了以下值: BLEND_ADD BLEND_SUB BLEND_MULT BLEND_MIN BLEND_MAX BLEND_RGBA_A
import sys import pygame import pygame.locals as pgl class Test: def __init__(self): pyg
我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示表示鼠标在屏幕上的位置的字符串。 import pygame, sys from pygame.locals import * pyga
我有总是在后台运行的音乐和一些在触发时会播放声音的事件。音乐效果很好。 pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.
我有这些代码 FONT = pygame.font.Font("font/calibri.ttf", 50) FONT.size = 25 但是编译器说 AttributeError: 'pygame
有我正在导入的图像: look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha() 我试图减少它的大小是这样的: pygame.
我正在为我的 pygame 制作一个帮助屏幕,每当我运行它时,我都会收到此错误消息: > self.surface.blit(self.helpscreen) TypeError: argument
在 pyGame 中应用程序,我想渲染 SVG 中描述的无分辨率 GUI 小部件。 我怎样才能做到这一点? (我喜欢 OCEMP GUI 工具包,但它的渲染似乎依赖于位图) 最佳答案 这是一个完整的例
有没有办法将多首歌曲加载到 Pygame 中?我不是在谈论这样的音效; crash_sound = pygame.mixer.Sound("crash.ogg") #and pygame.mixer.
我还有一个问题。当我尝试运行我的代码时,pygame 启动然后立即停止。 这是我的代码: import pygame import os import time import random pygam
我正在使用 pymunk 和 pygame 开发一个项目。我正在使用 PivotJoint 约束将我的 body 连接在一起。如果可能的话,我想让关节不可见 - 有什么办法可以做到这一点吗?现在关节在
我使用 fedora 20、Python 2.7 和 virtualenv 1.10.1。我想在 virtualenv 中安装 pygame,我得到了 You are installing a pot
尝试将文本添加到矩形中并使用箭头键在屏幕上移动矩形。我想让文字不会超出边缘。到目前为止,我已经在没有将其放入 Rect 中的情况下工作了,但我想让 Rect 函数工作。现在文本只是反弹回来,我不知道要
我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我... 我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surfa
我在 Windows 10 机器上运行了 python 3.9.1。我通过 pip 在我的机器上安装了 pygame 2.0.1 (python -m pip install https://gith
错误: File "/home/alien/cncell/core/animator.py", line 413, in create_animation_from_data pygame
这是代码。 5000 个弹跳旋转的红色方块。 (16x16 png) 在 pygame 版本上,我获得 30 fps,但使用 pyglet 获得 10 fps。对于这种事情,OpenGl 不应该更快吗
我以为 pygame.font.Font 是用来加载 .ttf 字体的,如果没有 .ttf 文件在同一个目录下就无法加载字体,但我看过一个视频,有人在没有 .ttf 文件的情况下加载字体。 ttf 字
我正在尝试使用 Travis CI 设置一个项目。项目也使用 pygame。我曾多次尝试设置它 - 但它似乎失败了。 我得到的最接近的是以下内容: .travis.yml : language: py
这个问题已经有答案了: Python error "ImportError: No module named" (38 个回答) 已关闭 3 年前。 我正在使用 Mac 并输入 pip install
我是一名优秀的程序员,十分优秀!