2016-12-18 12 views
2

これについて多くの質問があります。しかし、誰も私の問題を具体的に解決する答えはありません。私はこれを一日中Googleにしようとしました。Python - 私の宇宙船が向いている方向(角度)の弾を撃ちます

私の問題は簡単です。

私はこの宇宙船を持ち歩くことができます。私はすでに、それが向いている方向、すなわち向いている方向を追跡しています。例えば、船下の画像は約45度を見出しているにそれは私がちょうど弾丸が前方にまっすぐに行くようにする必要があり、0°(トップを開始し、時計回りに行く)から359°

enter image description here

に行きます今SH

class Projectile(object) : 

    def __init__(self, x, y, vel, screen) : 
     self.screen = screen 
     self.speed = 1 #Slow at the moment while we test it 
     self.pos = Vector2D(x, y) 
     self.velocity = vel #vel constructor parameter is a Vector2D obj 
     self.color = colors.green 

    def update(self) : 
     self.pos.add(self.velocity) 

    def draw(self) : 
     pygame.draw.circle(self.screen, self.color, self.pos.int().tuple(), 2, 0) 

:方向私の宇宙船は、直面Xから開始している(見出し)、Yは自分の宇宙船が現在

発射クラスである座標私の船のクラスのOOT方法:

class Ship(Polygon) : 

    # ... A lot of ommited logic and constructor 

    def shoot(self) : 
     p_velocity = # .......... what we need to find 
     p = Projectile(self.pos.x, self.pos.y, p_velocity, self.screen) 
     # What next? 
+0

ためのロジックです'self.pos'を更新しますか?おそらく最後の2つの位置のリストを保持し、それらから速度を計算しますか? – jmunsch

+0

@jmunsch私は正しく理解したとは思わない。どちらのクラスもプロパティを持ちます。これは単に画面上の位置です。各フレームにベロシティ値を追加して更新します –

+0

Vector2Dにはどのライブラリをインポートしますか? – eyllanesc

答えて

1

船角度を考えるには、試してみてください。

class Projectile(object) : 
    def __init__(self, x, y, ship_angle, screen) : 
     self.screen = screen 
     self.speed = 5 #Slow at the moment while we test it 
     self.pos = Vector2D(x,y) 
     self.velocity = Vector2D().create_from_angle(ship_angle, self.speed, return_instance=True) 
     self.color = colors.green 

    def update(self) : 
     self.pos.add(self.velocity) 

    def draw(self) : 
     pygame.draw.circle(self.screen, self.color, self.pos.int().tuple(), 2, 0) 

enter image description here

Vector2Dの関連部分:

def __init__(self, x = 0, y = 0) : # update to 0 
    self.x = x 
    self.y = y 

def create_from_angle(self, angle, magnitude, return_instance = False) : 
    angle = math.radians(angle) - math.pi/2 
    x = math.cos(angle) * magnitude 
    y = math.sin(angle) * magnitude 
    print(x, y, self.x, self.y, angle) 
    self.x += float(x) 
    self.y += float(y) 
    if return_instance : 
     return self 
+0

これを実行したときの動作を教えてください –

+0

ここで起こっているのは、船がどこにあるのではなく、0,0座標で弾丸が作成されているということです:s –

+0

http://imgur.com/a/8zl4Fこのイメージをチェックして、私が何を意味するのかを確認してください。 –

関連する問題