Menu

What Is Pygame? The Python Game Library Explained

Pygame is a free Python library for making 2D games: it opens a window, draws shapes and images, reads the keyboard and mouse, and plays sound. Here is what it does, what it is good for, and a small game you can play on this page.

This page includes runnable editors. Edit, run, and see output instantly.

Pygame is a free Python library for making 2D games. It opens a window, draws shapes, images and text into it, reads the keyboard and the mouse, plays sound, and keeps time. You write the rules of the game in ordinary Python.

Here is a complete pygame program. Press Play, click the game, and move the square with the arrow keys:

That is the whole shape of every pygame game: set up a window, then repeat a loop that reads input, updates the game and draws a frame, sixty times a second. The game loop page takes it apart line by line.

What pygame gives you

Pygame is a set of modules, each for one job:

ModuleWhat it does
pygame.displayOpens the window and shows each finished frame
pygame.drawDraws rectangles, circles, lines and polygons
pygame.imageLoads PNG and JPG images
pygame.RectStores a position and size, moves things, checks collisions
pygame.event, pygame.key, pygame.mouseRead the keyboard and the mouse
pygame.spriteGroups game objects and checks collisions between them
pygame.fontTurns text into an image you can draw
pygame.mixerPlays sound effects and music
pygame.timeKeeps a steady frame rate and runs timers

Underneath, pygame is built on SDL, a C library that many commercial games use for windows, graphics, input and audio. That is why pygame runs on Windows, macOS and Linux with the same code.

What it is good for

  • Learning to make games. Pygame has no editor and no hidden engine: the loop, the drawing and the collisions are all code you can read. Many people make their first game with it.
  • 2D games of any style: arcade games, platformers, shooters, puzzle games, card games and tile-based games.
  • Simulations and visual experiments: particles, physics toys, cellular automata, sorting visualizations.
  • Game jams. Pygame games are small, start in a second and are easy to share as Python files.

It is a weaker choice for 3D games, for games that need an editor to lay out levels, or for publishing to phones and consoles. Pygame vs Godot compares the two approaches.

pygame and pygame-ce

There are two versions of the library today. The original pygame, and pygame-ce (Community Edition), a fork made in 2023 by most of pygame's active developers. Both are imported with import pygame and nearly all code runs on both. pygame-ce is updated more often and adds features, so these docs use it. Pygame vs pygame-ce explains the difference and which to install.

What you need to know first

Pygame is Python, so you need the basics: variables, if statements, loops, functions and lists. Classes help once your game grows, because sprites are classes. If you are new to Python, the Python course starts from zero.

Where to go next

  1. Install pygame on your computer, or keep using the examples on these pages, which run in your browser.
  2. Write your first pygame program.
  3. Learn the game loop, then drawing, input and sprites, in the order of the sidebar.

Every example in these docs is a full program you can edit and play. The pygame playground is a bigger editor for your own games.

Frequently Asked Questions

What is pygame used for?

Making 2D games and interactive programs in Python: arcade games, platformers, puzzle games, simulations and visualizations. It handles the window, drawing, images, keyboard and mouse input, sound and timing, and you write the game rules in plain Python.

Is pygame good for beginners?

Yes. You need only basic Python (variables, if, loops and functions) to open a window and move a shape with the arrow keys. Because pygame gives you a loop and a canvas rather than an editor, you also learn how games work underneath.

Is pygame free?

Yes. Pygame and pygame-ce are free and open source under the LGPL license, so you can use them in personal and commercial games.

Can pygame make 3D games?

Not by itself. Pygame draws 2D images and shapes. Some people combine it with OpenGL through PyOpenGL, but for 3D games an engine such as Godot or Unity is a better fit.

Coddy programming languages illustration

Learn to code with Coddy

Get started