
Time for action – a simple application using PyGame
This example will make use of the modules we just discussed. For this application to work, you will need to install PyGame. The binary and source distribution of PyGame is available on Pygame's website.
- Create a new Python source file and write the following code in it.
1 import pygame 2 import sys 3 4 pygame.init() 5 bgcolor = (200, 200, 100) 6 surf = pygame.display.set_mode((400,400)) 7 8 circle_color = (0, 255, 255) 9 x, y = 200, 300 10 circle_rad = 50 11 12 pygame.display.set_caption("My Pygame Window") 13 14 while True: 15 for event in pygame.event.get(): 16 if event.type == pygame.QUIT: 17 sys.exit() 18 elif event.type == pygame.KEYDOWN: 19 if event.key == pygame.K_UP: 20 y -= 10 21 elif event.key == pygame.K_DOWN: 22 y += 10 23 elif event.key == pygame.K_RIGHT: 24 x += 10 25 elif event.key == pygame.K_LEFT: 26 x -= 10 27 28 circle_pos = (x, y) 29 30 surf.fill(bgcolor) 31 pygame.draw.circle(surf, circle_color , 32 circle_pos , circle_rad) 33 pygame.display.flip()
- The first line imports the
pygame
package. On line 4, the modules within thispygame
package are initialized. An instance of classSurface
is created usingdisplay.set_mode
method. This is the main PyGame window inside which the images will be drawn. To ensure that this window is constantly displayed on the screen, we need to add awhile
loop that will run forever, until the window is closed by the user. In this simple application everything we need is placed inside thewhile
loop. The background color of the PyGame window represented by objectsurf
is set on line 30. - A circular shape is drawn in the PyGame surface by the code on line 31. The arguments to
draw.circle
are(Surface,
color,
position,
radius)
. This creates a circle at the position specified by the argumentcircle_pos
. The instance of classSurface
is sent as the first argument to this method. - The code block 16-26 captures certain events. An event occurs when, for instance, a mouse button or a key is pressed. In this example, we instruct the program to do certain things when the arrow keys are pressed. When the
RIGHT
arrow key is pressed, the circle is drawn with thex
coordinate offset by 10 pixels to the previous position. As a result, the circle appears to be moving towards right whenever you press theRIGHT
arrow key. When the PyGame window is closed, thepygame.QUIT
event occurs. Here, we simply exit the application by callingsys.exit()
as done on line 17. - Finally, we need to ensure that everything drawn on the
Surface
is visible. This is accomplished by the code on line 31. If you disable this line, incompletely drawn images may appear on the screen. - Execute the program from a terminal window. It will show a new graphics window containing a circular shape. If you press the arrow keys on the keyboard, the circle will move in the direction indicated by the arrow key. The next illustration shows the screenshot of the original circle position (left) and when it is moved using the
UP
andRIGHT
arrow keys.A simple PyGame application with a circle drawn within the Surface (window). The image on the right side is a screenshot taken after maneuvering the position of the circle with the help of arrow keys:

What just happened?
We used PyGame to create a simple user interactive application. The purpose of this example was to introduce some of the basic concepts behind animation and game programming. It was just a preview of what is coming next! Later in this book we will use Pyglet framework to create some interesting 2D animations.
QT Phonon
When one thinks of a media player, it is almost always associated with a graphical user interface. Of course one can work with command-line multimedia players. But a media player with a GUI is a clear winner as it provides an easy to use, intuitive user interface to stream a media and control its playback. The next screenshot shows the user interface of an audio player developed using QT Phonon.
An Audio Player application developed with QT Phonon:

QT is an open source GUI framework. 'Phonon' is a multimedia package within QT that supports audio and video playback. Note that, Phonon is meant for simple media player functionality. For complex audio/video player functionality, you should use multimedia frameworks like GStreamer. Phonon depends on a platform-specific backend for media processing. For example, on Windows platform the backend framework is DirectShow. The supported functionality may vary depending on the platform.
To develop a media processing application, a media graph is created in Phonon. This media graph contains various interlinked media nodes. Each media node does a portion of media processing. For example, an effects node will add an audio effect, such as echo to the media. Another node will be responsible for outputting the media from an audio or video device and so on. In chapter 8, we will develop audio and video player applications using Phonon framework. The next illustration shows a video player streaming a video. It is developed using QT Phonon. We will be developing this application in Chapter 8.
Using various built-in modules of QT Phonon, it is very easy to create GUI-based audio and video players. This example shows a video player in action:

Other multimedia libraries
Python bindings for several other multimedia libraries are available on various platforms. Some of the popular libraries are mentioned below.
Snack Sound Toolkit
Snack is an audio toolkit that is used to create cross-platform audio applications. It includes audio analysis and input-output functionality and it has support for audio visualization as well. The official website for Snack Sound Toolkit is http://www.speech.kth.se/snack/.
PyAudiere
PyAudiere (http://pyaudiere.org/) is an open source audio library. It provides an API to easily implement the audio functionality in various applications. It is based on Audiere Sound Library.