2016-01-13 44 views
4

第一次海報在這裏。pygame - 敵人與地面碰撞時飛出屏幕?

儘量保持這一點儘可能簡單。我試圖在pygame中做一個遊戲,但似乎我的碰撞檢測正在發生。我有一個玩家類,可以檢測玩家是否與地面或環境列表中的任何其他物體發生碰撞,並且我有一個與同一列表中的任何物品發生碰撞的敵人類。爲了嘗試擊中玩家,敵人確定了他們需要前進的方向。有很可能在問題中起重要作用的重力。

問題是,當'蒼蠅'被放入並落在地上時,即使它們的X和Y值看起來(根據屏幕上物品的日誌)不會移動所有?

作爲澄清,這兩個'蒼蠅'被放入第二個列表允許碰撞檢測。此外,作爲一個側面說明,「毛刺」不一樣,如果沒有左,右碰撞檢測,我認爲你的if/else不正確,會出現...由於任何人誰可以提供幫助:)

def collisions(): 

#Detection Working as intended 
    for fly in Fly.List: 
     fly_proj = pygame.sprite.spritecollide(fly, Projectile.List, True) 
     if len(fly_proj) > 0: 
      for hit in fly_proj: 
       fly.health -= 100 

     X = pygame.sprite.spritecollide(fly, current_level.object_list, False) 
     if len(X) == 0: #Gravity here: 
      if (fly.vspeed == 0): 
       fly.vspeed = 1 
       print("Gravity") 
      else: 
       fly.vspeed += 0.35 
     if len(X) > 1: 
      for hit in X: 

       if fly.vspeed > 0:    
        fly.rect.bottom = hit.rect.top +1 
        fly.vspeed = 0 
       elif fly.vspeed < 0:  
        fly.rect.top = hit.rect.bottom -1 
       elif fly.hspeed > 0: 
        fly.rect.right = hit.rect.left 
        fly.hspeed = 0 
       elif fly.hspeed < 0:    
        fly.rect.left = hit.rect.right 
        fly.hspeed = 0 

     print(len(X),framecounter, fly.vspeed, fly.hspeed) 
#Kill fly if health reaches 0    
     if fly.health <= 0: 
      fly.destroy(Fly) 

#Detect where the player is 
     if window_rect.contains(fly.rect): 
      fly.playersnapshot = player.rect.x 
     if fly.rect.x - player.rect.x >= -10: 
      #print("LEFTING") 
      fly.hspeed = -2 
     if fly.rect.x - player.rect.x <= 10: 
      fly.hspeed = 2 
     fly.truefalse = True 
     event = None 

     fly.rect.y += fly.vspeed 
     fly.rect.x += fly.hspeed 
+0

可以同時飛'hspeed!= 0'和'vspeed!= 0'嗎?現在只有在'vspeed'爲零的情況下才檢查'hspeed'。 – furas

回答

1

也許當飛觸地面設置vspeed爲零,然後if/else檢查hspeed它使用地面left/right改變飛行left/right

我知道一種方法。

  1. 招飛垂直
  2. 檢查碰撞和使用if/elsevspeed
  3. 招飛水平
  4. 檢查碰撞和使用if/elsehspeed

-

編輯:

# move horizontally only 

fly.rect.x += fly.hspeed 

X = pygame.sprite.spritecollide(...) 

for hit in X: 
    if fly.hspeed > 0: 
     fly.rect.right = hit.rect.left 
    else: 
     fly.rect.left = hit.rect.right 

# move vertically only 

fly.rect.y += fly.vspeed 

X = pygame.sprite.spritecollide(...) 

for hit in X: 
    if fly.vspeed > 0: 
     fly.rect.bottom = hit.rect.top 
    else: 
     fly.rect.top = hit.rect.bottom 

    # on_ground = True 

我發現在「平臺跳線」的源代碼,這個方法對ProgramArcadeGames.com

看到頁面:platform_jumper.py

+0

謝謝,很快就會試用並更新結果:) –

+0

@ThomasDebenham請參閱代碼 – furas

+0

請參閱源代碼中的update():http://programarcadegames.com/python_examples/f.php?file = platform_jumper.py - 它使用重力。 – furas

0

感謝furas爲我提供的東西去了,我已經想通了什麼問題是。對於那些有興趣誰,代碼代替:

   if fly.vspeed > 0:    
        fly.rect.bottom = hit.rect.top +1 
        fly.vspeed = 0 
       elif fly.vspeed < 0:  
        fly.rect.top = hit.rect.bottom -1 
       elif fly.hspeed > 0: 
        fly.rect.right = hit.rect.left 
        fly.hspeed = 0 
       elif fly.hspeed < 0:    
        fly.rect.left = hit.rect.right 
        fly.hspeed = 0 

有:

   fly.rect.y += fly.hspeed 
       if fly.vspeed > 0: 
        fly.rect.bottom = hit.rect.top 
       else: 
        fly.rect.top = hit.rect.bottom 

       if fly.rect.bottom == hit.rect.top: 
        fly.vspeed = 0 
       if fly.vspeed == 0: 
        fly.rect.x += fly.vspeed 
        if fly.hspeed > 0: 
         fly.rect.right = hit.rect.left 
        else: 
         fly.rect.left = hit.rect.right 

,但現在我已經在那裏飛對象失控墜落(vspeed不斷增加)問題,罪魁禍首是這個的代碼量小:

if self.vspeed == 0: 
    self.vspeed = 1 
else: 
    self.vspeed += 0.35 #Rate of gravity 

如果任何人有任何線索,這裏發生了什麼,請不要猶豫,答案:)

回覆個

UPDATE

由於堆到Furas誰主要做所有的工作對我來說:d謝謝你教我一些新的東西:)

對於那些有興趣誰最終工作代碼是這樣的:(on_ground )在調用colllisions()函數時調用。

 fly.rect.y += fly.vspeed 

     X = pygame.sprite.spritecollide(fly, current_level.object_list, False) 

     for hit in X: 
      if fly.vspeed > 0: 
       fly.rect.bottom = hit.rect.top 
       on_ground = True 
      else: 
       fly.rect.top = hit.rect.bottom 

     # move horizontally only 

     fly.rect.x += fly.hspeed 

     X = pygame.sprite.spritecollide(fly, current_level.object_list, False) 

     for hit in X: 
      if fly.hspeed > 0: 
       fly.rect.right = hit.rect.left 
      else: 
       fly.rect.left = hit.rect.right 

     if on_ground == True: 
      fly.vspeed = 0 
      #print("WAT2") 
     else: 
      fly.vspeed += 0.35 
+0

我很確定你的新代碼幾乎在所有地方都混合了它的''''''''''。 「x」應該是水平的,「y」是垂直的。 – Blckknght

+0

停止增加速度使用例如'如果self.vspeed> 3:self.vspeed = 3' – furas

+0

它看起來對我來說是正確的?任何具體的例子?新的代碼是基於Furas發佈的邏輯,如果這有助於任何事情:) –