Sprites, Collisions and Animations

Pygame Game Development Course

Felipe A. Moreno-Vera

Note

In these modules you will learn:

  • What a Sprite is, the pygame.sprite.Sprite class, groups, and transparency
  • pygame.Rect in depth, collision types, and collision response
  • Frame-based animation, spritesheets, and the animation state machine

Sprites & Images

What is a Sprite?

In game development, a sprite is any visual object in the game: the player, an enemy, a bullet, a pickup item. The term comes from the graphics chips of the 1980s that handled independent 2D objects on top of a background.

In Pygame, pygame.sprite.Sprite is a base class that gives you structure to manage game objects in an organized way. Each sprite has two mandatory attributes:

  • self.image → the visual surface (the drawing)
  • self.rect → the rectangle that defines position and size
 self.image            self.rect
 ┌──────────┐          ┌──────────┐
 │  pixels  │          │ x, y     │  ← position on screen
 │  (Surface│          │ w, h     │  ← width and height
 │   2D)    │          │ center   │  ← and many helper props
 └──────────┘          └──────────┘

Anatomy of a Sprite

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()           # MANDATORY: call the parent __init__

        # image: Surface with the drawing
        self.image = pygame.Surface((48, 48))
        self.image.fill((50, 120, 220))   # Solid blue

        # rect: position and size on screen
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)   # Place on screen

        # Game-specific attributes
        self.speed = 4

    def update(self):                # Called by the group every frame
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:  self.rect.x -= self.speed
        if keys[pygame.K_RIGHT]: self.rect.x += self.speed

Why use Sprite instead of loose variables?

Without Sprite With Sprite
player_x, player_y, player_img player.rect.x, player.image
Logic scattered all over the code Encapsulated in the class
Complicated manual collisions Automatic spritecollide()
Drawing object by object group.draw(screen)

Rect Properties

pygame.Rect properties:
  rect.x, rect.y      = 100, 50        ← top-left corner
  rect.width, height  = 48, 48       ← size
  rect.center         = (124, 74)   ← center point
  rect.topleft        = (100, 50)  ← alias for (x, y)
  rect.topright       = (148, 50) ← top-right corner
  rect.bottomleft     = (100, 98) ← bottom-left corner
  rect.right          = 148       ← right edge
  rect.bottom         = 98       ← bottom edge

Move by assigning to any property:
  rect.center = (400, 300)  → centers the sprite
  rect.topright = (800, 0)  → top-right corner of the screen

Sprite Groups

A pygame.sprite.Group is a collection of sprites with built-in superpowers:

all_sprites = pygame.sprite.Group()    # General group
bullets     = pygame.sprite.Group()    # Specific group
enemies     = pygame.sprite.Group()

# Adding sprites
player = Player(400, 300)
all_sprites.add(player)
bullets.add(Bullet(400, 300))

# Inside the Game Loop:
all_sprites.update()          # Calls update() on ALL sprites
all_sprites.draw(screen)      # Draws ALL sprites at once

# Remove a sprite from ALL its groups
sprite.kill()

A sprite can belong to multiple groups at the same time.

Loading Images

# Load from file
image = pygame.image.load("player.png")

# IMPORTANT: convert for better performance
image = image.convert()         # No transparency
image = image.convert_alpha()   # With transparency (PNG with alpha channel)

# Make one color transparent (for sprites without an alpha channel)
image.set_colorkey((255, 0, 255))  # Magenta = transparent

# Scale
image = pygame.transform.scale(image, (64, 64))

Transparency and Surfaces

Pygame has three levels of transparency:

Method How it works Typical use
Surface.set_alpha(0-255) Global transparency of the whole Surface Fade in/out, HUDs
Surface.set_colorkey(color) One specific color becomes transparent Sprites without PNG alpha
convert_alpha() + PNG Per-pixel alpha channel Sprites with smooth edges

Transparency and Surfaces

Pygame has three levels of transparency:

Method How it works Typical use
Surface.set_alpha(0-255) Global transparency of the whole Surface Fade in/out, HUDs
Surface.set_colorkey(color) One specific color becomes transparent Sprites without PNG alpha
convert_alpha() + PNG Per-pixel alpha channel Sprites with smooth edges
# Semi-transparent surface
surf = pygame.Surface((200, 100))
surf.set_alpha(128)   # 0 = invisible, 255 = solid
surf.fill((0, 0, 0))  # Semi-transparent black overlay

# Surface with per-pixel alpha support
surf = pygame.Surface((64, 64), pygame.SRCALPHA)
# Now you can draw with (R, G, B, A) colors
pygame.draw.circle(surf, (255, 0, 0, 180), (32, 32), 30)

Sprite — Summary

  • Sprite = Base class — self.image and self.rect are mandatory
  • Group = Collection that handles update() and draw() for all sprites
  • kill() = Removes the sprite from all its groups (destroys it from the game)
  • convert() = Optimizes the image for the current display (better FPS)
  • set_alpha = Global transparency 0-255
  • SRCALPHA = Flag for a surface with per-pixel alpha channel