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

问题描述

我有代码 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 Rects.
I get the error `builtins.TypeError:

当我尝试运行我的代码时(大概是因为代码不喜欢在单个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_rectI instead get the error

我的问题是,如何更改代码,以便可以检查与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?

推荐答案

使用 pygame.Rect.collidelist ,以测试矩形是否与矩形列表之一冲突.

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

collidelist :

if player_rect.colliderect(tile_rects) >= 0:
    # [...]

这篇关于rect与rect列表冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:41