pygame / docs / ref / examples.html

<title>examples - Pygame Documentation</title>
<body bgcolor=#aaeebb text=#000000 link=#331111 vlink=#331111>

<table cellpadding=0 cellspacing=0 border=0 style='border: 3px solid black;' width='100%'>
<td bgcolor='#c2fc20' style='padding: 6px;' align=center valign=center><a href=''><img src='../pygame_tiny.gif' border=0 width=200 height=60></a><br><b>pygame documentation</b></td>
<td bgcolor='#6aee28' style='border-left: 3px solid black; padding: 6px;' align=center valign=center>
	<a href=>Pygame Home</a> &nbsp;||&nbsp;
	<a href=../index.html>Help Contents</a> &nbsp;||
	<a href=index.html>Reference Index</a> &nbsp;||
<a href="camera.html">Camera</a>&nbsp;||&nbsp;
<a href="cdrom.html">Cdrom</a>&nbsp;||&nbsp;
<a href="color.html">Color</a>&nbsp;||&nbsp;
<a href="cursors.html">Cursors</a>&nbsp;||&nbsp;
<a href="display.html">Display</a>&nbsp;||&nbsp;
<a href="draw.html">Draw</a>&nbsp;||&nbsp;
<a href="event.html">Event</a>&nbsp;||&nbsp;
<a href="examples.html">Examples</a>&nbsp;||&nbsp;
<a href="font.html">Font</a>&nbsp;||&nbsp;
<a href="gfxdraw.html">Gfxdraw</a>&nbsp;||&nbsp;
<a href="image.html">Image</a>&nbsp;||&nbsp;
<a href="joystick.html">Joystick</a>&nbsp;||&nbsp;
<a href="key.html">Key</a>&nbsp;||&nbsp;
<a href="locals.html">Locals</a>&nbsp;||&nbsp;
<a href="mask.html">Mask</a>&nbsp;||&nbsp;
<a href="midi.html">Midi</a>&nbsp;||&nbsp;
<a href="mixer.html">Mixer</a>&nbsp;||&nbsp;
<a href="mouse.html">Mouse</a>&nbsp;||&nbsp;
<a href="movie.html">Movie</a>&nbsp;||&nbsp;
<a href="music.html">Music</a>&nbsp;||&nbsp;
<a href="overlay.html">Overlay</a>&nbsp;||&nbsp;
<a href="pixelarray.html">Pixelarray</a>&nbsp;||&nbsp;
<a href="pygame.html">Pygame</a>&nbsp;||&nbsp;
<a href="rect.html">Rect</a>&nbsp;||&nbsp;
<a href="scrap.html">Scrap</a>&nbsp;||&nbsp;
<a href="sndarray.html">Sndarray</a>&nbsp;||&nbsp;
<a href="sprite.html">Sprite</a>&nbsp;||&nbsp;
<a href="surface.html">Surface</a>&nbsp;||&nbsp;
<a href="surfarray.html">Surfarray</a>&nbsp;||&nbsp;
<a href="tests.html">Tests</a>&nbsp;||&nbsp;
<a href="time.html">Time</a>&nbsp;||&nbsp;
<a href="transform.html">Transform</a>

<a name="pygame.examples">
  <i>module of example programs</i><br>
  <tr><td><a href="examples.html#pygame.examples.aliens.main">pygame.examples.aliens.main</a> - <font size=-1>play the full aliens example</font></td><td>play the full aliens example</td></tr>
  <tr><td><a href="examples.html#pygame.examples.oldalien.main">pygame.examples.oldalien.main</a> - <font size=-1>play the original aliens example</font></td><td>play the original aliens example</td></tr>
  <tr><td><a href="examples.html#pygame.examples.stars.main">pygame.examples.stars.main</a> - <font size=-1>run a simple starfield example</font></td><td>run a simple starfield example</td></tr>
  <tr><td><a href="examples.html#pygame.examples.chimp.main">pygame.examples.chimp.main</a> - <font size=-1>hit the moving chimp</font></td><td>hit the moving chimp</td></tr>
  <tr><td><a href="examples.html#pygame.examples.moveit.main">pygame.examples.moveit.main</a> - <font size=-1>display animated objects on the screen</font></td><td>display animated objects on the screen</td></tr>
  <tr><td><a href="examples.html#pygame.examples.fonty.main">pygame.examples.fonty.main</a> - <font size=-1>run a font rendering example</font></td><td>run a font rendering example</td></tr>
  <tr><td><a href="examples.html#pygame.examples.vgrade.main">pygame.examples.vgrade.main</a> - <font size=-1>display a vertical gradient</font></td><td>display a vertical gradient</td></tr>
  <tr><td><a href="examples.html#pygame.examples.eventlist.main">pygame.examples.eventlist.main</a> - <font size=-1>display pygame events</font></td><td>display pygame events</td></tr>
  <tr><td><a href="examples.html#pygame.examples.arraydemo.main">pygame.examples.arraydemo.main</a> - <font size=-1>show various surfarray effects</font></td><td>show various surfarray effects</td></tr>
  <tr><td><a href="examples.html#pygame.examples.sound.main">pygame.examples.sound.main</a> - <font size=-1>load and play a sound</font></td><td>load and play a sound</td></tr>
  <tr><td><a href="examples.html#pygame.examples.sound_array_demos.main">pygame.examples.sound_array_demos.main</a> - <font size=-1>play various sndarray effects</font></td><td>play various sndarray effects</td></tr>
  <tr><td><a href="examples.html#pygame.examples.liquid.main">pygame.examples.liquid.main</a> - <font size=-1>display an animated liquid effect</font></td><td>display an animated liquid effect</td></tr>
  <tr><td><a href="examples.html#pygame.examples.glcube.main">pygame.examples.glcube.main</a> - <font size=-1>display an animated 3D cube using OpenGL</font></td><td>display an animated 3D cube using OpenGL</td></tr>
  <tr><td><a href="examples.html#pygame.examples.scrap_clipboard.main">pygame.examples.scrap_clipboard.main</a> - <font size=-1>access the clipboard</font></td><td>access the clipboard</td></tr>
  <tr><td><a href="examples.html#pygame.examples.mask.main">pygame.examples.mask.main</a> - <font size=-1>display multiple images bounce off each other using collision detection</font></td><td>display multiple images bounce off each other using collision detection</td></tr>
  <tr><td><a href="examples.html#pygame.examples.testsprite.main">pygame.examples.testsprite.main</a> - <font size=-1>show lots of sprites moving around</font></td><td>show lots of sprites moving around</td></tr>
  <tr><td><a href="examples.html#pygame.examples.headless_no_windows_needed.main">pygame.examples.headless_no_windows_needed.main</a> - <font size=-1>write an image file that is smoothscaled copy of an input file</font></td><td>write an image file that is smoothscaled copy of an input file</td></tr>
  <tr><td><a href="examples.html#pygame.examples.fastevents.main">pygame.examples.fastevents.main</a> - <font size=-1>stress test the fastevents module</font></td><td>stress test the fastevents module</td></tr>
  <tr><td><a href="examples.html#pygame.examples.overlay.main">pygame.examples.overlay.main</a> - <font size=-1>play a .pgm video using overlays</font></td><td>play a .pgm video using overlays</td></tr>
  <tr><td><a href="examples.html#pygame.examples.blend_fill.main">pygame.examples.blend_fill.main</a> - <font size=-1>demonstrate the various surface.fill method blend options</font></td><td>demonstrate the various surface.fill method blend options</td></tr>
  <tr><td><a href="examples.html#pygame.examples.cursors.main">pygame.examples.cursors.main</a> - <font size=-1>display two different custom cursors</font></td><td>display two different custom cursors</td></tr>
  <tr><td><a href="examples.html#pygame.examples.pixelarray.main">pygame.examples.pixelarray.main</a> - <font size=-1>display various pixelarray generated effects</font></td><td>display various pixelarray generated effects</td></tr>
  <tr><td><a href="examples.html#pygame.examples.scaletest.main">pygame.examples.scaletest.main</a> - <font size=-1>interactively scale an image using smoothscale</font></td><td>interactively scale an image using smoothscale</td></tr>
  <tr><td><a href="examples.html#pygame.examples.midi.main">pygame.examples.midi.main</a> - <font size=-1>run a midi example</font></td><td>run a midi example</td></tr>
  <tr><td><a href="examples.html#pygame.examples.scroll.main">pygame.examples.scroll.main</a> - <font size=-1>run a Surface.scroll example that shows a magnified image</font></td><td>run a Surface.scroll example that shows a magnified image</td></tr>
  <tr><td><a href="examples.html#pygame.examples.movieplayer.main">pygame.examples.movieplayer.main</a> - <font size=-1>play an MPEG movie</font></td><td>play an MPEG movie</td></tr>
<p>These examples should help get you started with pygame. Here is a brief rundown of what you get. The source code for these examples is in the public domain. Feel free to use for your own projects. </p>
<p>There are several ways to run the examples. First they can be run as stand-alone programs. Second they can be imported and their main() methods called (see below). Finally, the easiest way is to use the python -m option: </p>
<pre>   python -m pygame.examples.&lt;example name&gt; &lt;example arguments&gt;
</pre><p>eg: </p>
<pre>   python -m pygame.examples.scaletest someimage.png
</pre><p>Resources for the examples are found in the pygame/examples/data subdirectory. </p>
<p>We're always on the lookout for more examples and/or example requests. Code like this is probably the best way to start getting involved with python gaming. </p>
<p>This page is adapted from the original examples <tt>readme.txt</tt> by: </p>
<pre>    Pete Shinners
</pre><p>Further references </p>
<pre>  pySDL    : <a href=""><a/>
  SDL      : <a href=""><a/>
  aliens.c : <a href=""><a/>
</pre><p>examples as a package is new to pygame <tt>1.9.0</tt>. </p>
<!--COMMENTS:pygame.examples--> &nbsp;<br> 

<a name="pygame.examples.aliens.main">
  <i>play the full aliens example</i><br>
  <tt>pygame.aliens.main(): return None</tt><br>
<p>This started off as a port of the <tt>SDL</tt> demonstration, Aliens. Now it has evolved into something sort of resembling fun. This demonstrates a lot of different uses of sprites and optimized blitting. Also transparency, colorkeys, fonts, sound, music, joystick, and more. <tt>(PS</tt>, my high score is 117! goodluck) </p>
<!--COMMENTS:pygame.examples.aliens.main--> &nbsp;<br> 

<a name="pygame.examples.oldalien.main">
  <i>play the original aliens example</i><br>
  <tt>pygame.examples.oldalien.main(): return None</tt><br>
<p>This more closely resembles a port of the <tt>SDL</tt> Aliens demo. The code is a lot simpler, so it makes a better starting point for people looking at code for the first times. These blitting routines are not as optimized as they should/could be, but the code is easier to follow, and it plays quick enough. </p>
<!--COMMENTS:pygame.examples.oldalien.main--> &nbsp;<br> 

<a name="pygame.examples.stars.main">
  <i>run a simple starfield example</i><br>
  <tt>pygame.examples.stars.main(): return None</tt><br>
<p><tt>A</tt> simple starfield example. You can change the center of perspective by leftclicking the mouse on the screen. </p>
<!--COMMENTS:pygame.examples.stars.main--> &nbsp;<br> 

<a name="pygame.examples.chimp.main">
  <i>hit the moving chimp</i><br>
  <tt>pygame.examples.chimp.main(): return None</tt><br>
<p>This simple example is derived from the line-by-line tutorial that comes with pygame. It is based on a 'popular' web banner. Note there are comments here, but for the full explanation, follow along in the tutorial. </p>
<!--COMMENTS:pygame.examples.chimp.main--> &nbsp;<br> 

<a name="pygame.examples.moveit.main">
  <i>display animated objects on the screen</i><br>
  <tt>pygame.examples.moveit.main(): return None</tt><br>
<p>This is the full and final example from the Pygame Tutorial, "How Do <tt>I</tt> Make It Move". It creates 10 objects and animates them on the screen. </p>
<p>Note it's a bit scant on error checking, but it's easy to read. :] Fortunately, this is python, and we needn't wrestle with a pile of error codes. </p>
<!--COMMENTS:pygame.examples.moveit.main--> &nbsp;<br> 

<a name="pygame.examples.fonty.main">
  <i>run a font rendering example</i><br>
  <tt>pygame.examples.fonty.main(): return None</tt><br>
<p>Super quick, super simple application demonstrating the different ways to render fonts with the font module </p>
<!--COMMENTS:pygame.examples.fonty.main--> &nbsp;<br> 

<a name="pygame.examples.vgrade.main">
  <i>display a vertical gradient</i><br>
  <tt>pygame.examples.vgrade.main(): return None</tt><br>
<p>Demonstrates creating a vertical gradient with NumPy python. The app will create a new gradient every half second and report the time needed to create and display the image. If you're not prepared to start working with the NumPy arrays, don't worry about the source for this one :] </p>
<!--COMMENTS:pygame.examples.vgrade.main--> &nbsp;<br> 

<a name="pygame.examples.eventlist.main">
  <i>display pygame events</i><br>
  <tt>pygame.examples.eventlist.main(): return None</tt><br>
<p>Eventlist is a sloppy style of pygame, but is a handy tool for learning about pygame events and input. At the top of the screen are the state of several device values, and a scrolling list of events are displayed on the bottom. </p>
<p>This is not quality 'ui' code at all, but you can see how to implement very non-interactive status displays, or even a crude text output control. </p>
<!--COMMENTS:pygame.examples.eventlist.main--> &nbsp;<br> 

<a name="pygame.examples.arraydemo.main">
  <i>show various surfarray effects</i><br>
  <tt>pygame.examples.arraydemo.main(arraytype=None): return None</tt><br>
<p>Another example filled with various surfarray effects. It requires the surfarray and image modules to be installed. This little demo can also make a good starting point for any of your own tests with surfarray </p>
<p>If arraytype is provided then use that array package. Valid values are 'numeric' or 'numpy'. Otherwise default to NumPy, or fall back on Numeric if NumPy is not installed. As a program <tt></tt> accepts an optional --numeric or --numpy flag. (New pygame <tt>1.9.0)</tt> </p>
<!--COMMENTS:pygame.examples.arraydemo.main--> &nbsp;<br> 

<a name="pygame.examples.sound.main">
  <i>load and play a sound</i><br>
  <tt>pygame.examples.sound.main(file_path=None): return None</tt><br>
<p>Extremely basic testing of the mixer module. Load a sound and play it. All from the command shell, no graphics. </p>
<p>If provided, use the audio file 'file_path', otherwise use a default file. </p>
<p><tt></tt> optional command line argument: an audio file </p>
<!--COMMENTS:pygame.examples.sound.main--> &nbsp;<br> 

<a name="pygame.examples.sound_array_demos.main">
  <i>play various sndarray effects</i><br>
  <tt>pygame.examples.sound_array_demos.main(arraytype=None): return None</tt><br>
<p>If arraytype is provided then use that array package. Valid values are 'numeric' or 'numpy'. Otherwise default to NumPy, or fall back on Numeric if NumPy is not installed. </p>
<p>Uses sndarray and NumPy ( or Numeric) to create offset faded copies of the original sound. Currently it just uses hardcoded values for the number of echos and the delay. Easy for you to recreate as needed. Run as a program <tt></tt> takes an optional command line option, --numpy or --numeric, specifying which array package to use. </p>
<!--COMMENTS:pygame.examples.sound_array_demos.main--> &nbsp;<br> 

<a name="pygame.examples.liquid.main">
  <i>display an animated liquid effect</i><br>
  <tt>pygame.examples.liquid.main(): return None</tt><br>
<p>This example was created in a quick comparison with the BlitzBasic gaming language. Nonetheless, it demonstrates a quick 8-bit setup (with colormap). </p>
<!--COMMENTS:pygame.examples.liquid.main--> &nbsp;<br> 

<a name="pygame.examples.glcube.main">
  <i>display an animated 3D cube using OpenGL</i><br>
  <tt>pygame.examples.glcube.main(): return None</tt><br>
<p>Using PyOpenGL and pygame, this creates a spinning <tt>3D</tt> multicolored cube. </p>
<!--COMMENTS:pygame.examples.glcube.main--> &nbsp;<br> 

<a name="pygame.examples.scrap_clipboard.main">
  <i>access the clipboard</i><br>
  <tt>pygame.examples.scrap_clipboard.main(): return None</tt><br>
<p><tt>A</tt> simple demonstration example for the clipboard support. </p>
<!--COMMENTS:pygame.examples.scrap_clipboard.main--> &nbsp;<br> 

<a name="pygame.examples.mask.main">
  <i>display multiple images bounce off each other using collision detection</i><br>
  <tt>pygame.examples.mask.main(*args): return None</tt><br>
<p>Positional arguments: </p>
<pre>    one or more image file names.
</pre><p>This <tt>pygame.masks</tt> demo will display multiple moving sprites bouncing off each other. More than one sprite image can be provided. </p>
<p>If run as a program then <tt></tt> takes one or more image files as command line arguments. </p>
<!--COMMENTS:pygame.examples.mask.main--> &nbsp;<br> 

<a name="pygame.examples.testsprite.main">
  <i>show lots of sprites moving around</i><br>
  <tt>pygame.examples.testsprite.main(update_rects = True, use_static = False, use_FastRenderGroup = False, screen_dims = [640, 480], use_alpha = False, flags = 0): return None</tt><br>
<p>Optional keyword arguments: </p>
<pre>    update_rects - use the RenderUpdate sprite group class
    use_static - include non-moving images
    use_FastRenderGroup - Use the FastRenderGroup sprite group
    screen_dims - pygame window dimensions
    use_alpha - use alpha blending
    flags - additional display mode flags
</pre><p>Like the <tt>testsprite.c</tt> that comes with sdl, this pygame version shows lots of sprites moving around. </p>
<p>If run as a stand-alone program then no command line arguments are taken. </p>
<!--COMMENTS:pygame.examples.testsprite.main--> &nbsp;<br> 

<a name="pygame.examples.headless_no_windows_needed.main">
  <i>write an image file that is smoothscaled copy of an input file</i><br>
  <tt>pygame.examples.headless_no_windows_needed.main(fin, fout, w, h): return None</tt><br>
<p>arguments: </p>
<pre>    fin - name of an input image file
    fout - name of the output file to create/overwrite
    w, h - size of the rescaled image, as integer width and height
</pre><p>How to use pygame with no windowing system, like on headless servers. </p>
<p>Thumbnail generation with scaling is an example of what you can do with pygame. </p>
<p><tt>NOTE:</tt> the pygame scale function uses mmx/sse if available, and can be run in multiple threads. </p>
<p>If <tt></tt> is run as a program it takes the following command line arguments: </p>
<pre>    -scale inputimage outputimage new_width new_height
    eg. -scale in.png outpng 50 50
<!--COMMENTS:pygame.examples.headless_no_windows_needed.main--> &nbsp;<br> 

<a name="pygame.examples.fastevents.main">
  <i>stress test the fastevents module</i><br>
  <tt>pygame.examples.fastevents.main(): return None</tt><br>
<p>This is a stress test for the fastevents module. </p>
 <li>Fast events does not appear faster!</li>
 <li> </li>
<p>So far it looks like normal <tt>pygame.event</tt> is faster by up to two times. So maybe fastevent isn't fast at all. </p>
<p>Tested on windowsXP sp2 athlon, and freebsd. </p>
<p><tt>However..</tt>. on my debian duron 850 machine fastevents is faster. </p>
<!--COMMENTS:pygame.examples.fastevents.main--> &nbsp;<br> 

<a name="pygame.examples.overlay.main">
  <i>play a .pgm video using overlays</i><br>
  <tt>pygame.examples.overlay.main(fname): return None</tt><br>
<p>Play the .pgm video fila a path fname. </p>
<p>If run as a program <tt></tt> takes the file name as a command line argument. </p>
<!--COMMENTS:pygame.examples.overlay.main--> &nbsp;<br> 

<a name="pygame.examples.blend_fill.main">
  <i>demonstrate the various surface.fill method blend options</i><br>
  <tt>pygame.examples.blend_fill.main(): return None</tt><br>
<p><tt>A</tt> interactive demo that lets one choose which BLEND_xxx option to apply to a surface. </p>
<!--COMMENTS:pygame.examples.blend_fill.main--> &nbsp;<br> 

<a name="pygame.examples.cursors.main">
  <i>display two different custom cursors</i><br>
  <tt>pygame.examples.cursors.main(): return None</tt><br>
<p>Display an arrow or circle with crossbar cursor. </p>
<!--COMMENTS:pygame.examples.cursors.main--> &nbsp;<br> 

<a name="pygame.examples.pixelarray.main">
  <i>display various pixelarray generated effects</i><br>
  <tt>pygame.examples.pixelarray.main(): return None</tt><br>
<p>Display various pixelarray generated effects. </p>
<!--COMMENTS:pygame.examples.pixelarray.main--> &nbsp;<br> 

<a name="pygame.examples.scaletest.main">
  <i>interactively scale an image using smoothscale</i><br>
  <tt>pygame.examples.scaletest.main(imagefile, convert_alpha=False, run_speed_test=True): return None</tt><br>
<p>arguments: </p>
<pre>    imagefile - file name of source image (required)
    convert_alpha - use convert_alpha() on the surf (default False)
    run_speed_test - (default False)
</pre><p><tt>A</tt> smoothscale example that resized an image on the screen. Vertical and horizontal arrow keys are used to change the width and height of the displayed image. If the convert_alpha option is True then the source image is forced to have source alpha, whether or not the original images does. If run_speed_test is True then a background timing test is performed instead of the interactive scaler. </p>
<p>If <tt></tt> is run as a program then the command line options are: </p>
<pre>    ImageFile [-t] [-convert_alpha]
    [-t] = Run Speed Test
    [-convert_alpha] = Use convert_alpha() on the surf.
<!--COMMENTS:pygame.examples.scaletest.main--> &nbsp;<br> 

<a name="pygame.examples.midi.main">
  <i>run a midi example</i><br>
  <tt>pygame.examples.midi.main(mode='output', device_id=None): return None</tt><br>
<p>Arguments: </p>
<pre>    mode - if 'output' run a midi keyboard output example
              'input' run a midi event logger input example
              'list' list available midi devices
           (default 'output')
    device_id - midi device number; if None then use the default midi input or
                output device for the system
</pre><p>The output example shows how to translate mouse clicks or computer keyboard events into midi notes. It implements a rudimentary button widget and state machine. </p>
<p>The input example shows how to translate midi input to pygame events. </p>
<p>With the use of a virtual midi patch cord the output and input examples can be run as separate processes and connected so the keyboard output is displayed on a console. </p>
<p>new to pygame <tt>1.9.0</tt> </p>
<!--COMMENTS:pygame.examples.midi.main--> &nbsp;<br> 

<a name="pygame.examples.scroll.main">
  <i>run a Surface.scroll example that shows a magnified image</i><br>
  <tt>pygame.examples.scroll.main(image_file=None): return None</tt><br>
<p>This example shows a scrollable image that has a zoom factor of eight. It uses the <tt>Surface.scroll</tt> function to shift the image on the display surface. <tt>A</tt> clip rectangle protects a margin area. If called as a function, the example accepts an optional image file path. If run as a program it takes an optional file path command line argument. If no file is provided a default image file is used. </p>
<p>When running click on a black triangle to move one pixel in the direction the triangle points. Or use the arrow keys. Close the window or press <tt>ESC</tt> to quit. </p>
<!--COMMENTS:pygame.examples.scroll.main--> &nbsp;<br> 

<a name="pygame.examples.movieplayer.main">
  <i>play an MPEG movie</i><br>
  <tt>pygame.examples.moveplayer.main(filepath): return None</tt><br>
<p><tt>A</tt> simple movie player that plays an <tt>MPEG</tt> movie in a Pygame window. It showcases the <tt></tt> module. The window adjusts to the size of the movie image. It is given a boarder to demonstrate that a movie can play autonomously in a sub- window. Also, the file is copied to a file like object to show that not just Python files can be used as a movie source. </p>
<p>The <tt></tt> module is problematic and may not work on all systems. It is intended to replace it with an ffmpeg based version. </p>
<!--COMMENTS:pygame.examples.movieplayer.main--> &nbsp;<br> 

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.