python - 如何将图像(播放器)旋转到鼠标方向?

标签 python pygame

我正在考虑在 pygame 中制作一个 2d 射击游戏,我想让我的玩家(Player_1)指向鼠标方向。我花了几个小时寻找解决方案,并尝试了我能找到的所有解决方案,但没有一个有效,所以你可以吗请帮助我 ? 这是我的代码:

import pygame, sys, os
from pygame.locals import *
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
#Exit settings
def quit():
  pygame.quit()
  sys.quit()
def events():
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            quit()

#IDS
CLOCK=pygame.time.Clock()
FPS=120
DS=pygame.display.set_mode((0,0), pygame.FULLSCREEN)
pygame.display.set_caption("Shooting simulator")
W,H=DS.get_size()
P_X=W/2-50
P_Y=H/2-50

#Colors
Red=pygame.Color("#FF0000")
Blue=pygame.Color("#0000FF")
Green=pygame.Color("#00FF00")
Black=pygame.Color("#000000")
White=pygame.Color("#FFFFFF")

#IGT(in game things)
Player_1=pygame.image.load("Img/Player_1.png").convert()

def game_loop():
  while True:
    events()

    DS.fill(White)
    DS.blit(Player_1,(P_X,P_Y))
    pygame.display.flip()
game_loop()

This is my player(Player_1)

我将非常感谢所有的帮助。

最佳答案

您必须计算从玩家到鼠标的矢量角度。通过 pygame.mouse.get_pos() 获取鼠标位置以及玩家周围的矩形 ( pygame.Rect ):

mx, my = pygame.mouse.get_pos()
player_rect = Player_1.get_rect(topleft=(P_X,P_Y))

计算从玩家到鼠标的向量,并通过 math.atan2 计算向量的角度。 y 轴需要反转(-dy),因为 y 轴通常指向上方,但在 PyGame 坐标系中 y 轴指向下方。

dx, dy = mx - player_rect.centerx, player_rect.centery - my
angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

此外,还必须扣除校正角度(- Correction_angle)。校正角度取决于Sprite。如果 Sprite

向右看,校正角度为 0: Correction_angle = 0
仰视,校正角度为 90: Correction_angle = 90
向左看,校正角度为180: Correction_angle = 180
向下看,校正角度为270: Correction_angle = 270

使用 pygame.transform.rotate() 旋转播放器通过围绕其中心的角度:
(另请参阅How do I rotate an image around its center using Pygame?)

rot_image = pygame.transform.rotate(Player_1, angle)
rot_image_rect = rot_image.get_rect(center=player_rect.center)
<小时/>

最小示例: repl.it/@Rabbid76/PyGame-RotateWithMouse

import math
import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
player = pygame.image.load("player.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:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    player_pos  = window.get_rect().center
    player_rect = player.get_rect(center = player_pos)

    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - player_rect.centerx, my - player_rect.centery
    angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

    rot_image      = pygame.transform.rotate(player, angle)
    rot_image_rect = rot_image.get_rect(center = player_rect.center)

    window.fill((255, 255, 255))
    window.blit(rot_image, rot_image_rect.topleft)
    pygame.display.flip()

pygame.quit()
exit()

关于python - 如何将图像(播放器)旋转到鼠标方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603835/

相关文章:

python - scipy.sparse 中缺少 save_npz 方法

python - Elasticsearch-Python-具有刷新功能的批量助手API

python - 如何在pygame中控制单个子弹

python - Pygame 缩放 Sprite

python - 将 Sprite 片段集成到迷宫程序中

python - 正则表达式删除分号和句号之间的字符

python - 当我在包含字典的列表中进行枚举时,出现列表索引超出范围错误

python - 什么是 _PyEval_EvalFrameDefault?

python - 存在 midi 文件但无法使用 pygame 加载

python - 乒乓克隆碰撞检测