python - 矩形与矩形列表的碰撞

标签 python list pygame rect

我有代码player_rect.colliderect(tile_rects):,其中player_rect是单个Rect,而tile_rects > 是 Rect 的列表。
我收到错误 `builtins.TypeError:

Argument must be rect style object

当我尝试运行我的代码时(大概是因为代码不喜欢在单个矩形上包含矩形列表)。

我还刚刚发现,当我切换tile_rectsplayer_rect的位置时,我反而得到了错误

builtins.AttributeError: 'list' object has no attribute 'colliderect'

我的问题是,如何更改代码以便检查与矩形和矩形列表的碰撞?

最佳答案

使用pygame.Rect.collidelist测试矩形是否与矩形列表之一发生碰撞。

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/

相关文章:

c - 抛出异常 : read access violation. **head** 为 0xCCCCCCCC。发生

python - 在 Lenovo 笔记本电脑上运行 “Windows fatal exception” 脚本时出现不需要的 “pywinatuo”

JavaScript/Python 匿名函数与命名函数作用域

python nginx uwsgi -- 请求的修饰符不可用 : 1 --

python - 将时间列表转换为总时间

python - Pygame 的矩形不移动?

python - 如何将自定义停用词列表添加到 StopWordsRemover

python - 在Python3中逐一读取列表的元素

python - 如何在 Pygame 中更改图像的颜色?

python - 需要帮助在 PyGame 中使用 for 循环加载图像