python - 如何让我的播放器向鼠标位置旋转?

标签 python pygame

基本上,我需要让玩家面对鼠标指针,虽然我可以看到正在发生一些事情,但这根本不是我所需要的。

我知道之前已经有人问过这个问题,但尝试实现这些答案似乎不起作用。因此,如果有人可以查看我的代码并告诉我哪里搞砸了,那将不胜感激!

class Player(pygame.sprite.Sprite):
    def __init__(self, game, x, y):
        self._layer = PLAYER_LAYER
        self.groups = game.all_sprites
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = game.player_img
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.hit_rect = PLAYER_HIT_RECT
        self.hit_rect.center = self.rect.center
        self.vel = vec(0, 0)
        self.pos = vec(x, y)
        self.rot = 0
    def update(self):
        rel_x, rel_y = pygame.mouse.get_pos() - self.pos
        self.rot = -math.degrees(math.atan2(rel_y, rel_x))
        self.image = pygame.transform.rotate(self.game.player_img, self.rot)
        self.rect = self.image.get_rect()
        self.rect.center = self.pos
        self.pos += self.vel * self.game.dt

class Camera:
    def __init__(self, width, height):
        self.camera = pygame.Rect(0, 0, width, height)
        self.width = width
        self.height = height

    def apply(self, entity):
        return entity.rect.move(self.camera.topleft)

    def apply_rect(self, rect):
        return rect.move(self.camera.topleft)

    def update(self, target):
        x = -target.rect.centerx + int(WIDTH / 2)
        y = -target.rect.centery + int(HEIGHT / 2)

        x = min(-TILESIZE, x)
        y = min(-TILESIZE, y)
        x = max(-(self.width - WIDTH - TILESIZE), x)
        y = max(-(self.height - HEIGHT - TILESIZE), y)
        self.camera = pygame.Rect(x, y, self.width, self.height)

将我的播放器放置在没有相机偏移的左上角,可以使旋转工作,但是当放置在其他地方时,它会搞砸。

最佳答案

参见How to rotate an image(player) to the mouse direction? 。您想要执行的操作取决于播放器的哪一部分(顶部或右侧等)应面向鼠标。

不要计算和求和相对角度。计算从玩家到鼠标的向量:

player_x, player_y = # position of the player
mouse_x, mouse_y   = pygame.mouse.get_pos()
   
dir_x, dir_y = mouse_x - player_x, mouse_y - player_y

向量的角度可以通过 math.atan2 计算出来。必须相对于玩家的基本方向来计算角度。

例如

播放器的右侧面向鼠标:

angle = (180 / math.pi) * math.atan2(-dir_y, dir_x)

播放器的顶部面向鼠标:

angle = (180 / math.pi) * math.atan2(-dir_x, -dir_y)

可以使用校正角度设置基本对齐。例如,玩家查看右上角时为 45:

angle = (180 / math.pi) * math.atan2(-dir_y, dir_x) - 45

方法update可能如下所示:

def update(self):
    self.pos += self.vel * self.game.dt

    mouse_x, mouse_y = pygame.mouse.get_pos()
    player_x, player_y = self.pos

    dir_x, dir_y = mouse_x - player_x, mouse_y - player_y
    
    #self.rot = (180 / math.pi) * math.atan2(-dir_y, dir_x)
    #self.rot = (180 / math.pi) * math.atan2(-dir_y, dir_x) - 45
    self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)
    
    self.image = pygame.transform.rotate(self.game.player_img, self.rot)
    self.rect = self.image.get_rect()
    self.rect.center = self.pos

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

关于python - 如何让我的播放器向鼠标位置旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56627414/

相关文章:

python - 不使用点符号间接访问 Python 实例属性

Python 到 SQLite3

Python:将列表转换为字典

python - Pygame 简单程序测试显示卡住

python - 如何使用Python提取嵌套的JSON数据?

python - 如何循环回到开头

python - pip 安装失败,显示 "connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:598)"

python - pygame以相反方向绘制项目

python - 我想用 Pygame 做一个游戏。但是我的箭头无法从正确的位置射出

python - 如何在 python/pygame 中制作按钮?