有没有办法在Pygame Zero中改变声音的音量?

huangapple go评论74阅读模式
英文:

Is there a way to change volume of sounds in Pygame Zero?

问题

我正在全面完善一款我根据教程制作的游戏,并一直试图将音效整合到成品中。然而,它们声音太大了,我似乎找不到减小音量的方法。我知道在普通的pygame中有一种方法,但我不知道在pgzero中是否有类似的方法。任何帮助将不胜感激 有没有办法在Pygame Zero中改变声音的音量?

以下是一部分代码,希望有所帮助:

if gem.colliderect(ship):
        sounds.gem_collect.play()
        gem.x = random.randint(20, 1180)
        gem.y = 0
        score = score + 1
英文:

I'm working on fully fleshing out a game I made following a tutorial, and have been trying to incorporate sound effects into the finished thing. However they are way too loud and I can't seem to find a way to quieten them. I know there's a way in normal pygame but I don't know if there's one for pgzero. Any help would be greatly appreciated 有没有办法在Pygame Zero中改变声音的音量?

Here's a snippet of code in case that helps:

if gem.colliderect(ship):
        sounds.gem_collect.play()
        gem.x = random.randint(20, 1180)
        gem.y = 0
        score = score + 1

答案1

得分: 0

我从未使用过pygame zero。但是这里有文档。文档中没有提到如何设置声音对象的音量,但你可以为音乐使用set_volume()

music.set_volume(volume)

设置音乐系统的音量。这需要一个介于0(静音)和1(最大音量)之间的数字。

你可以尝试在声音对象上使用set_volume()方法,看看会发生什么。也许只是文档没有很好地记录。

if gem.colliderect(ship):
    sounds.gem_collect.set_volume(0.2)
    sounds.gem_collect.play()
    gem.x = random.randint(20, 1180)
    gem.y = 0
    score = score + 1
英文:

I have never used pygame zero. But here is the documentation. Doesn't say anything about setting volume for sound objects, but you can set_volume() for music:

> music.set_volume(volume)
>
> Set the volume of the music system.
> This takes a number between 0 (meaning silent) and 1 (meaning full volume).

You could try to use the set_volume() method on a sound object and see what happens. Maybe it's just not documented well.

if gem.colliderect(ship):
    sounds.gem_collect.set_volume(0.2)
    sounds.gem_collect.play()
    gem.x = random.randint(20, 1180)
    gem.y = 0
    score = score + 1

答案2

得分: 0

在Pygame Zero的文档中,我没有找到使用sounds控制音量的方法,但你可以在Music中进行更改。

if gem.colliderect(ship):
    music.play_once(track_name)  # 你不需要在其中添加音轨扩展名
    music.set_volume(num)  # num是0(表示静音)到1(表示最大音量)之间的任何数字
    gem.x = random.randint(20, 1180)
    gem.y = 0
    score = score + 1

注意
这个Music API是实验性的,并且有它自己的局限性,只在1.1版本及以后可用。一些局限性包括:

  • 在一些Linux发行版上可能无法使用MP3格式。
  • 一些OGG Vorbis文件似乎会使Pygame占用100%的CPU而挂起。

更多关于Pygame Zero中的Music的信息,请参考链接

英文:

In Pygame zero's documentation I didn't found a way to control volume using sounds but you can change it in Music.

if gem.colliderect(ship):
        music.play_once(track_name) # you don't need to add track extenstion in it
        music.set_volume(num) # num is any number between 0 (meaning silent) and 1 (meaning full volume)
        gem.x = random.randint(20, 1180)
        gem.y = 0
        score = score + 1

Attention
This Music api is experimental and have it's own drawbacks and is only availbale in 1.1 and after. Some of the Drawbacks

  • MP3 may not be available on some Linux distributions.

  • Some OGG Vorbis files seem to hang Pygame with 100% CPU.

more about Music in pygame zero link

huangapple
  • 本文由 发表于 2023年7月27日 19:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定