Adding sound doesn’t take much code, but it makes the game much more interesting. The first part focuses on adding sound to the Alien Invasion project, and the second focuses on writing a separate program that plays the game automatically. Pygame - Adding Sound and Automating Game Play This site uses Just the Docs, a documentation theme for Jekyll. Finding Employment, Understanding Django, and more (2/28/20).Adding Sound and Automating Game Play in Alien Invasion (4/3/20).Recommended Reading, Django 3.1, and more (9/3/20).Pygame - Adding Sound and Automating Game Play.Role of self and ai_game in code that creates the ship.There is also a global stop_all function that stops all existing playback: simpleaudio. play_obj.wait_done() Is Playingįor checking whether the PlayObject is still playing, you can call the is_playing function: play_obj.is_playing() StopĪlthough there is no pause or resume function provided at the time of writing, you can still use the stop function to end the audio playback: play_obj.stop() Multiple calls to the play function will run all the playback in parallel at the background asynchronously. Finally, add the following function and you can run this Python file to test the audio playback. This function will return a PlayObject, which essentially handles audio playback jobs such as stop and check if still playing. The next step is to call the play function: play_obj = wave_obj.play() If you happen to have an mp3 instead of wav, simply convert it to a wav file instead. It is highly recommended to use a wav file as input. Modify the input path according to what you have: wave_obj = sa.om_wave_file('alarm.wav') Add the following code in your Python file. sample_rate ( int) - the sample rate in Hzįortunately, this module also provides us with the from_wave_file function, which allows us to input the file path and it will load the corresponding data into the WaveObject.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |