Menu

Pygame blit(): Draw Images and Surfaces on Screen

blit() copies one Surface onto another: an image onto the screen, text onto a panel, a tile onto a map. Learn the destination argument, blitting at a Rect, the area argument for sprite sheets, and blits() for many at once.

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

blit() copies one Surface onto another. screen.blit(image, (x, y)) draws image on the screen with its top-left corner at (x, y). Images, text and anything you drew on your own Surface all reach the screen through blit.

The images come from the free art pack these docs provide in an assets folder. On your computer, put PNG files next to your script and change the paths.

The arguments

screen.blit(source, dest, area=None, special_flags=0)
ArgumentMeaning
sourceThe Surface to copy: an image, rendered text, your own Surface
destWhere: an (x, y) position, or a Rect (its top-left corner is used)
areaOptional: copy only this rectangle of source
special_flagsOptional blend mode, such as pygame.BLEND_ADD

blit returns the Rect of the screen it changed, clipped to the screen.

Blitting at a Rect

Games usually keep a Rect for each object and blit at it. get_rect() with a keyword places the Rect, which makes centering easy:

Press Play, click the game and fly with the arrow keys.

Order matters

Each blit covers what is already there. Blit the background first, the player and enemies next, the score last. A screen.fill() after a blit wipes it out.

Part of an image: area

area copies only a rectangle of the source. It is one way to draw a single frame of a sprite sheet without cutting it first:

frame = pygame.Rect(24 * index, 0, 24, 24)
screen.blit(sheet, (100, 100), area=frame)

Animation uses subsurface for the same job, which cuts the frames once.

Many at once: blits()

screen.blits() takes a list of (source, dest) pairs and blits them all in one call, which saves some time when you draw hundreds of tiles:

screen.blits([(tile, (x * 36, y * 36)) for (x, y) in floor_cells])

Blending

special_flags mixes colors instead of covering them. pygame.BLEND_ADD adds the source's color to the screen's, which makes glows and light effects; pygame.BLEND_MULT darkens. Blending is not needed for normal drawing.

Frequently Asked Questions

What does blit mean in pygame?

To blit is to copy the pixels of one Surface onto another. screen.blit(image, (x, y)) draws image on the screen with its top-left corner at (x, y). The word comes from "block transfer".

How do I blit an image at its center?

Get a Rect placed by its center and blit at that Rect: rect = image.get_rect(center=(240, 180)), then screen.blit(image, rect). A Rect as the destination uses its top-left corner.

Why is my blitted image not showing?

Check that you blit after screen.fill() (not before it), that pygame.display.flip() runs after the blit, and that the position is inside the window.

Coddy programming languages illustration

Learn to code with Coddy

Get started