2016-12-30 13 views
1

これまでは配列を単一の変数と比較していました。プレーヤーの位置:配列を比較する方法(完全一致ではない)

for position in ships: 
    if player_location_x > position[0]-100 and player_location_x < position[0]+100 and player_location_y > position[1]-100 and player_location_y < position[1]+100: 
     #Do something (e.g. draw bullets between the two locations) 

これをどのようにして配列自体の値の比較にも拡張できますか? 「船」のxとyの値を比較して互いの距離をチェックし、xとyの値を「船舶」と「more_ships」の間で比較します。

ships = numpy.array([ 
           [ 
            shuttle_class.npc_x[0], 
            shuttle_class.npc_y[0], 
            shuttle_class.img,  
            shuttle_class.hp 
           ], 

           [ 
            shuttle_class.npc_x[1], 
            shuttle_class.npc_y[1], 
            shuttle_class.img,  
            shuttle_class.hp 
           ], 

           [ 
            shuttle_class.npc_x[2], 
            shuttle_class.npc_y[2], 
            shuttle_class.img,  
            shuttle_class.hp 
           ] 
        ]) 

more_ships = numpy.array([ 
           [ 
            shuttle_class.npc_x[3], 
            shuttle_class.npc_y[3], 
            shuttle_class.img,  
            shuttle_class.hp 
           ], 

           [ 
            shuttle_class.npc_x[4], 
            shuttle_class.npc_y[4], 
            shuttle_class.img,  
            shuttle_class.hp 
           ], 

           [ 
            shuttle_class.npc_x[5], 
            shuttle_class.npc_y[5], 
            shuttle_class.img,  
            shuttle_class.hp 
           ] 
         ]) 
+0

あなたが作ることができる改善の1つは四分木を使用することです。何かが動くたびに、その位置を再計算し、距離を確認するだけで、同じグリッドセクションにあるオブジェクトをチェックするだけですあなたはそうです。 numpyでもっと良い方法があるかもしれませんが、あなたが現在行っている方法では、前にそれを実行することから、それは巨大なスピードの増加を示しています:) – Peter

+0

私はあなたの質問を正しく理解していますか? y1)とmore_ships(配列x2、y2)を使用し、可能な組み合わせごとにペアごとに 'x1-x2'と' y1-y2'を作成しますか? – VBB

+0

@VBBはい、それは正しいです – pytheron

答えて

1

出荷のために2つのアレイx1, y1から始めましょう。それぞれの対の距離をx2, y2で生成したいとします。ディスカッションのために、5つの船と3つの船を持っているとしましょう。だから我々はnumpyの関数meshgridを使用します。

xx1, xx2 = np.meshgrid(x1, x2) # both these are 3x5 arrays 
yy1, yy2 = np.meshgrid(y1, y2) 
dx = xx1 - xx2 # np.abs(xx1 - xx2) if you want just absolute values 
dy = yy1 - yy2 

今すぐあなたの最終的なリストを取得するにはnp.whereを使用することができます。

sel = np.where((dx <= d_max) & (dy <= d_max)) 

selが2xNの配列です。値は条件を満たすn点の指数です。


EDIT:OPが要求するサンプルコードを追加します。

import matplotlib.pyplot as plt 
import numpy as np 

sx = np.array([0, 250, 500]) 
sy = np.array([100, 100, 100]) 
mx = np.array([1000, 0]) 
my = np.array([0,0]) 
plt.scatter(sx, sy) 
plt.scatter(mx, my, c='r', marker='o') 
plt.grid() 

我々は3隻(s)、および2 more_ships(m)を持っています。

xx1, xx2 = np.meshgrid(sx, mx) 
yy1, yy2 = np.meshgrid(sy, my) 

は、私たちは、このビットを調べてみましょう: np.shape(xx1)(2,3)です。最初のインデックスは m(複数の出荷)を指し、2番目のインデックスは sを指します。

dx = np.abs(xx1 - xx2) 
dy = np.abs(yy1 - yy2) 
d_max = 200.0 
sel = np.where((dx <= d_max) & (dy <= d_max)) 
print sel 

selには2つの配列があります。最初の配列は、最初の軸(m)のインデックスを参照し、2番目の配列は(s)を指します。この場合、配列は10の値を持ちます。つまり、more_ships[1]は、ships[0]の200ピクセル以内です。 more_ships[0]ships[2]の200ピクセル以内であることを意味する、とmore_ships[1]ships[0]の近くにある - あなたが変更した場合

sxnp.array([0, 250, 1000])に続いselarray([0, 1]), array([2, 0])になります。

+0

あなたの返信をありがとう。私は申し訳ありませんが、私はこれを実装しようとしましたが、ここで書いたことを頭に入れてはいけません - pygame.draw.lineのようなメソッドを使って例を示すことができますか? 200ピクセルの距離を持つ2本の船舶? – pytheron

+0

x軸とy軸の両方で200ピクセル以下* – pytheron

関連する問題