Pygame中如何让图片面向鼠标并用按键移动?

How to Make an Image Face The Mouse and Move With Keys in Pygame?

这听起来可能令人困惑,所以我会尽力描述。

我写了一些简单的代码,在 window 中间有一个图像,当鼠标在 window.这很好,但我在尝试使用 WASD 移动图像时遇到了很大的麻烦。

我想为图像添加基本的自上而下的 WASD 移动,但无论我尝试什么它总是 'overwritten'(我认为)通过旋转代码使其无法移动并停留在图像的直接中间屏幕.

我能做的最好的就是将它从中间向任何方向移动一个设定的量(比如移动 1 个像素),但是如果我按住键,它会保持从中间 1 个像素,并且我一松手,它就再次居中。 这是我深入了解的代码,但它仍然无法正常工作。你可以自己测试你只需要改变'cursor'图像就可以了。

import math, pygame

pygame.init()
window = pygame.display.set_mode((1280,800))
cursor = pygame.image.load("images/cursor.png").convert_alpha()

#   0 - image is looking to the right
#  90 - image is looking up
# 180 - image is looking to the left
# 270 - image is looking down
correction_angle = 90

run = True
while run:
    pygame.time.delay(2)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #cursor postion and rectangle
    cursor_pos  = window.get_rect().center
    cursor_rect = cursor.get_rect(center = (cursor_pos))

    #calculates mouse position, angle and rotation for image
    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - cursor_rect.centerx, my - cursor_rect.centery
    angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

    #rotated image surface
    rot_image      = pygame.transform.rotate(cursor, angle)
    rot_image_rect = rot_image.get_rect(center = cursor_rect.center)


    #simple movement / key presses
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        rot_image_rect.x -= 1
    if keys[pygame.K_d]:
        rot_image_rect.x += 1
    if keys[pygame.K_w]:
        rot_image_rect.y -= 1
    if keys[pygame.K_s]:
        rot_image_rect.y += 1

    
    #bliting all images and surfaces to screen and update screen.
    window.fill((220,216,192))
    window.blit(rot_image, rot_image_rect.topleft)
    pygame.display.update()




pygame.quit()
exit()

您必须更改 cursor_pos 而不是 rot_image_rect。注意 rot_image_rect 是根据 cursor_pos 计算得出的。因此,如果你想改变 rot_image_rect 的位置,你必须改变 cursor_pos:

cursor_pos = list(window.get_rect().center)

run = True
while run:
    # [...]

     #simple movement / key presses
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        cursor_pos[0] -= 1
    # [...]

完整示例:

import math, pygame

pygame.init()
window = pygame.display.set_mode((1280,800))
cursor = pygame.image.load("images/cursor.png").convert_alpha()

#   0 - image is looking to the right
#  90 - image is looking up
# 180 - image is looking to the left
# 270 - image is looking down
correction_angle = 90

cursor_pos = list(window.get_rect().center)

run = True
while run:
    pygame.time.delay(2)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #simple movement / key presses
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        cursor_pos[0] -= 1
    if keys[pygame.K_d]:
        cursor_pos[0] += 1
    if keys[pygame.K_w]:
        cursor_pos[1] -= 1
    if keys[pygame.K_s]:
        cursor_pos[1] += 1

    #cursor postion and rectangle
    cursor_rect = cursor.get_rect(center = (cursor_pos))

    #calculates mouse position, angle and rotation for image
    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - cursor_rect.centerx, my - cursor_rect.centery
    angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

    #rotated image surface
    rot_image      = pygame.transform.rotate(cursor, angle)
    rot_image_rect = rot_image.get_rect(center = cursor_rect.center)
    
    #bliting all images and surfaces to screen and update screen.
    window.fill((220,216,192))
    window.blit(rot_image, rot_image_rect.topleft)
    pygame.display.update()


pygame.quit()
exit()