编写一个“跟随一个目标”的函数:
def FollowMe(pops, fpos, step):
# [...]
我建议计算飞船和鼠标之间的距离以及从 ( fpos
) 到 ( pops
) 的单位方向向量。
距离可以通过计算欧几里得距离得到。Pygame 提供distance_to()
了这一点。单位方向向量可以通过将方向向量除以距离或通过归一化 ( normalize()
) 方向向量来计算:
target_vector = pygame.math.Vector2(pops)
follower_vector = pygame.math.Vector2(fpos)
distance = follower_vector.distance_to(target_vector)
direction_vector = target_vector - follower_vector
if distance > 0:
direction_vector /= distance
现在您可以定义一个精确的step_distance
并沿精灵方向移动到跟随者:
if distance > 0:
new_follower_vector = follower_vector + direction_vector * step_distance.
定义 astep
并确保步距不大于步距:
step_distance = min(distance, step)
最大步距为
max_step = distance - minimum_distance
把它们放在一起:
def FollowMe(pops, fpos, step):
target_vector = pygame.math.Vector2(pops)
follower_vector = pygame.math.Vector2(fpos)
new_follower_vector = pygame.math.Vector2(fpos)
distance = follower_vector.distance_to(target_vector)
if distance > 0:
direction_vector = (target_vector - follower_vector) / distance
step_distance = min(distance, step)
new_follower_vector = follower_vector + direction_vector * step_distance
return (new_follower_vector.x, new_follower_vector.y)
另请参阅如何在 pygame 中进行平滑移动。
使用该功能可沿鼠标方向移动太空船,并在按下 pygame.key.get_pressed()
时移动飞船。返回一个包含每个键状态的列表。如果按住某个键,则该键的状态为,否则为。用于评估按钮的当前状态并获得连续移动:SPACEpygame.key.get_pressed()
True
False
pygame.key.get_pressed()
ship_pos = display_width // 2, display_height // 2
while True:
# [...]
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
ship_pos = FollowMe(pos, ship_pos, 3)
angle = 270-math.atan2(pos[1]-ship_pos[1],pos[0]-ship_pos[0])*180/math.pi
rotimage = pygame.transform.rotate(space_ship,angle)
rect = rotimage.get_rect(center=ship_pos)
有关图像的旋转,请参阅如何将图像(播放器)旋转到鼠标方向?以及如何使用 PyGame 围绕其中心旋转图像?
另请参阅分别向目标或鼠标旋转 向目标移动和问题的答案如何向鼠标旋转精灵并移动它?
不使用任何外部资源(声音和图像)的最小示例:
import sys, pygame, math
from pygame.locals import *
pygame.init()
def FollowMe(pops, fpos, step):
target_vector = pygame.math.Vector2(pops)
follower_vector = pygame.math.Vector2(fpos)
new_follower_vector = pygame.math.Vector2(fpos)
distance = follower_vector.distance_to(target_vector)
if distance > 0:
direction_vector = (target_vector - follower_vector) / distance
step_distance = min(distance, step)
new_follower_vector = follower_vector + direction_vector * step_distance
return (new_follower_vector.x, new_follower_vector.y)
display_width = 1280
display_height = 720
screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Space Survival")
clock = pygame.time.Clock()
bk = pygame.Surface(screen.get_size())
space_ship = pygame.Surface((20, 50), pygame.SRCALPHA)
space_ship.fill((0, 255, 0))
mousec = pygame.Surface((20, 20), pygame.SRCALPHA)
mousec.fill((255, 0, 0))
ship_pos = display_width // 2, display_height // 2
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pos = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
ship_pos = FollowMe(pos, ship_pos, 3)
angle = 270-math.atan2(pos[1]-ship_pos[1],pos[0]-ship_pos[0])*180/math.pi
rotimage = pygame.transform.rotate(space_ship,angle)
rect = rotimage.get_rect(center=ship_pos)
screen.blit(bk, (0, 0))
screen.blit(rotimage,rect)
screen.blit(mousec, pos)
pygame.display.update()