2017-05-24 72 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