2011-06-26 99 views
-4

我很累,而且我一直試圖運行這一整天。 IT是一種乒乓球遊戲,球從牆上反彈並與用戶的「板」或槳接觸。我已經花了一段時間來調試,結果發現我的iCollide和iGet_Velocity部件導致了很多問題。在Python中調試乒乓遊戲

from livewires import games, color 
games.init (screen_width = 640, screen_height = 480, fps = 50) 

class Ball (games.Sprite): 
    def update (self): 
     if self.right>544: 
      self.dx = -self.dx 
     if self.top > 11 or self.bottom > 470: 
      self.dy = -self.dy 
     if self.left < 0: 
      self.iLost() 

    def iLost (self): 
     games.Message (screen = self.screen, 
         x = 340, 
         y = 240, 
         text = "Game Over", 
         size = 90, 
         color = color.red, 
         lifetime = 250, 
         after_death = self.screen.quit)      

    def ihandle_collision (self): 
     new_dx, new_dy = Slab.iVelocity() 
     self.dx += self.dx + new_dx #For handling collision, must take velocity of the mouse object and put add it to the velocity of the ball. 
     self.dy += self.dy + new_dy 

class Slab (games.Sprite): 
    def update (self): 
     self.x = games.mouse.x 
     self.y = games.mouse.y 
     self.iCollide() 

    def iCollide (self): 
     for Ball in self.overlapping_sprites: 
      Ball.ihandle_collision() 

    def iVelocity (self): 
     self.get_velocity() 


def main(): 
    #Backgrounds 
    pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False) 
    games.screen.background = pingpongbackground 
    #Ball: Initializing object and setting speed. 
    ballimage = games.load_image ("pingpongball.jpg", transparent = True) 
    theball = Ball (image = ballimage, 
        x = 320, 
        y = 240, 
        dx = 2, 
        dy = 2) 
    games.screen.add(theball) 
    #Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position 
    slabimage = games.load_image ("pingpongpaddle.jpg", transparent = True) 
    theslab = Slab (image = slabimage, 
        x = games.mouse.x, 
        y = games.mouse.y) 
    games.screen.add(theslab) 
    games.mouse.is_visible = False 
    games.screen.event_grab = True 
    games.screen.mainloop() 

main() 
+1

你能提供一些關於你所看到的具體問題的細節嗎?否則它不是一個問題和更多的代碼審查 – lunixbochs

+0

對不起。在與槳接觸後,遊戲結冰。 – Louis93

回答

3

好了,無需更多的數據,我不能肯定地說什麼是錯的。但它看起來像你的碰撞響應代碼有點棘手。例如,在第一部分中,您應該這樣做來正確處理您的邊界條件:

def update (self): 
    if self.right>544 and self.dx > 0: 
     self.dx = -self.dx 
    if (self.top > 11 and self.dy < 0) or (self.bottom > 470 and self.dy > 0): 
     self.dy = -self.dy 
    if self.left < 0: 
     self.iLost() 

此外,您的碰撞響應代碼有點過分。這是未經測試,但它大致正確的事:(編輯:重寫了這個與更多的評論,這樣它會更清楚)

def iCollide (self): 
    for Ball in self.overlapping_sprites: 

     #Extract the components of the slab's velocity 
     slab_vx, slab_vy = self.iVelocity() 

     #Compute relative components of slab velocity 
     rel_vx, rel_vy = Ball.dx - slab_vx, Ball.dy - slab_vy 

     #Determine the time of intersection and the normal direction 
     #This is done to compute which face the ball hits 
     #Initially suppose that the objects intersect at t=infinity 
     t, nx, ny = 100000, 0, 0 

     #Check left/right faces 
     if rel_vx != 0: 
      #Check if ball hits left face 
      toi = (self.left - Ball.right)/rel_vx 
      if toi < t: 
       t, nx, ny = toi, -1, 0 

      #Check if ball hits right face 
      toi = (self.right - Ball.left)/rel_vx 

      if toi < t: 
       t, nx, ny = toi, 1, 0 

     #Check top/bottom faces: 
     if rel_vy != 0: 

      #Check if ball hits top face 
      toi = (self.top - Ball.bottom)/rel_vx 
      if toi < t: 
       t, nx, ny = toi, 0, -1 

      #Check if ball hits right face 
      toi = (self.bottom - Ball.top)/rel_vx 
      if toi < t: 
       t, nx, ny = toi, 0, 1 

     #Check if impact force properly corrects velocity 
     if Ball.dx * nx + Ball.dy * ny < 0: 

      #Reflect the ball's position 
      Ball.dx += Ball.dx * nx * 2.0 
      Ball.dy += Ball.dy * ny * 2.0 

     #Check if cursor movement is not pushing ball into paddle: 
     if slab_vx * nx + slab_vy * ny < 0: 
      Ball.dx += slab_vx 
      Ball.dy += slab.vy 

你應該嘗試做的是找到過板坯球的臉相交,然後根據該方向反映球的速度。現在這個代碼沒有經過測試,因爲我無法讓你的例子運行。但是它應該讓你知道正確的解決方案應該做什麼。

+0

嘿,你介意解釋計算時間部分嗎?不應該是slab.iVelocity,因爲我們希望球具有板坯擊中它的速度? – Louis93

+0

偉大的,很好的答案。我覺得很有趣,你能夠像以前那樣快速地輸入這個功能。它沒有執行到完美,但其他函數中還有一些其他錯誤,但我現在理解它背後的邏輯。非常感謝! – Louis93

3

這兩條線看潦草:

self.dx += self.dx + new_dx 
    self.dy += self.dy + new_dy 

嘗試:

self.dx += new_dx 
    self.dy += new_dy 
+0

我解決了這個問題,但是當我這樣做的時候忘了保存它。我更關心物體是否在邏輯上與另一個進行通信...... – Louis93

+0

儘管如此,它仍然無法工作。球會通過一個靜止的光標。 – Mikola