Skip to main content
 首页 » 编程设计

python之无法理解如何在 PyGame 中制作角色脸鼠标

2025年05月04日14落叶无声

以下是我目前所拥有的。此刻,玩家在移动,我现在只想让它始终面向鼠标光标(想想迈阿密热线或其他自上而下的游戏)。我使用了一些我在网上找到的涉及 atan2 的代码,但我几乎不明白它的作用(以某种方式找到玩家和鼠标之间的距离......)并且我的角色只是在屏幕外疯狂旋转直到我得到“内存不足”错误。任何帮助将不胜感激,我在整个互联网上都看过,找不到任何帮助我的东西。

import pygame, sys, math 
from pygame.locals import * 
 
pygame.init() 
 
#setup the window with a 720p resolution and a title 
winX, winY = 1280, 720 
display = pygame.display.set_mode((winX, winY)) 
winTitle = pygame.display.set_caption("Game name") 
 
#Colours 
cBlack = (0, 0, 0) 
 
#setup the clock and FPS limit 
FPS = 70 
clock = pygame.time.Clock() 
 
#sprite groups 
allSprites = pygame.sprite.Group() 
 
#background image 
bg = pygame.image.load("bg.jpg") 
 
class Player(pygame.sprite.Sprite): 
    def __init__(self, speed): 
        #import the __init__ from the Sprite class 
        pygame.sprite.Sprite.__init__(self) 
        #sets the objects speed to the speed argument 
        self.speed = speed 
        #set the soldiers image 
        self.image = pygame.image.load("soldier_torso_DW.png") 
        #get the rectangle of the image (used for movement later) 
        self.rect = self.image.get_rect() 
        #sets starting position 
        self.rect.x, self.rect.y = winX/2, winY/2 
 
    def checkPlayerMovement(self): 
        """Check if user has pressed any movement buttons 
            and sets the characters position accordingly.""" 
        pressed = pygame.key.get_pressed() 
        if pressed[K_w]: 
            self.rect.y -= self.speed 
        if pressed[K_s]: 
            self.rect.y += self.speed 
        if pressed[K_a]: 
            self.rect.x -= self.speed 
        if pressed[K_d]: 
            self.rect.x += self.speed 
 
    def checkMouseMovement(self): 
        """Check if user has moved the mouse and rotates the 
            character accordingly.""" 
        mX, mY = pygame.mouse.get_pos() 
        angleRad = math.atan2(mY, mX) - math.atan2(self.rect.y, self.rect.x) 
        angleDeg = math.degrees(angleRad) 
        print angleDeg 
        self.image = pygame.transform.rotate(self.image, angleDeg) 
 
    def update(self): 
        """Performs all movement and drawing functions and shit 
            for the object""" 
        self.checkPlayerMovement() 
        self.checkMouseMovement() 
        #draw the image at the given coordinate 
        display.blit(self.image, (self.rect.x, self.rect.y)) 
 
def updateAll(): 
    #fill the window with black 
    display.blit(bg, (0, 0)) 
    #update all sprites 
    allSprites.update() 
    #keeps FPS at 60 
    clock.tick(FPS) 
    #updates the display 
    pygame.display.flip() 
 
#creates a player with a speed of 5 
mainChar = Player(5) 
#add the player character to main sprite group 
allSprites.add(mainChar) 
 
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit() 
    updateAll() 

请您参考如下方法:

(坦白说,我不熟悉 pygame 包,但我用其他语言做过这种编程)

讨论

当你想让玩家决定它应该旋转多少度以面对一个物体时,玩家应该认为自己是世界中心。但是,它的坐标并不总是 (0,0)。我们首先需要通过从玩家希望面对的对象中减去玩家的位置,将我们的坐标转换为以 self 为中心的坐标。

接下来,您似乎只使用玩家的位置来计算旋转。除非玩家总是面对一个方向等于从其当前位置到原点的矢量方向,否则这不是正确的计算。您必须包含额外的信息来跟踪玩家的旋转,这通常是通过具有 (x, y) 分量的单位方向向量来完成的。

方法

  1. 计算从对象位置到玩家位置的相对向量
    • 将此向量称为 Vrel
  2. 计算 atan2(Vrel) - atan2(playerDiry, playerDirx)
    • 结果现在是你的玩家需要旋转的量,相对于它本身
  3. 将 sprite 平移到原点,执行旋转,再平移回来(我确信 pygame 具有原地旋转的功能)

atan2() 函数

atan2 函数将为您提供相对于正 X 轴的有符号 旋转。

因此,给定笛卡尔 X-Y 平面和位置 (x, y),如果您绘制从 (0, 0) 到 (x, y) 的向量,则 atan2(y, x) 将给出两者之间的角度向量 (1, 0) 和 (x, y)。例如,如果您的 (x, y) 是 (0, 1)(沿 y 轴),则 atan2(1, 0) 将为您提供 π/2 或 90°,并且如果您的 (x, y)沿负 y 轴 (0, -1),则 atan2(0, -1) 将为您提供 -π/2 或 -90°。

atan2 输出的正角度始终为逆时针旋转,负角度为顺时针旋转。大多数计算器确实需要 atan2(y, x) 的形式,但不是全部,所以要小心。 In Python's math library, it is atan2(y, x)

例子

请原谅不按比例、颜色选择不当、幼儿园质量的图片,并尝试专注于概念。

对象在位置 (5, 5) 位置 (-3, -1) 的玩家。 玩家面向方向 (-0.8, 0.6)

  1. 相对向量是 (Objectx - Playerx, Objecty - Playery) = (5 , 5) - (-3, -1) = (5 - -3, 5 - -1) = (5+3, 5+1) = (8, 6)

  1. atan2(Vrel) = atan2(6, 8) = 0.9272952180016122 弧度

    • atan2(playerDiry, playerDirx) = atan2(-0.8, 0.6) = 2.498091544796509 rad



  • atan2(Vrel) - atan2(玩家) = 0.9272952180016122 - 2.498091544796509 = -1.5707963267948966 弧度

所以玩家必须围绕自己的中心旋转 -1.57 弧度(顺时针近 90 度)才能面对物体。