Translated

This commit is contained in:
2020-11-09 09:01:34 +08:00 committed by GitHub
parent 027395a187
commit 78ff15ce12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 166 additions and 166 deletions

View File

@ -1,166 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (robsean)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Add sound to your Python game)
[#]: via: (https://opensource.com/article/20/9/add-sound-python-game)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Add sound to your Python game
======
Hear what happens when your hero fights, jumps, collects loot, and more
by adding sounds to your game. Learn how in the 13th article in this
series on creating a platformer in Pygame.
![Colorful sound wave graph][1]
This is part 13 in an ongoing series about creating video games in [Python 3][2] using the [Pygame][3] module. Previous articles are:
1. [Learn how to program in Python by building a simple dice game][4]
2. [Build a game framework with Python using the Pygame module][5]
3. [How to add a player to your Python game][6]
4. [Using Pygame to move your game character around][7]
5. [What's a hero without a villain? How to add one to your Python game][8]
6. [Add platforms to your game][9]
7. [Simulate gravity in your Python game][10]
8. [Add jumping to your Python platformer game][11]
9. [Enable your Python game player to run forward and backward][12]
10. [Using Python to set up loot in Pygame][13]
11. [Add scorekeeping to your Python game][14]
12. [Add throwing mechanics to your Python game][15]
Pygame provides an easy way to integrate sounds into your Python video game. Pygame's [mixer module][16] can play one or more sounds on command, and by mixing those sounds together, you can have, for instance, background music playing at the same time you hear the sounds of your hero collecting loot or jumping over enemies.
It is easy to integrate the mixer module into an existing game, so—rather than giving you code samples showing you exactly where to put them—this article explains the four steps required to get sound in your application.
### Start the mixer
First, in your code's setup section, start the mixer process. Your code already starts Pygame and Pygame fonts, so grouping it together with these is a good idea:
```
pygame.init()
pygame.font.init()
pygame.mixer.init() # add this line
```
### Define the sounds
Next, you must define the sounds you want to use. This requires that you have the sounds on your computer, just as using fonts requires you to have fonts, and using graphics requires you to have graphics.
You also must bundle those sounds with your game so that anyone playing your game has the sound files.
To bundle a sound with your game, first create a new directory in your game folder, right along with the directory you created for your images and fonts. Call it `sound`:
```
`s = 'sound'`
```
Even though there are plenty of sounds on the internet, it's not necessarily _legal_ to download them and give them away with your game. It seems strange because so many sounds from famous video games are such a part of popular culture, but that's how the law works. If you want to ship a sound with your game, you must find an open source or [Creative Commons][17] sound that gives you permission to give the sound away with your game.
There are several sites that specialize in free and legal sounds, including:
* [Freesound][18] hosts sound effects of all sorts.
* [Incompetech][19] hosts background music.
* [Open Game Art][20] hosts some sound effects and music.
Some sound files are free to use only if you give the composer or sound designer credit. Read the conditions of use carefully before bundling any with your game! Musicians and sound designers work just as hard on their sounds as you work on your code, so it's nice to give them credit even when they don't require it.
To give your sound sources credit, list the sounds that you use in a text file called `CREDIT`, and place the text file in your game folder.
You might also try making your own music. The excellent [LMMS][21] audio workstation is easy to use and ships with lots of interesting sounds. It's available on all major platforms and exports to [Ogg Vorbis][22] (OGG) audio format.
### Add sound to Pygame
When you find a sound that you like, download it. If it comes in a ZIP or TAR file, extract it and move the sounds into the `sound` folder in your game directory.
If the sound file has a complicated name with spaces or special characters, rename it. The filename is completely arbitrary, and the simpler it is, the easier it is for you to type into your code.
Most video games use OGG sound files because the format provides high quality in small file sizes. When you download a sound file, it might be an MP3, WAVE, FLAC, or another audio format. To keep your compatibility high and your download size low, convert these to Ogg Vorbis with a tool like [fre:ac][23] or [Miro][24].
For example, assume you have downloaded a sound file called `ouch.ogg`.
In your code's setup section, create a variable representing the sound file you want to use:
```
`ouch = pygame.mixer.Sound(os.path.join(s, 'ouch.ogg'))`
```
### Trigger a sound
To use a sound, all you have to do is call the variable when you want to trigger it. For instance, to trigger the `OUCH` sound effect when your player hits an enemy:
```
for enemy in enemy_hit_list:
    pygame.mixer.Sound.play(ouch)
    score -= 1
```
You can create sounds for all kinds of actions, such as jumping, collecting loot, throwing, colliding, and whatever else you can imagine.
### Add background music
If you have music or atmospheric sound effects you want to play in your game's background, you can use the `music` function of Pygame's mixer module. In your setup section, load the music file:
```
`music = pygame.mixer.music.load(os.path.join(s, 'music.ogg'))`
```
And start the music:
```
`pygame.mixer.music.play(-1)`
```
The `-1` value tells Pygame to loop the music file infinitely. You can set it to anything from `0` and beyond to define how many times the music should loop before stopping.
### Enjoy the soundscapes
Music and sound can add a lot of flavor to your game. Try adding some to your Pygame project!
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/9/add-sound-python-game
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/colorful_sound_wave.png?itok=jlUJG0bM (Colorful sound wave graph)
[2]: https://www.python.org/
[3]: https://www.pygame.org/news
[4]: https://opensource.com/article/17/10/python-101
[5]: https://opensource.com/article/17/12/game-framework-python
[6]: https://opensource.com/article/17/12/game-python-add-a-player
[7]: https://opensource.com/article/17/12/game-python-moving-player
[8]: https://opensource.com/article/18/5/pygame-enemy
[9]: https://opensource.com/article/18/7/put-platforms-python-game
[10]: https://opensource.com/article/19/11/simulate-gravity-python
[11]: https://opensource.com/article/19/12/jumping-python-platformer-game
[12]: https://opensource.com/article/19/12/python-platformer-game-run
[13]: https://opensource.com/article/19/12/loot-python-platformer-game
[14]: https://opensource.com/article/20/1/add-scorekeeping-your-python-game
[15]: https://opensource.com/article/20/9/add-throwing-python-game
[16]: https://www.pygame.org/docs/ref/mixer.html
[17]: https://opensource.com/article/20/1/what-creative-commons
[18]: https://freesound.org
[19]: https://incompetech.filmmusic.io
[20]: https://opengameart.org
[21]: https://opensource.com/life/16/2/linux-multimedia-studio
[22]: https://en.wikipedia.org/wiki/Vorbis
[23]: https://www.freac.org/index.php/en/downloads-mainmenu-330
[24]: http://getmiro.com

View File

@ -0,0 +1,166 @@
[#]: collector: (lujun9972)
[#]: translator: (robsean)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Add sound to your Python game)
[#]: via: (https://opensource.com/article/20/9/add-sound-python-game)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
添加声音到你的 Python 游戏
======
通过添加声音到你的游戏中,听听当你的英雄战斗,跳跃,收集战利品时会发生什么。学习如何在这一 Pygame 系列的第十三篇文章中,创建一个声音平台类精灵。
![彩色声波图][1]
这是仍在进行中的关于使用 [Pygame][3] 模块来在 [Python 3][2] 中创建电脑游戏的第十三部分。先前的文章是:
1. [通过构建一个简单的掷骰子游戏去学习怎么用 Python 编程][4]
2. [使用 Python 和 Pygame 模块构建一个游戏框架][5]
3. [如何在你的 Python 游戏中添加一个玩家][6]
4. [用 Pygame 使你的游戏角色移动起来][7]
5. [如何向你的 Python 游戏中添加一个敌人][8]
6. [在 Pygame 游戏中放置平台][9]
7. [在你的 Python 游戏中模拟引力][10]
8. [为你的 Python 平台类游戏添加跳跃功能][11]
9. [使你的 Python 游戏玩家能够向前和向后跑][12]
10. [在你的 Python 平台类游戏中放一些奖励][13]
11. [添加计分到你的 Python 游戏][14]
12. [在你的Python游戏中加入投掷技巧][15]
Pygame 提供了一种简单的方法来集成声音到你的 Python 电脑游戏中。Pygame 的 [mixer 模块][16] 可以依据命令播放一个或多个声音,并且你也可以将这些声音混合在一起,例如,你能够在听到你的英雄收集奖励或跳过敌人声音的同时播放背景音乐。
集成 mixer 模块到一个已存在的游戏中是很容易的,因此 — 并不是给予你代码示例来向你展示放置它们的准确位置 — 这篇文章分四个所需要的步骤来阐明如何在你应用程序中的获取声音。
### 启动 mixer
首先,在你代码的 setup 部分,启动 mixer 进程。你的代码已经启动 Pygame 和 Pygame 字体,因此将它们归类到一起是一个很好的主意:
```
pygame.init()
pygame.font.init()
pygame.mixer.init() # add this line
```
### 定义声音
接下来,你必需定义你想要使用的声音。这样就要求你的计算机上有声音文件,就像使用字体就要求你有字体文件,使用图像就要求你有图像文件一样。
你还必需把这些声音与你的游戏捆绑在一起,以便任何玩你游戏的人都有声音文件。
为将一个声音与你的游戏捆绑在一起,首先在你的游戏目录中创建一个新的目录,就像你为你图像和字体创建的目录一样。称它为 `sound`:
```
s = 'sound'
```
尽管在互联网上有很多声音文件,下载这些声音文件并将其与你的游戏一起分发并不一定是合法的。这看起来是很奇怪的,因为这么多来自著名电脑游戏的声音是流行文化的一部分,但法律就是这样运作的。如果你想与你的游戏一起分发一个声音文件,你必需找到一个开放源码或 [Creative Commons][17] 声音文件,它们准许与游戏一起提供声音。
这里有一些专门提供自由和合法的声音文件的网站,包括:
* [Freesound][18] 托管存储所有种类的音效。
* [Incompetech][19] 托管存储背景音乐。
* [Open Game Art][20] 托管存储一些音效和音乐。
一些声音文件只有当你给予作曲家或声音设计师一下费用时才是可以自由使用的。在与你的游戏捆绑前,仔细阅读使用条件!音乐家和声音设计师在声音上的工作就像你在代码上的工作一样努力,因此当他们不要一些费用时,给予他们一些费用也是极好的。
给予你的声音源文件一些费用,在一个名称为 `CREDIT` 的文本文件中列出你使用的声音,并在你的游戏文件夹中放置该文本文件。
你也可以尝试制作你自己的音乐。极好的 [LMMS][21] 音频工作站易于使用,并携带很多有趣的声音。它在所有主要的平台上都可以使用,也可以导出为 [Ogg Vorbis][22] (OGG) 音频格式。
### 添加声音到 Pygame
当你找到你喜欢的一个声音文件时,下载它。如果它来自一个 ZIP 或 TAR 文件中,提取它并将其移动到你游戏目录中的 `sound` 文件夹中。
如果声音文件有一个带有空格或特殊字符的名字,重新命名它。文件名称是完全随意的,它的名称越简单,你就越容易输入到你的代码中。
大多数的电脑游戏使用 OGG 格式声音文件,因为这种格式在占有较小空间的情况下提供高质量的声音。当你下载一个声音文件时,它可能是一个 MP3, WAVE, FLAC, 或者其它的音频格式。为保持你的文件的较高兼容性和降低下载文件大小,使用一个工具 (像 [fre:ac][23] 或 [Miro][24]) 来转换这些的文件格式为 Ogg 格式。
例如,假设你已经下载一个称为 ouch.ogg 的声音文件。
在你代码的 setup 部分中,创建一个代表这你想使用的声音文件的变量:
```
ouch = pygame.mixer.Sound(os.path.join(s, 'ouch.ogg'))
```
### 触发一个声音
为使用一个声音,你所需要做的全部工作是,当你想触发声音时来调用变量。例如,当你的玩家击中一名敌人时,触发 `OUCH` 声音效果:
```
for enemy in enemy_hit_list:
pygame.mixer.Sound.play(ouch)
score -= 1
```
你可以为所有的动作类型创建声音,例如,跳跃,收集奖励,投掷,碰撞,和任何你可以想象到的东西。
### 添加背景音乐
如果你有你想在你的游戏的背景中播放的音乐或令人激动的音效,你可以使用 Pygame 中的 mixer 模块中的`music` 函数。在你的 setup 部分中,加载音乐文件:
```
music = pygame.mixer.music.load(os.path.join(s, 'music.ogg'))
```
然后,开始音乐:
```
pygame.mixer.music.play(-1)
```
`-1` 值来告诉 Pygame 无限循环音乐文件。你可以将其设置为 `0` 或任意更高的数值,以定义音乐在停止前循环多少次。
### 享受声音时空
音乐和声音可以为你的游戏添加很多韵味。尝试添加一些声音到你的 Pygame 工程!
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/9/add-sound-python-game
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[robsean](https://github.com/robsean)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/colorful_sound_wave.png?itok=jlUJG0bM (Colorful sound wave graph)
[2]: https://www.python.org/
[3]: https://www.pygame.org/news
[4]: https://opensource.com/article/17/10/python-101
[5]: https://opensource.com/article/17/12/game-framework-python
[6]: https://opensource.com/article/17/12/game-python-add-a-player
[7]: https://opensource.com/article/17/12/game-python-moving-player
[8]: https://opensource.com/article/18/5/pygame-enemy
[9]: https://opensource.com/article/18/7/put-platforms-python-game
[10]: https://opensource.com/article/19/11/simulate-gravity-python
[11]: https://opensource.com/article/19/12/jumping-python-platformer-game
[12]: https://opensource.com/article/19/12/python-platformer-game-run
[13]: https://opensource.com/article/19/12/loot-python-platformer-game
[14]: https://opensource.com/article/20/1/add-scorekeeping-your-python-game
[15]: https://opensource.com/article/20/9/add-throwing-python-game
[16]: https://www.pygame.org/docs/ref/mixer.html
[17]: https://opensource.com/article/20/1/what-creative-commons
[18]: https://freesound.org
[19]: https://incompetech.filmmusic.io
[20]: https://opengameart.org
[21]: https://opensource.com/life/16/2/linux-multimedia-studio
[22]: https://en.wikipedia.org/wiki/Vorbis
[23]: https://www.freac.org/index.php/en/downloads-mainmenu-330
[24]: http://getmiro.com