问题描述
这听起来可能令人困惑,所以我会尽力描述。
我写了一些简单的代码,在窗口中间有一个图像,当鼠标悬停在窗口上时,它将面向鼠标位置(旋转以面向它)。这很好,但我在尝试使用 WASD 移动图像时也遇到了大麻烦。
我想为图像添加基本的自上而下的 WASD 移动,但无论我尝试什么,它总是被旋转代码“覆盖”(我认为),使其无法移动并停留在屏幕的直接中间。>
我能做的最好的事情就是将它从中间向任何方向移动一定量(例如 1 个像素的移动),但是如果我按住键,它会从中间保持 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
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()