2016-04-06 6 views
0
def checkCollision(self): 
     for entity in self.Entities: 
      ox = entity.x 
      oy = entity.y 
      for block in self.Blocks: 

       if block.y < entity.y and entity.y < block.y + block.size: 
        if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x): 
         print ("Hit Bottom") 
         entity.tpEntity(ox, oy+0.5) 
       elif entity.y < block.y and block.y < entity.y+entity.height: 
        if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x): 
         print ("Hit Top") 
         entity.tpEntity(ox, oy-self.gravity) 
       elif entity.x < block.x and block.x < entity.x+entity.width: 
        if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height): 
         print("Hit Left") 
         entity.tpEntity(ox-0.5,oy) 
       elif entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x: 
        if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height): 
         print("Hit Right") 
         entity.tpEntity(ox+0.5,oy) 

私はこの奇妙なバグを抱えています。私は壁に歩いていると、常にその上にあるかのようにそれを登録します。誰が私になぜこれが起こっているのかを説明することができますか、どのように私は/それを修正する必要がありますか?ありがとうございました:)壁に奇妙な歩き方バグ、衝突系のパイゲーム

EDIT:self.gravity変数は、各惑星/地図上で異なっているはずの惑星/地図上の重力です。既定値(ここ)は0.2です。

+0

ロジックエラーであるため、バグを追跡するのは少し難しいでしょう。このバグを掘り下げるための良い戦略は、各条件ブロック内のすべてのxとyの値を出力することです。これは、あなたがそのエラーを取得したときのblock.x、entity.x、block.yなどが何であるかを理解するのに役立ちます。だから、あなたが壁に歩いて行くときに、意図したif/elif文を満たすかどうかを確認することができます。 –

+0

'item.x + item.width'のようなものは単純化して、はるかに読みやすく分かりやすくすることができますので、問題を簡単に特定できます。任意のrectオブジェクトは、topleft、topright、bottomleftなどの属性を持っています。rectページを確認してください。http://www.pygame.org/docs/ref/rect.html – Keatinge

+0

私は強くpygame.sprite.collideanyメソッドを使用することをお勧めします。 1つの単純な行でこのコード全体を実行します。 –

答えて

0

エンティティとブロックがクラスであると仮定していますか?

def checkCollision(self): 
    for entity in self.Entities: 
     ox = entity.x 
     oy = entity.y 
     for block in self.Blocks: 
      if entity.rect.colliderect(block.rect): 

をして、反射を決定するために、xとyの位置をテスト:あなたはpygame.Rectオブジェクトでそれぞれ1を設定し、事業体が動くときにそれを更新した場合は、実行して、これを簡素化することができます。