有很多关于此的问题。但他们都没有解决我的问题的具体解决方案,我试图谷歌这一整天。Python - 在我的飞船正面临的方向(角度度数)上拍摄子弹
我的问题很简单。
我有这个太空船,我可以移动和旋转,我已经跟踪它的标题,它面对的方向。例如船在下面的图片是标题大约45度它从0°(从顶部和去顺时针)至359°
我只是需要让子弹径直向前方向(航向)我的飞船正面临着从X开始,Y坐标我的飞船是目前
弹丸类:
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)
现在SH我的船级的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?
如果是逻辑更新'self.pos'?也许保留最后2个位置的列表并计算它们的速度? – jmunsch
@jmunsch我不认为我理解正确。两个类都有一个属性,它们只是它们在屏幕上的位置。它们通过在每帧中添加速度值进行更新 –
您为Vector2D导入了什么库? – eyllanesc