问题描述
我想在这个碰撞模拟中添加多个矩形,我不熟悉 OOP,我知道这是解决这个问题的最佳解决方案,以免重复相同的代码。 到目前为止,这是我的代码:
version.sbt
它还没有使用 OOP。
解决方法
创建矩形列表:
other_rect_list = [other_rect1,other_rect2,other_rect3]
使用 collidelist()
o 找到列表中第一个与矩形碰撞的矩形的索引:
index = moving_rect.collidelist(other_rect_list)
if index >= 0:
other_rect = other_rect_list[index]
# [...]
或者,您可以使用 collidelistall()
获取所有碰撞矩形的索引列表:
index_list = moving_rect.collidelistall(other_rect_list)
for index in index_list:
other_rect = other_rect_list[index]
# [...]