1

我对 Pygame 2(实际上是一般的 python)很陌生,我想为学校制作一个小游戏。我使用了这个代码但是当我按下时我无法让它向光标移动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

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

4

1 回答 1

1

编写一个“跟随一个目标”的函数:

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()TrueFalsepygame.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()
于 2020-12-25T16:21:05.927 回答