如何移动鼠标指针并同时指向它?

How do I move to the mouse pointer and point towards it at the same time?

我是 Pygame 2 的新手(实际上 python 一般而言),我想为学校制作一个小游戏。我使用了 this 中的代码,但是当我按下 SPACE 时,我无法让它向光标移动。我不希望它在 x/y 轴上移动... 这是代码 btw

import sys, pygame, math
from pygame.locals import *
spaceship = ('spaceship.png')
mouse_c = ('cursor.png')
backg = ('background.jpg')
lazer = pygame.mixer.Sound("pew.WAV")
pygame.init()

display_width = 1280
display_height = 720
screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Space Survival")

bk = pygame.image.load(backg).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
Ammo = 20
Fuel = 200

while True:
    for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    elif event.type == MOUSEBUTTONDOWN and event.button == 1 and Ammo <= 20:
        print("Pew")
        pygame.mixer.Sound.play(lazer)
    elif event.type == MOUSEBUTTONDOWN and event.button == 3 and Fuel <= 200:
        print("Boost")

screen.blit(bk, (0, 0))
pos = pygame.mouse.get_pos()
angle = 270-math.atan2(pos[1]-(display_height/2),pos[0]-(display_width/2))*180/math.pi
rotimage = pygame.transform.rotate(space_ship,angle)
rect = rotimage.get_rect(center=((display_width/2),(display_height/2)))
screen.blit(rotimage,rect) #I need space_ship to rotate towards my cursor
screen.blit(mousec, (pos))
pygame.display.update()
clock.tick(60)   #FPS

运行 Pygame 2.0 在 Python 3.8。 任何提示将不胜感激:)

写一个函数,“follows2 一个目标:

def FollowMe(pops, fpos, step):
    # [...]

我建议计算space飞船和鼠标之间的距离以及从(fpos)到(pops)的单位方向向量。
距离可以通过计算Euclidean distance. Pygame provides distance_to() for that. Th unit direction vector can be computed by dividing the direction vector by the distance or by normalizing (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.

定义一个step并保证步长距离不大于step:

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)

另见 How to make smooth movement in pygame

使用函数使space船向鼠标方向移动,pygame.key.get_pressed()SPACE时移动船按下。 pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use 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)

有关图像的旋转,请参阅 and How do I rotate an image around its center using PyGame?

另见 Rotate towards target or mouse respectivley Move towards target and the answer to the question


不使用任何外部资源(声音和图像)的最小示例:

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()