的掩码
Terrain从未设置。装上适当的
Terrain口罩:
class Terrain(object): def __init__(self, x, y): self.x = x self.y = y maskSurf = pg.Surface((display_width, display_height)).convert_alpha() maskSurf.fill(0) pg.draw.rect(maskSurf, (160, 160, 160), (self.x, self.y, display_width, 500), 0) self.mask = pg.mask.from_surface(maskSurf) print(self.mask.count()) # [...]
如果使用
pygame.mask.Mask.overlap(),那么你已经检查的重叠
Spaceship和
Terrain,而不是
Terrain和
Spaceship。
由于
Terrain遮罩是整个屏幕的遮罩,因此
overlap()测试的偏移量是的位置
Spaceship:
def check_for_collisions(): offset = (int(spaceship.x), int(spaceship.y)) collide = terrain.mask.overlap(spaceship.mask, offset) print(offset, collide) return collide
最小示例:
[ repl.it/@Rabbid76/PyGame-SurfaceMaskIntersect](https://repl.it/@Rabbid76/PyGame-SurfaceMaskIntersect#main.py)
另请参阅:面膜



