碰撞检测

52.6 碰撞检测

Rect(x,y,w,h) 矩形;rect1.colliderect(rect2) 是否相交。

精灵位置用 Rect 管理便于碰撞。

矩形碰撞

# ========================================
# 示例:colliderect
# ========================================
import pygame

a = pygame.Rect(50, 50, 40, 40)
b = pygame.Rect(70, 70, 40, 40)
print('碰撞' if a.colliderect(b) else '未碰撞')