知道哪个矩形在碰撞列表中发生碰撞
Know which rect has been collided in a collidelist
当我的子弹碰到矩形时,我试图在碰撞列表中找到一个特定的矩形。
我有 92 个矩形,每个矩形都有一个类型:"Opaque"、"Transparent"。
当我在游戏启动时创建矩形时,我将它们放在一个列表中,以便在测试碰撞列表之后。另一个列表按顺序包含所有类型。
喜欢
collide_wall_list = [rect,rect,rect,rect]
#Each rect is a pygame.draw.rect
wall_type_list = ["Opaque","Opaque","Opaque","Transparent"]
它们是按顺序创建的,所以第二个矩形是第二个类型。
if new_bullet_list[n].collidelist(collide_wall_list) != -1:
这是为了测试我的子弹是否与矩形列表发生碰撞。
现在,问题是使用碰撞列表,我们无法知道哪个矩形正在碰撞。
你知道如何知道哪些 rects 正在碰撞吗?然后对于类型它会很容易,因为 rects 和类型在相同的位置。
collidelist returns 第一次碰撞的索引,如果没有发生碰撞则为 -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."
好吧,你显然有一个检查是否发生碰撞的函数,returns -1?我不知道。但这是我的想法;试着让你的函数看起来像这样:
int IsCollision( "all your variables" + int n) { // n = the current rect's number
if ("collision" == true) {
return n;
} else {
return 0;
}
}
当然,现在您需要将另一行更改为:
if new_bullet_list[n].collidelist(collide_wall_list) != 0: //colidelist will return "n", so the number of your rectangle. If that number is not equal to zero, a collision happened with your n'th rect.
当我的子弹碰到矩形时,我试图在碰撞列表中找到一个特定的矩形。 我有 92 个矩形,每个矩形都有一个类型:"Opaque"、"Transparent"。 当我在游戏启动时创建矩形时,我将它们放在一个列表中,以便在测试碰撞列表之后。另一个列表按顺序包含所有类型。 喜欢
collide_wall_list = [rect,rect,rect,rect]
#Each rect is a pygame.draw.rect
wall_type_list = ["Opaque","Opaque","Opaque","Transparent"]
它们是按顺序创建的,所以第二个矩形是第二个类型。
if new_bullet_list[n].collidelist(collide_wall_list) != -1:
这是为了测试我的子弹是否与矩形列表发生碰撞。 现在,问题是使用碰撞列表,我们无法知道哪个矩形正在碰撞。 你知道如何知道哪些 rects 正在碰撞吗?然后对于类型它会很容易,因为 rects 和类型在相同的位置。
collidelist returns 第一次碰撞的索引,如果没有发生碰撞则为 -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."
好吧,你显然有一个检查是否发生碰撞的函数,returns -1?我不知道。但这是我的想法;试着让你的函数看起来像这样:
int IsCollision( "all your variables" + int n) { // n = the current rect's number
if ("collision" == true) {
return n;
} else {
return 0;
}
}
当然,现在您需要将另一行更改为:
if new_bullet_list[n].collidelist(collide_wall_list) != 0: //colidelist will return "n", so the number of your rectangle. If that number is not equal to zero, a collision happened with your n'th rect.