0 XP
Chapter 9
Sprites & Images
Load real art assets and use sprite classes for clean game objects.
Core Concept
pygame.Sprite is the base class for all game objects — combine it with groups for easy rendering and collision.
Theory
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((40, 60), pygame.SRCALPHA)
pygame.draw.rect(self.image, (141, 247, 127), (0, 0, 40, 60), border_radius=8)
self.rect = self.image.get_rect(center=(320, 180))
def update(self):
keys = pygame.key.get_pressed()
self.rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
In game loop:all_sprites.update()
all_sprites.draw(screen)
Live Demo — Run & Explore
Try it live
Loading...
Output will appear here…
Exercise 1 / 1
📝 Create an Enemy sprite class with a red color. Add 3 enemies at different positions to a sprite group and draw them.
Your solution
Loading...
Output will appear here…
Ask AI Tutor
AI Tutorgemini-3.1-flash-lite-preview
AI
Hi! I'm your AI tutor for Sprites & Images. 🎓
Ask me anything about this lesson — what a variable is, why loops are useful, or "why is my code not working?". I'm here to help you understand, not just give you answers!
What's on your mind? 😊