pygame.Surface
A Surface is an image in memory. The screen is a Surface, every loaded image is a Surface, and rendered text is a Surface. Learn to create, combine and make them fast.
On this page
Overview#
A pygame.Surface holds pixels of a fixed size. You draw on Surfaces with pygame.draw, copy one onto another with blit(), and show the display Surface with pygame.display.flip().
Syntax#
Python
surf = pygame.Surface((width, height), flags=0)
surf = pygame.Surface((width, height), pygame.SRCALPHA) # per-pixel transparency
rect = target.blit(source, dest=(x, y), area=None, special_flags=0)
surf.fill(color, rect=None)
fast = image.convert() # no transparency
fast = image.convert_alpha() # keeps per-pixel transparencyKey methods#
| Method | Returns / does |
|---|---|
blit(source, dest, area=None, special_flags=0) | Copies source onto this Surface at dest (a position or Rect). area copies only part of the source. Returns the affected Rect. |
blits(sequence) | Many blits in one call: a list of (source, dest) tuples. Faster than a Python loop of blit. |
fill(color, rect=None) | Fills the Surface or one area. Returns the affected Rect. |
convert() / convert_alpha() | A new Surface in the display's pixel format for fast blitting. Needs set_mode() first. |
copy() | An independent duplicate. |
get_rect(**kwargs) | A Rect the size of the Surface; keywords position it, e.g. get_rect(center=(320, 240)). |
get_size(), get_width(), get_height() | Dimensions. |
set_colorkey(color) | Treats one colour as transparent. |
set_alpha(value) | Transparency for the whole Surface, 0 (invisible) to 255 (opaque). |
subsurface(rect) | A child Surface that shares pixels with this one; changes affect both. |
get_at((x, y)) / set_at((x, y), color) | Read or write a single pixel (slow for many pixels). |
Three kinds of transparency#
- Colour key: one colour is skipped when blitting. Fast; ideal for simple sprites with a solid background colour.
- Surface alpha:
set_alpha(128)makes the whole Surface half transparent. Good for fading. - Per-pixel alpha: Surfaces created with
pygame.SRCALPHA, or images loaded and converted withconvert_alpha(), store transparency for every pixel. Needed for soft edges and shadows.
Practical example#
surface_example.pyDownload
"""pygame.Surface: creating surfaces, blitting, transparency (colorkey, alpha, per-pixel) and subsurfaces."""
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 360))
clock = pygame.time.Clock()
# 1. A plain surface with a colorkey: pure magenta becomes transparent.
keyed = pygame.Surface((100, 100))
keyed.fill((255, 0, 255))
pygame.draw.circle(keyed, "gold", (50, 50), 40)
keyed.set_colorkey((255, 0, 255))
# 2. Whole-surface alpha: the entire image is 50% transparent.
faded = pygame.Surface((100, 100))
faded.fill("royalblue")
faded.set_alpha(128)
# 3. Per-pixel alpha (SRCALPHA): every pixel has its own transparency.
glow = pygame.Surface((100, 100), pygame.SRCALPHA)
for r in range(50, 0, -5):
pygame.draw.circle(glow, (255, 255, 255, 255 - r * 5), (50, 50), r)
# 4. A subsurface shares pixels with its parent (useful for sprite sheets).
sheet = pygame.Surface((200, 50))
sheet.fill("seagreen")
pygame.draw.rect(sheet, "white", (110, 10, 30, 30))
right_half = sheet.subsurface((100, 0, 100, 50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((40, 48, 80))
pygame.draw.rect(screen, "tomato", (0, 150, 640, 60)) # a stripe to show transparency
screen.blit(keyed, (30, 130))
screen.blit(faded, (170, 130))
screen.blit(glow, (310, 130))
screen.blit(right_half, (450, 155))
# blit returns the area that changed; special_flags change how colours mix
screen.blit(glow, (520, 130), special_flags=pygame.BLEND_RGB_ADD)
pygame.display.flip()
clock.tick(30)
pygame.quit()Blend modes#
special_flags changes how colours combine: pygame.BLEND_RGB_ADD brightens (good for glows and lights), BLEND_RGB_MULT darkens (shadows, tinting), and BLEND_RGBA_MULT can tint while respecting alpha.
Performance tips#
- Always
convert()orconvert_alpha()loaded images once. - Pre-render things that don't change (backgrounds, text) to a Surface and blit that.
- Avoid
get_at/set_atloops over many pixels; usepygame.PixelArrayorpygame.surfarray(needs NumPy) for bulk pixel work.
More in performance tips.
Common errors#
pygame.error: No video mode has been set:convert()beforeset_mode().ValueError: subsurface rectangle outside surface area: the rect passed tosubsurfacemust lie fully inside the parent.- Black background instead of transparency: the Surface lacks per-pixel alpha. Create it with
pygame.SRCALPHAor useconvert_alpha().
pygame-ce notes#
pygame-ce adds Surface.fblits() (a faster variant of blits without return values) and Surface.get_frect() (returns a float FRect). Neither exists in the original pygame.