Purple Hat – Games & GUI
advanced40 XP
Game Mechanics
Add animation, collision detection, and scoring to your games
Game Mechanics
Moving Objects
Python
ball_x, ball_y = 400, 300
speed_x, speed_y = 5, 3
ball_x += speed_x
ball_y += speed_y
if ball_x <= 0 or ball_x >= 800:
speed_x = -speed_x # bounce!Collision
Python
player = pygame.Rect(px, py, 40, 40)
enemy = pygame.Rect(ex, ey, 40, 40)
if player.colliderect(enemy):
lives -= 1⌨️
Type it yourself
Don't just read it — type the example from above into the box, press Run ▶, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.
Python · Playground
Output
Press ▶ Run to see your code come to life…Quick check
01/2
How do you detect if two Pygame rectangles overlap?