2017-05-24 73 views
0
def pong_collision(paddles, pong, size): 
    ''' paddles and pong out of bounds collison ''' 

    for paddle in paddles: 
     ends = [0, size[0] - paddle.rect.width] 

     # pong|paddle collision 
     if paddle.rect.colliderect(pong.rect): 
      pong.vel.x = -pong.vel.x 
      pong.music.sound.play(loops=0, maxtime=0, fade_ms=0) 


     if (ends[1] <= pong.rect.x or pong.rect.x <= ends[0]): 
     # pong out of bounds collision 

      if pong.rect.x <= ends[0]: 
       # add point to right paddle if pong is < 0 
       if paddle.side == 'right': 
        paddle.text.value += 1 

      if pong.rect.x >= ends[1]: 
       # add poitn to left paddle if pong is > screen size 
       if paddle.side == 'left': 
        paddle.text.value += 1 


      # freezes ball until user starts game 
      pong.state = False 

      # resets pong position to initial 
      pong.rect.topleft = [ 
       (size[0]-pong.rect.width)/2, 
       (size[1]-pong.rect.height)/2 
      ] 

所以我有上面的乒乓碰撞检测,它检测乒乓球到达屏幕边界的时间。假设要发生的是玩家,而不是得分。然后,球暂停并重置到屏幕中间。除了一件事情之外,一切正常,当正确的玩家获得一分时,该分数不会被添加。当乒乓到达屏幕边界时给玩家添加点

我很困惑,为什么会发生这种情况,显然碰撞检测是相同的两个桨,所以为什么不工作?

回答

1

既然你有for paddle in paddles:,如果球超出界限,它会在重复通过两个桨之前被重置,我猜测左边的桨是桨组中的第一个。

解决此问题的一种方法是,当您确定球超出界限以确保两个桨被评估时,将遍历桨。

+0

你是对的,这是它的问题太糟糕了,现在我不得不添加额外的代码来修复它。 –

+0

如果只是附加循环,则代码应该是1个附加行,总体上2个操作(两个if语句),如果使用elif,则可以将其减少为1。 – njoosse