给定船舶角度,尝试:
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)
的相关部分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