Python

From GPWiki
Jump to: navigation, search

Python is an interpreted language. The interpreter is open-source. A good range of libraries exist.

About Python

Python has become a powerful tool for game development. It has been used extensively in commercial games, including Freedom Force, EVE Online, Civilization IV, Toontown Online, Pirates of the Caribbean Online, Temple of Elemental Evil, and Battlefield 2 (as well as 2142).

Python's primary advantage is that it is very fast to develop in. Writing python code typically takes a fraction of the time that it takes to write the equivalent C code. It has been estimated that it is 5 to 10 times faster to write a program in Python than C. This can cut months or years off of a project's development time.

There is also a significant body of library code that handles 2D and 3D graphics rendering.

Python's primary disadvantage is that it is generally not fast enough for complex calculations (like 3D physics). Most complex games that use python offload a few subroutines to C, on an as-needed basis, or use an existing C library (like ODE for physics). Most developers that try this mix swear by it: the result is a game that can be developed at the speed of python, but which runs at the speed of C wherever it is necessary to do so.

When mixing python with C, the impact on speed depends on how much of the time is spent executing Python logic versus how much of the time is spent in C libraries. The Python logic can be 10-100 times slower than C. It is recommended that the critical code (matrix calculations, for-each-pixel manipulations, 3D graphics, etc) be in C libraries.

Python makes it easy to interface with C; Boost Python makes it easy to interface with C++. It is also possible to use Pyrex, a C/Python hybrid language that makes it particularly easy to integrate with C libraries, and also makes it possible to write C-speed code in a Python-like language. In addition SWIG easily allows a developer to wrap C/C++ functions for use with many scripting languages including Python.

Python is generally considered a fairly easy language to learn. If you already have a C/C++ background, you will find its features familiar: you will only have to spend an hour in the Python.org tutorials to be able to use it.

Python's feature set includes powerful list and string manipulation, tables, sets, and a sizeable library of utility functions such as serialization, XML parsing, networking, regular expressions, and so forth. Matrix and vector manipulation are included with Numeric Python. Very high-quality 3D graphics libraries are also available. As a rule, all of these libraries are written in and operate at the speed of C.

A key advantage of Python is that it is relatively easy for non-programmers to read. This makes it suitable as a scripting language. For example in an RPG you might build the main part of the game in C++ but write scripts (where the shopkeepers go at night, what the NPCs say, how the trolls walk back and forth) in Python.

In conclusion, this language is very useful as long as you don't ask it to do what it does worst (calculation on large data sets): such tasks should be offloaded to C or special numeric processing libraries such as SciPy.

There exists a tool, Psyco, which gives your program a boost without needing any rewrite. All you have to do is import it at the beginning of your code. The main drawback is that Psyco only runs on x86-compatible platforms at the moment.

If you plan using Python for game development you might also want to consider looking at Ruby although you will find Python is faster (All Languages Compared, Python vs Ruby). Both languages have their up- and downsides, so choose wisely.

Tutorials and Source

Library Dependent

Generic game programming libraries

Panda3D 
all round game engine
Pygame 
an SDL wrapper for Python and more
PGU 
Phil's Pygame Utilities. PGU provides a collection of tools and libraries that enhance Pygame. Tools include a tile editor and a level editor (tile, isometric, hexagonal). GUI enhancements include full featured gui, html rendering, document layout, and text rendering. The libraries include a sprite and tile engine (tile, isometric, hexagonal), a state engine, a timer, and a high score system.
Pyglet 
a cross-platform windowing and multimedia library for Python with no external dependencies or installation requirements. Pyglet provides an object-oriented programming interface for developing games and other visually-rich applications. Pyglet allows programs to open multiple windows on multiple screens, draw in those windows with OpenGL, and play back audio and video in most formats. Unlike similar libraries available, pyglet has no external dependencies (such as SDL) and is written entirely in Python.

3D libraries

Name Current Version License Windows Sound Rendering Animation Collision
PySoy Beta-2 (2008-01) GPLv3 Multiple 3D (OpenAL) VA/VBO via C-Thread C-based Physics (ODE) C-based AABB Trees
Panda3D 1.5.3 (2008-09) BSD Multiple 3D (FMOD/OpenAL/Miles) OpenGL/DirectX(8/9)/TinyGL C-based (ODE optional) C-based (ODE optional)

External Links

Python.org 
Main python page, included downloads of the interpreter and documentation.
Rabbyt 
A fast Sprite library for Python with game development in mind. With Rabbyt Anims, even old graphics cards can produce very fast animations of 2,400 or more sprites handling position, rotation, scaling, and color simultaneously.
Pyrex  
Pyrex lets you write code that mixes Python and C data types any way you want, and compiles it into a C extension for Python.
boost::python 
A convenient way to combine C++ and Python code.
SWIG 
Simplified Wrapper and Interface Generator is a tool that easily allows a developer to wrap C/C++ functions for use with scripting languages. Currently supports Python, Perl, and Tcl.
Jython 
Java implementation of python.
Psyco 
A module that boosts the speed of python programs.
Iron Python 
An alternative implementation of python for .NET.
Boo 
A python-like language for .NET and Mono. Nice for game development because it is statically typed and runs as fast as C# code. Use with the Tao Framework for OpenGL or else Managed DirectX.
PyOpenGL 
A binding to OpenGL
PyODE 
A binding to the Open Dynamics Engine, an open source physics and collision engine.
Twisted 
An all-round networking library with many features
Python 3D Software Collection 
Collected links to various Python 3D and game-related libraries
Python 3D 
all things python and 3d.
pyglfw 
an GLFW wrapper for Python
Soya 3D 
a 3D game engine using Python.
PySoy 
a 3D game engine using Python with integrated physics (ODE)
Panda3d 
a 3D game engine using Python with integrated sound, physics, animations, networking, shaders, running under Windows, Linux and OS X, with 3d exporters from Blender, Maya and 3dsMax
PyWeek 
A bi-annual programming game-writing challenge using Python.
PyDay 
A programming game-writing challenge with only a day to write a complete game using less than 32kb of code.
PyGame.org 
main site for PyGame SDL warping library (with some extra features)
MGF Magazine 
My Game Fast - Panda3D and Python online training