Python 为什么';这个圆不是在碰撞时消失了吗?

Python 为什么';这个圆不是在碰撞时消失了吗?,python,graphics,Python,Graphics,这些似乎是一个稍大的项目的相关部分。这些基本上是碰撞检测功能。气泡是圆的原始列表。我希望碰撞的圆在碰撞时消失。我相当确定我的问题在“if collide==”条件中 def getDistance(point1,point2): a= point1.getX() b= point2.getX() c= point1.getY() d= point2.getY() distance= math.sqrt((b-a)**2 + ((d-c)**2))

这些似乎是一个稍大的项目的相关部分。这些基本上是碰撞检测功能。气泡是圆的原始列表。我希望碰撞的圆在碰撞时消失。我相当确定我的问题在“if collide==”条件中

def getDistance(point1,point2):
    a= point1.getX()
    b= point2.getX()
    c= point1.getY()
    d= point2.getY()
    distance=  math.sqrt((b-a)**2 + ((d-c)**2))
    return distance

def balloonBubbleCollide(balloon,bubble):
    point1 = balloon.getCenter()
    point2= bubble.getCenter()
    distance= getDistance(point1, point2)
    if distance <= 30:
        return True

def check(balloon, bubbles, window):
    for bubble in bubbles:
       collide = balloonBubbleCollide(balloon, bubble)
       if collide == True:
            bubbles.remove(bubble)
def getDistance(点1、点2):
a=point1.getX()
b=point2.getX()
c=point1.getY()
d=point2.getY()
距离=数学sqrt((b-a)**2+((d-c)**2))
返回距离
def BallookBubbleCollide(气泡,气泡):
point1=balloon.getCenter()
point2=bubble.getCenter()
距离=getDistance(点1,点2)

如果距离,则在对列表进行迭代时,不应使用
remove
修改列表

要过滤掉碰撞的气泡,请使用以下内容:

def check(balloon, bubbles, window):
    bubbles[:] = [bubble for bubble in bubbles
                  if not baloonBubbleCollide(balloon, bubble)]

显示的代码将创建要保留的冒泡对象的第一个新列表(使用列表理解),然后立即用它替换
冒泡
列表的当前内容。

不要试图修改您正在迭代的列表。有关您的编码样式的小注释:无需与True进行比较,如果发生碰撞,您可以简单地说:…
。您似乎也非常喜欢“getter”函数,但对简单参数使用属性更具python风格(因此使用
point.x
point.y
balloon.center
,…)。这些可以在以后升级到属性。有趣的@BasSwinckels我一直在接近我在教程中看到的内容。我会记住这一点。@aero26使用大量的getter和setter似乎是你在一年级的课程中教给学生的关于使用Java进行OO设计的东西。这在Python中肯定不常见,请参见例如。我想经验法则是只使用简单参数的属性,如果以后需要添加(例如,设置前简单检查有效性,或获取易于计算的值),则将其更改为属性(用户的语法没有任何更改)。对于“昂贵”计算,请使用get/set methods.Hmmm。这些圆圈似乎还没有消失。