2017-01-31 13 views
-1

私は、リファクタリングしようとしているpygameアプリケーションのWASDの方向キーのメインループに次の重複コードを持っています。最善の方法を見つけようと努力しています。私は、PLAYERクラスp GAMEクラスgとWORLDクラスwのインスタンスを作成しました。Python pygameリファクタリング...メソッドにコードをラップする

と呼ばれるGAMEクラスのメソッドを理想的に作りたいと思います。私が反対している主な問題は、 - =または+ =でなければならないためです。別のWASDキーのif文全体を記述することなく、それを実行するのは難しいです。より良い方法が必要です。

 if event.key == pygame.K_w: 
      p.fov.y -= g.pixelUpdate 
      for ent in w.entityList: 
      ent.rect.y += g.pixelUpdate 
      ent.collisionRect.y += g.pixelUpdate 
      for ent in w.ents_in_fov: 
      if p.collision(ent): 
       p.collided = True 
       for ent in w.entityList: 
       ent.rect.y -= g.pixelUpdate 
       ent.collisionRect.y -= g.pixelUpdate 
       p.fov.y += g.pixelUpdate 

     elif event.key == pygame.K_d: 
      p.fov.x += g.pixelUpdate 
      for ent in w.entityList: 
      ent.rect.x -= g.pixelUpdate 
      ent.collisionRect.x -= g.pixelUpdate 
      for ent in w.ents_in_fov: 
      if p.collision(ent): 
       p.collided = True 
       for ent in w.entityList: 
       ent.rect.x += g.pixelUpdate 
       ent.collisionRect.x += g.pixelUpdate 
       p.fov.x -= g.pixelUpdate 

     elif event.key == pygame.K_s: 
      p.fov.y += g.pixelUpdate 
      for ent in w.entityList: 
      ent.rect.y -= g.pixelUpdate 
      ent.collisionRect.y -= g.pixelUpdate 
      for ent in w.ents_in_fov: 
      if p.collision(ent): 
       p.collided = True 
       for ent in w.entityList: 
       ent.rect.y += g.pixelUpdate 
       ent.collisionRect.y += g.pixelUpdate 
       p.fov.y -= g.pixelUpdate 

     elif event.key == pygame.K_a: 
      p.fov.x -= g.pixelUpdate 
      for ent in w.entityList: 
      ent.rect.x += g.pixelUpdate 
      ent.collisionRect.x += g.pixelUpdate 
      for ent in w.ents_in_fov: 
      if p.collision(ent): 
       p.collided = True 
       for ent in w.entityList: 
       ent.rect.x -= g.pixelUpdate 
       ent.collisionRect.x -= g.pixelUpdate 
       p.fov.x += g.pixelUpdate 
+0

おいおいへのアクセスを取得するupdatePositions(x, y, p, w)が必要な場合がありますその恐ろしい繰り返しコード – zzoop

+0

ヒント: 'a - = b'の代わりに' a + = -b'を使うことができるので、 'a + = x'で一つの関数を作成し、' x = x = -b' – furas

答えて

0

ヒント1:の代わりa -= bあなたはa += -b

ヒント2使用することができます。あなたはあなたが

として updatePositions(x, y)を作成し、実行することができ 0

p.fov.x += 0 
p.fov.y += g.pixelUpdate 

p.fov.x += g.pixelUpdate 
p.fov.y += 0 

値を使用することができますが

if event.key == pygame.K_w: 
    updatePositions(0, -g.pixelUpdate) 

elif event.key == pygame.K_s 
    updatePositions(0, g.pixelUpdate) 

elif event.key == pygame.K_d: 
    updatePositions(g.pixelUpdate, 0) 

elif event.key == pygame.K_a: 
    updatePositions(-g.pixelUpdate, 0) 

EDIT:このような何か:ところで

def updatePositions(x, y):  
     p.fov.x += x 
     p.fov.y += y 

     for ent in w.entityList: 
     ent.rect.x -= x 
     ent.rect.y -= y 
     ent.collisionRect.x -= x 
     ent.collisionRect.x -= y 

     for ent in w.ents_in_fov: 
     if p.collision(ent): 
      p.collided = True 
      for ent in w.entityList: 
      ent.rect.x += x 
      ent.rect.y += y 
      ent.collisionRect.x += x 
      ent.collisionRect.y += y 
      p.fov.x -= x 
      p.fov.y -= y 

それがすべてで見て、noobのを助けるpw

+0

これは完璧です、これは私が必要としていたものです。私は 'x + = -1'と' x - = 1'を同じにすることができるのか分かりませんでした。そして、それは私の頭の中で大規模な光に変わった。ありがとうございました! – zzoop

関連する問題