rect 与 rect 列表的冲突 [英] rect collision with list of rects

查看:37
本文介绍了rect 与 rect 列表的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码 player_rect.colliderect(tile_rects): 其中 player_rect 是单个 Recttile_rectsRect 的列表.
我收到错误 `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 Rects.
I get the error `builtins.TypeError:

参数必须是矩形样式对象

Argument must be rect style object

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

when I try to run my code (presumably as the code doesn't like having a list of rects over a single rect).

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

I also just found out than when I switch the positions of tile_rects and player_rectI instead get the error

builtins.AttributeError: 'list' 对象没有属性 'colliderect'

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

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

My question is, how can I change my code so that I can check for collisions with a rect and a list of rects?

推荐答案

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

Use pygame.Rect.collidelist to test whether a rectangle collides with one of a list of rectangles.

collidelist:

测试矩形是否与矩形序列中的任何一个发生碰撞.返回找到的第一个碰撞的索引.如果未发现冲突,则返回索引 -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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆