rect与rect列表冲突 [英] rect collision with list of rects
问题描述
我有代码 player_rect.colliderect(tile_rects):
,其中 player_rect
是单个 Rect
,而 tile_rects
是 Rect
s的列表.
我收到错误`builtins.TypeError:
I have the code player_rect.colliderect(tile_rects):
where player_rect
is a single Rect
, and tile_rects
is a list of Rect
s.
I get the error `builtins.TypeError:
参数必须是矩形样式对象
Argument must be rect style object
当我尝试运行我的代码时(大概是因为代码不喜欢在单个rect上有一个rect列表).
when I try to run my code (presumably as the code doesn't like having a list of rects over a single rect).
我还发现,当我切换 tile_rects
和 player_rect
的位置时,我得到了错误
I also just found out than when I switch the positions of tile_rects
and player_rect
I instead get the error
builtins.AttributeError:列表"对象没有属性"colliderect"
builtins.AttributeError: 'list' object has no attribute 'colliderect'
我的问题是,如何更改代码,以便可以检查与rect和rect列表的冲突?
My question is, how can I change my code so that I can check for collisions with a rect and a list of rects?
推荐答案
Use pygame.Rect.collidelist
to test whether a rectangle collides with one of a list of rectangles.
测试矩形是否与矩形序列中的任何一个发生碰撞.返回找到的第一个碰撞的索引.如果未发现冲突,则返回索引-1.
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.colliderect(tile_rects) >= 0:
# [...]
这篇关于rect与rect列表冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!