|
|
|
| Home Blender Games CG Projects University Projects Tutorials Resources Links Random Stuff |
Music Support with PygameNo Support for Music Files?We haven't had support for mp3 or ogg files in Blender since the 2.25 days. Therefore I started looking for alternative ways to add music support to my games until it is reimplemented. Pygame is a set of Python modules written on top of the SDL library. It's designed for writing games, so it should be the perfect solution to expanding the game engine's abilities. This tutorial will show you how you can play mp3 and ogg files in your games using pygame. Required Files For Windows users, I've already isolated the files needed by pygame for mp3 and ogg support. To begin with, download these files. Users on other platforms will need a Python 2.3 and Pygame 1.6 installation for their platform. Pygame has many dependencies on other files. This pack contains all the necessary files to use the sound functionality of pygame. Other functions of Pygame may require additional files from Python 2.3. The zip should contain the following files: copy_reg.py ntpath.py os.py stat.py string.py types.py UserDict.py Each of these should also have it's associated .pyc file. These files were taken from Python 2.3. SDL.dll python23.dll These are needed by the Blender runtime. Finally the pygame folder contains all the pygame files. First Steps To begin with extract everything in the zip to a folder. Nothing else needs to be installed, the package is completely self-contained. This keeps things simple for anyone who wishes to play your game. They just need to extract everything and run the executable. Programming Now to setup your python scripts inside the game engine. Use this code to load and play a music sample: import pygame.mixer # Import Mixer Module pygame.mixer.init() # Initialise Mixer Module pygame.mixer.music.load("MyMusic.ogg") # Load music from specified path and filename pygame.mixer.music.play() # Play Music That's it, just 4 lines of code! When you want to stop the music, you can use either of these functions: pygame.mixer.music.stop() # Stops Music pygame.mixer.music.fadeout(milliseconds) # Fades out and stops music over given milliseconds Other useful functions are: pygame.mixer.music.set_volume(value) # Sets volume of music. Value range 0.0 to 1.0. pygame.mixer.music.queue("MyMusic2.ogg") # Queue track to play when current one ends. All functions of pygame.mixer.music can be read about here. Final Steps Once your game is done and music programmed in, export it as a runtime. Save your runtime in the folder you created to hold the files. Place your music files in this folder as well. If everything has been done correctly you should now be able to hear music playing when you run your game! Problems The most common problem occurs when trying to import the mixer module. If you try play your game in .blend format from inside Blender, it will search for the files in the Blender installation directory, not the one with the .blend in. If you wish to test your game inside Blender, you should open your .blend by double-clicking it. If end users don't have pygame installed and aren't running windows, the files provided will generate an error when people try play your games. |