In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to add sound to your Python game". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to add sound to your Python game".
Start mixer
First, start the mixer process in the settings section of your code. Your code has started Pygame and Pygame fonts, so it's a good idea to group them together:
Pygame.init () pygame.font.init () pygame.mixer.init () # add this line define sound
Next, you must define the sound you want to use. This requires you to have sound files on your computer, just as using fonts requires you to have font files, and using images requires you to have image files.
You must also tie these sounds to your game so that anyone who plays your game has these sound files.
To tie a sound to your game, first create a new directory in your game directory, just like the directory you created for your images and fonts. Call it sound:
S = 'sound'
Although there are many sound files on the Internet, it is not necessarily legal to download these sound files and distribute them with your game. This seems strange because so many voices from famous computer games are part of popular culture, but that's how the law works. If you want to distribute a sound file with your game, you must find an open source or co-licensed sound file that allows sound to be provided with the game.
Here are some websites that provide free and legal sound files, including:
Freesound hosting stores all kinds of sound effects.
Incompetech hosting stores background music.
Open Game Art hosting stores some sound effects and music.
Some sound files are free to use as long as you give a thank you to the composer or sound designer. Read the conditions of use carefully before bundling with your game! Musicians and sound designers work as hard on sound as you do on code, so it's great to thank them even if they don't ask.
Give some thanks to your sound source file, list the sounds you use in a text file called CREDIT, and place the text file in your game folder.
You can also try to make your own music. The excellent LMMS audio workstation is easy to use and has a lot of interesting sounds. It is available on all major platforms and can be exported to the Ogg Vorbis (OGG) audio format.
Add sound to Pygame
When you find a sound file you like, download it. If it is a ZIP or TAR file, extract it and move it to the sound folder in your game directory.
If the name of the sound file contains spaces or special characters, rename it. The name of the file is completely arbitrary, and the simpler the name, the easier it is for you to enter it into your code.
Most computer games use OGG format sound files because this format takes up less space and provides high-quality sound. When you download a sound file, it may be a MP3, WAVE, FLAC, or other audio format. To maintain high compatibility of your files and reduce the download size, use a tool like fre:ac or Miro to convert these files to Ogg format.
For example, suppose you have downloaded a sound file called ouch.ogg.
In the settings section of your code, create a variable that represents the sound file you want to use:
Ouch = pygame.mixer.Sound (os.path.join (s, 'ouch.ogg')) triggers a sound
To use a sound, all you have to do is call this variable when you want to trigger it. For example, when your player hits an enemy, trigger the OUCH sound effect:
For enemy in enemy_hit_list: pygame.mixer.Sound.play (ouch) score-= 1
You can create sounds for a variety of actions, such as jumping, collecting rewards, throwing, colliding, and any other action you can imagine.
Add background music
If you have music or exciting sound effects that you want to play in the background of your game, you can use the music function in the mixer module in Pygame. In your settings section, load the music file:
Music = pygame.mixer.music.load (os.path.join (s, 'music.ogg'))
Then, start the music:
Pygame.mixer.music.play (- 1)
The-1 value tells Pygame to infinitely loop the music file. You can set it to any value from 0 to higher to define how many times the music loops before it stops.
Enjoy the sound effects
Music and sound can add a lot of charm to your game. Try to add some sounds to your Pygame project!
Thank you for your reading, the above is the content of "how to add sound to your Python game". After the study of this article, I believe you have a deeper understanding of how to add sound to your Python game. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.