我有代码player_rect.colliderect(tile_rects):
,其中player_rect
是单个Rect
,而tile_rects
> 是 Rect
的列表。
我收到错误 `builtins.TypeError:
Argument must be rect style object
当我尝试运行我的代码时(大概是因为代码不喜欢在单个矩形上包含矩形列表)。
我还刚刚发现,当我切换tile_rects
和player_rect
的位置时,我反而得到了错误
builtins.AttributeError: 'list' object has no attribute 'colliderect'
我的问题是,如何更改代码以便检查与矩形和矩形列表的碰撞?
最佳答案
使用pygame.Rect.collidelist
测试矩形是否与矩形列表之一发生碰撞。
Test whether the rectangle collides with any in a sequence of rectangles. The index of the first collision found is returned. If no collisions are found an index of -1 is returned.
if player_rect.collidelist(tile_rects) >= 0:
# [...]
关于python - 矩形与矩形列表的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61007064/