Menu

Pygame Surface: Create, Fill and Draw on Surfaces

A Surface is an image in memory: the screen is one, every loaded image is one, and you can make your own. Learn pygame.Surface, fill, SRCALPHA, set_alpha, set_colorkey, subsurface and convert.

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

A Surface is an image in memory. The window is one (set_mode() returns it), every image you load is one, and text becomes one when you render it. You can also make blank Surfaces, draw on them once, and blit them as often as you like.

The badge is drawn once, before the loop, and copied twelve times every frame. Drawing a complex picture once and blitting it is much faster than drawing it shape by shape each frame.

Making a Surface

plain = pygame.Surface((100, 50))                   # black, no transparency
clear = pygame.Surface((100, 50), pygame.SRCALPHA)  # transparent, per pixel alpha

Useful methods on any Surface:

MethodDoes
fill(color)Paints the whole Surface (or fill(color, rect) for part of it)
get_width(), get_height(), get_size()Its size
get_rect(**place)A Rect of its size, optionally placed: get_rect(center=(240, 180))
blit(source, dest)Draws another Surface onto this one
copy()An independent copy
subsurface(rect)A Surface that shares pixels with part of this one
get_at((x, y)), set_at((x, y), color)Reads or writes one pixel

Three kinds of transparency

Per pixel alpha. A Surface made with pygame.SRCALPHA (or an image loaded with convert_alpha()) stores an alpha value for every pixel. Draw on it with four-number colors such as (255, 255, 255, 100).

Surface alpha. surface.set_alpha(0 to 255) makes the whole Surface see-through when it is blitted, without touching its pixels. It is the easy way to fade:

Color key. surface.set_colorkey(color) makes every pixel of one color invisible. Old games used magenta (255, 0, 255) backgrounds for this; PNG images with real transparency have mostly replaced it.

subsurface

sheet.subsurface((x, y, w, h)) returns a Surface that is a window onto part of another Surface, without copying pixels. It is how you cut frames out of a sprite sheet; see animation.

convert() and convert_alpha()

After loading an image, call convert() (no transparency) or convert_alpha() (keeps transparency). They change the pixels to the screen's format, so blitting is several times faster. Both need the window to exist, so call them after pygame.display.set_mode(). Loading images shows them in use.

Frequently Asked Questions

What is a Surface in pygame?

An image in memory with a width, a height and pixels. The window you get from set_mode() is a Surface, so is every image you load and every piece of text you render. You draw on Surfaces and blit Surfaces onto each other.

How do I make a transparent Surface in pygame?

Pass pygame.SRCALPHA: pygame.Surface((w, h), pygame.SRCALPHA). It starts fully transparent, and colors with a fourth alpha value draw see-through pixels on it.

What is the difference between set_alpha and SRCALPHA?

SRCALPHA gives every pixel its own transparency. set_alpha(128) makes the whole Surface half transparent when it is blitted, which is the quick way to fade an image in or out.

Coddy programming languages illustration

Learn to code with Coddy

Get started