Pygame is a cross-platform set of Python modules designed for creating games. The modules are designed to be simple, easy to use, and fun—a key part of Pygame's ideology. In this post, I'll show you how to use Pygame, and share tips and resources for learning it.
This tutorial assumes you already know how to code in Python. If not, check out The Best Way to Learn Python, Learn Python, The Python Tutorial, Codecademy, or Learn Python the Hard Way to get started.
Why Pygame?
It's Simple
Python is often regarded as the best "first programming language" to learn, and has been praised for its easy-to-learn syntax and gradual learning curve. For this reason, many new programmers start by learning Python.
Pygame extends Python, adopts Python’s philosophy, and aims to be easy to use. Plus, aspiring new game developers with no programming experience can use Pygame, as they can quickly pick up Python first.
It Has a Large Community
Pygame has been available since 2000 and, since then, a large community has built up around it. The community claims that Pygame has been downloaded millions of times, and has had millions of visits to its websit”. As a result of the large community, reported bugs are fixed quickly, help is readily available, and a large number of additional features have been added. Furthermore, the community ensures that Pygame development is fun; for example, a bi-annual Python game competition is run to promote the platform. The size of the community is what separates Pygame from the other Python game frameworks.
It's Open Source
The fact that Pygame is open source means that bugs are generally fixed very quickly by the community. It also means that you can extend Pygame to suit your needs and maybe even give back to the community.
Pygame is written in C, and taking a peek at the code is a great way to better understand how Python and Pygame work.
It's Highly Portable
Pygame is highly portable, as it supports Windows, Linux, Mac OS X, BeOS, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX. In addition to these computing platforms, a Pygame subset for Android exists. Furthermore, Pygame does not require OpenGL and can use DirectX, WinDIB, X11, Linux framebuffer and many other APIs to render graphics. This ensures that a larger number of users can play your game.
Getting Started
Installation
Before installing Pygame, ensure that you have installed Python. You will notice that there are two versions of Python: Python 2 and Python 3. Personally, I recommend that you use Python 2 alongside Pygame, as Python 2 is more prevalent and there are Pygame modules that do not support Python 3. However, you would still be able to make great games using Python 3.
After ensuring that a version of Python is running on your machine, you can pick up Pygame from its download website.
IDEs
In addition to installing the Pygame modules, I recommend that you install a Python IDE, which will make it easier for you to create and compile games.
Note that an integrated development environment, known as IDLE, already comes bundled with Python. However, IDLE has frequently been criticized for usability issues and I personally cannot recommend it.
My personal preference is PyScripter, but PyCharm, Wing IDE, Python Tools for Visual Studio and Netbeans are all viable options. As always, the choice of an IDE comes down to personal preference, so I suggest you try all of them and see which one you like best.
Resources
Here are a few resources that will help you get
started with Pygame.
University of Colorado
The University of Colorado provides a great introduction to Pygame. The slides attached will ensure that you know about and how to use the key components of Pygame. After you have read this document, you should understand the core concepts of Pygame.
Tutorials
There are a multitude of tutorials available that will teach you Pygame in a step-by-step manner. I’ve hand-picked three of my favorites:
- PyGame Tutorials - Simple Introduction to Game Programming: Covers the basic features of Pygame
- PyGame Basics Tutorial: A seven-part series starting from the beginning, with a particularly great section on Music and Sound Effects
- The Python Game Book: Starts off with the basics, but teaches advanced concepts including sprites, pixel-perfect collision detection, and scrolling
Video Series
Game Development in Python 3 With Pygame is a video tutorial series available as a YouTube playlist. The series contains 29 videos, beginning with displaying images and rendering text, and moves on to teach animation and user input. If you prefer video tutorials to text-based tutorials, don’t miss this series.
Books
If you are a beginner to both Python and Pygame, I recommend taking a look at Invent Your Own Computer Games with Python. This book starts off by teaching you the basics of Python and supports this instruction by showing you how to make simple command-line games. After ensuring you understand the basics of Python, the book moves on to teach Pygame.
On the other hand, if you are itching to jump straight into Pygame, Making Games with Python & Pygame is the book for you. By the end of this book, you will have created 11 games and will be ready to go on your own.
All of these books are free and are online, so you can get started right away!
Next Steps
Read the Documentation and Wiki
Taking a look at the documentation is an important step on your road to Pygame mastery. In here, you will find documents, tutorials and references that cover all the features of Pygame. The documentation will allow you to gain knowledge of all the aspects of Pygame, going beyond what you would have learned through the earlier resources.
The Pygame Wiki will also aid in answering any questions you may have about the platform, so don’t hesitate to take a look.
Look at Code
Looking at code will help you understand how different parts of Pygame interact to make a complete game. Moreover, examples will show you best practices, and you may pick up some tricks along the way.
Check out the official Pygame examples for details on how to use individual modules, or check out public Pygame projects on GitHub to see a full set of code.
Participate in the Community
Pygame has an active IRC channel and mailing list. (The mailing list is also available in a forum-like interface.) Members of the community are eager to help beginners, so don’t be afraid to ask questions.
Once you become a Pygame expert, contribute back to the community and help others. Teaching others will help you learn, and will solidify the concepts you already know.
Make Your Own Game
The resources provided above will be more than enough to gain a solid understanding of Pygame. Furthermore, if you work your way through the resources, you will have made multiple games. However, I urge you to make your own creation; do more than build Tic-Tac-Toe or Concentration. Anyone can follow a tutorial to make a game. Let your creativity flow and put your skills to the test by building an original game.
Development Tips
Use Groups
Pygame comes with a really powerful feature known as groups, through the pygame.sprite.Group
class. A group is essentially a
container for sprite objects.
For example, if I am creating multiple Bullet
sprites, I
can add them to a group called bullets
:
bullets = pygame.sprite.Group() for i in range (10): bullets.add(Bullet())
Working with groups is much faster than working with individual sprites.
You can simply call Group.update()
to update all the sprites within a group, or
Group.draw()
to blit the contents of the Group
to your Surface
. Furthermore,
groups make collision detection easier, because Pygame has a method to check for
collisions between two groups: pygame.sprite.groupcollide()
.
Convert Your Surfaces
Ensure that you are converting a Surface
whenever you are
creating one by calling the convert()
method. This method changes the pixel
format of a Surface
to match that of the display Surface
. If this method is not
called, a pixel conversion will have to be done every time you blit something to your Surface
—a very slow operation.
For example, instead of:
ballPicture = pygame.image.load('ball.png')
call:
ballPicture = pygame.image.load('ball.png').convert()
or if you need to set the alpha:
ballPicture = pygame.image.load('ball.png').convert_alpha()
Note that convert_alpha()
will not result in the same
performance increase as convert()
, since the pixel format will not be an exact
match for the Surface
window. Nevertheless, it results in an improved blit
speed.
Update vs. Flip
Both pygame.display.update()
and pygame.display.flip()
update the contents of the game window. However, simply calling these
methods update the entire screen, heavily slowing down the frame rate. Instead,
consider only updating a specific part of the screen (wherever changes are to
be made) by calling pygame.display.update()
and passing a rect
representing the
area to be updated into the method. In doing this, you will see a vast
improvement in performance.
Note that, at times, the entire screen will need to be updated, and in these instances both methods will work fine.
Additional Modules
Recall that I mentioned Pygame's large and helpful community earlier in the post. I recommend you take advantage of this community by downloading additional modules made by its members. These modules will help facilitate the rapid development of your game and simplify the game creation process.
My personal favorites include:
- Pyganim: Makes it easy to add sprite animations to your games
- input.py: Simplifies control handling in Pygame when using multiple controller methods
- Python Game Utilities: A large collection of Pygame utilities
You can find more libraries on the Pygame website.
Executable Games
If you want your game to be truly cross-platform, you must consider the scenario in which your users do not have Python and Pygame installed on their machines.
Consider the py2exe extension to make Windows executables, and the py2app extension to create applications for Mac OS X.
Showcase
Pygame has not been used very often commercially, as Python is not known for its game development capabilities. Nevertheless, Pygame has been used to create many successful independent games:
Galcon
Galcon is a multi-player galactic action-strategy game, and may be the most prominent game developed with Pygame. It received as an award as a Top 10 Game of the Year in 2007 from GameTunnel, one of the first indie game review sites.
Frets on Fire
Frets on Fire, an open-source Guitar Hero clone was created in Pygame, as an entry to the Assembly 2006 Game Development Competition by Unreal Voodoo.
Dangerous High School Girls in Trouble!
The multi-award-winning role-playing game Dangerous High School Girls in Trouble!, by independent developer Mousechief, was a finalist in IndieCade 2008.
PyWeek
PyWeek is a bi-annual game programming challenge that only accepts games created in Python. Thus, many submissions are created using Pygame. A list of previous winners is available on the PyWeek website.
Conclusion
In this post, I’ve given you an introduction to game development with Python and Pygame. I hope that I've been successful in getting you interested in Pygame and in teaching you more about it. Don’t hesitate to ask your questions here or in various Pygame forums. You are now ready to begin your journey into the world of Pygame. Best of luck!